Example usage for org.apache.poi.ss.usermodel Row getRowNum

List of usage examples for org.apache.poi.ss.usermodel Row getRowNum

Introduction

In this page you can find the example usage for org.apache.poi.ss.usermodel Row getRowNum.

Prototype

int getRowNum();

Source Link

Document

Get row number this row represents

Usage

From source file:edu.si.sidora.excel2tabular.FilteredSheet.java

License:Apache License

/**
 * Examine a sheet for data rows and record the results.
 *//*from  w  w  w .ja v  a  2 s  .c om*/
private void findDataRows() {
    initializing = true;

    // begin by assuming that all rows might be data rows
    final int lastRowIndex = sheet.getLastRowNum();
    final int firstRowIndex = sheet.getFirstRowNum();
    dataRange = closed(firstRowIndex, lastRowIndex);

    // Because the rows in a sheet are not ordered by length, we will have to traverse all of them to find a row
    // of maximum length. This gives us an opportunity to record any blank rows at the same time.
    final Row maximalRow = compareByLengthAndRecordBlankRows.max(this);
    final int maximalRowIndex = maximalRow.getRowNum();

    if (isBlankRow(maximalRow)) {
        log.trace("The maximal row was empty, so this sheet has no data.");
        dataRange = EMPTY_RANGE;
        return;
    }
    log.trace("Found index of maximally long data row at: {} with length: {}", maximalRowIndex,
            maximalRow.getLastCellNum());
    // search for nearest blank row after the maximal row
    final Integer nextBlankRowIndex = blankRows.higher(maximalRowIndex);
    final int lastDataRowIndex = nextBlankRowIndex == null ? lastRowIndex : nextBlankRowIndex - 1;

    // search for nearest blank row before the maximal row
    final Integer previousBlankRowIndex = blankRows.lower(maximalRowIndex);
    final int firstDataRowIndex = previousBlankRowIndex == null ? firstRowIndex : previousBlankRowIndex + 1;
    dataRange = closed(firstDataRowIndex, lastDataRowIndex);
    log.trace("Found data range: {}", dataRange);
    initializing = false;
    initialized = true;
}

From source file:edu.swjtu.excel.InportArrExcel.java

/**
 * ?excel//from w w w .ja  v  a2 s  . com
 * 2016716?7:24:58
 * @author jimolonely
 * @param excelPath
 * @throws IOException
 * @throws InstantiationException
 * @throws IllegalAccessException
 */
public ArrayList<ArrCarLine> inport(String excelPath)
        throws IOException, InstantiationException, IllegalAccessException {
    InputStream fis = new FileInputStream(excelPath);
    String fileType = excelPath.substring(excelPath.lastIndexOf(".") + 1, excelPath.length());
    ArrayList<ArrCarLine> data = new ArrayList<ArrCarLine>();
    Workbook wb = null;
    if (fileType.equals("xlsx")) {
        wb = new XSSFWorkbook(fis);
    } else if (fileType.equals("xls")) {
        wb = new HSSFWorkbook(fis);
    }
    Sheet sht0 = wb.getSheetAt(0);
    for (Row r : sht0) {
        if (r.getRowNum() < 1) {
            continue;// 
        }
        ArrCarLine acl = new ArrCarLine();
        for (int i = 0; i < 7; i++) {
            if (r.getCell(i) == null)
                return null;
            r.getCell(i).setCellType(Cell.CELL_TYPE_STRING);
            String cellValue = r.getCell(i).getStringCellValue();

            switch (i) {
            case 0:
                acl.setArrangeId(Integer.parseInt(cellValue));
                break;
            case 1:
                acl.setArrName(cellValue);
                break;
            case 2:
                acl.setDate(cellValue);
                break;
            case 3:
                acl.setTime(cellValue);
                break;
            case 4:
                acl.setLineName(cellValue);
                break;
            case 5:
                acl.setLicensePlate(cellValue);
                break;
            case 6:
                acl.setDriver(cellValue);
                break;
            }
        }
        data.add(acl);
    }
    fis.close();
    return data;
}

From source file:energy.usef.pbcfeeder.PbcFeeder.java

License:Apache License

private void fillColStubInputMap(HSSFSheet pbcDataSheet) {
    stubColInputMap = new HashMap<>();
    for (Cell topRowCell : pbcDataSheet.getRow(0)) {
        if (topRowCell.getStringCellValue().equals("")) {
            break;
        }/*  w w w  .ja va2 s . c om*/
        String columnName = topRowCell.getStringCellValue();
        List<Double> colValues = new ArrayList<>();
        for (Row r : pbcDataSheet) {
            if (r.getRowNum() == 0) {
                continue;
            }
            Cell c = r.getCell(topRowCell.getColumnIndex());
            if (c != null) {
                if (c.getCellType() == Cell.CELL_TYPE_NUMERIC) {
                    colValues.add(c.getNumericCellValue());
                }
            }
        }
        stubColInputMap.put(columnName, colValues);
    }
}

From source file:energy.usef.pbcfeeder.PbcFeeder.java

License:Apache License

private void fillStubRowInputList(HSSFSheet pbcDataSheet) {
    stubRowInputList = new ArrayList<>();
    for (Row r : pbcDataSheet) {
        if (r.getRowNum() == 0) {
            continue;
        }/*w ww  .j  a  v a  2s.  c  o  m*/
        PbcStubDataDto row = new PbcStubDataDto();
        for (Cell c : r) {
            if (c != null) {
                if (c.getCellType() == Cell.CELL_TYPE_NUMERIC) {
                    fillPbcStubDataDto(c, row);
                }
            }
        }
        stubRowInputList.add(row);
    }
}

From source file:energy.usef.pbcfeeder.PbcFeeder.java

License:Apache License

/**
 * This method fills in the map with CongestionPoint-->Lower Power Limit and the map CongestionPoint-->Upper Power Limit.
 *
 * @param pbcCongestionPointLimitsSheet the excel page with the power limits.
 *//* w  ww  .  j a  v  a 2s.  c om*/
private void fillCongestionPointLimitsMaps(HSSFSheet pbcCongestionPointLimitsSheet) {
    congestionPointLowerLimitMap = new HashMap<>();
    congestionPointUpperLimitMap = new HashMap<>();
    for (Cell columnHeading : pbcCongestionPointLimitsSheet.getRow(0)) {
        // stop when one encounters the first empty cell.
        if (StringUtils.isBlank(columnHeading.getStringCellValue())) {
            break;
        }
        for (Row row : pbcCongestionPointLimitsSheet) {
            // skip first row.
            if (row.getRowNum() == 0) {
                continue;
            }
            String congestionPoint = row.getCell(0).getStringCellValue();
            Cell cell = row.getCell(columnHeading.getColumnIndex());
            if (LOWER_LIMIT.equals(columnHeading.getStringCellValue())) {
                congestionPointLowerLimitMap.put(congestionPoint, new BigDecimal(cell.getNumericCellValue()));
            }
            if (UPPER_LIMIT.equals(columnHeading.getStringCellValue())) {
                congestionPointUpperLimitMap.put(congestionPoint, new BigDecimal(cell.getNumericCellValue()));
            }
        }
    }
}

From source file:examples.toHTML.ToHtml.java

License:Apache License

private void printSheetContent(Sheet sheet) {
    printColumnHeads();/*from  w w w  .  j  a v a2  s  .c  o  m*/

    out.format("<tbody>%n");
    Iterator<Row> rows = sheet.rowIterator();
    while (rows.hasNext()) {
        Row row = rows.next();

        out.format("  <tr>%n");
        out.format("    <td class=\"%s\">%d</td>%n", ROW_HEAD_CLASS, row.getRowNum() + 1);
        for (int i = firstColumn; i < endColumn; i++) {
            // &nbsp;
            String content = " ";
            String attrs = "";
            CellStyle style = null;
            if (i >= row.getFirstCellNum() && i < row.getLastCellNum()) {
                Cell cell = row.getCell(i);
                if (cell != null) {
                    style = cell.getCellStyle();
                    attrs = tagStyle(cell, style);
                    //Set the value that is rendered for the cell
                    //also applies the format
                    CellFormat cf = CellFormat.getInstance(style.getDataFormatString());
                    CellFormatResult result = cf.apply(cell);
                    content = result.text;

                    content = replaceUmlaut(content);

                    if (content.equals(""))
                        // &nbsp;
                        content = " ";
                }
            }
            out.format("    <td class=\"%s %s\">%s</td>%n", styleName(style), attrs, content);
        }
        out.format("  </tr>%n");
    }
    out.format("</tbody>%n");
}

From source file:Excel.ExcelToJTable.java

static void fillData(File file) {
    try {//w  ww.ja  va 2 s .c o m
        FileInputStream fs = new FileInputStream(file);
        XSSFWorkbook workbook = new XSSFWorkbook(fs);
        XSSFSheet sheet = workbook.getSheetAt(0);
        Iterator<Row> rowIterator = sheet.iterator();
        while (rowIterator.hasNext()) {
            Row row = rowIterator.next();
            Iterator<Cell> cellIterator = row.cellIterator();
            if (row.getRowNum() == 0) {
                while (cellIterator.hasNext()) {
                    Cell cell = cellIterator.next();
                    columns.add(cell.getStringCellValue());
                }
            } else {
                dataTemp = new Vector();
                while (cellIterator.hasNext()) {
                    Cell cell = cellIterator.next();
                    switch (cell.getCellType()) {
                    case Cell.CELL_TYPE_STRING:
                        dataTemp.add(cell.getStringCellValue());
                        break;
                    case Cell.CELL_TYPE_NUMERIC:
                        dataTemp.add(cell.getNumericCellValue());
                        break;
                    case Cell.CELL_TYPE_BLANK:
                        dataTemp.add("");
                        break;
                    default:
                        break;
                    }
                }
                data.add(dataTemp);
                fs.close();
            }
        }

    } catch (Exception ex) {
        System.out.print("Cause: \n" + ex.getCause() + "\n Message: \n" + ex.getMessage() + "\n Stack Trace: \n"
                + Arrays.toString(ex.getStackTrace()));
    }
}

From source file:excelcon.ExcelCon.java

public static String done(String[][] names, XSSFSheet sheet, File selectedDirectory) throws IOException {

    String headBlock = "";
    String s = "";
    int namesLength = names.length;
    String[] statment = new String[namesLength];
    File[] file = new File[names.length];
    BufferedWriter[] writer = new BufferedWriter[names.length];
    for (int i = 0; i < names.length; i++) {
        file[i] = new File(selectedDirectory.getPath() + "\\" + names[i][0] + " for sheet "
                + sheet.getSheetName() + ".txt");

        writer[i] = new BufferedWriter(new FileWriter(file[i]));
        statment[i] = "";

    }//w w w  .  j  av a2  s  .  c  o  m

    for (Row row : sheet) {
        int cellIndex = 0;
        String _tempHeadBlock = row.getCell(cellIndex, Row.CREATE_NULL_AS_BLANK).toString();
        if (!_tempHeadBlock.isEmpty()) {
            headBlock = _tempHeadBlock;
        }
        if (headBlock == "") {
            System.out.println("Head Block is empty");
        }
        cellIndex++;
        //System.out.println(headBlock);
        String _tempFeature = row.getCell(cellIndex, Row.CREATE_NULL_AS_BLANK).toString();

        for (int i = 0; i < namesLength; i++) {
            if (_tempFeature.equals(names[i][0])) {
                //System.out.println(_tempFeature);
                cellIndex++;
                try {
                    int no1 = Math
                            .round(Float.valueOf(row.getCell(cellIndex, Row.CREATE_NULL_AS_BLANK).toString()));
                    // System.out.println("No1 :"+ no1);
                    cellIndex++;

                    int no2 = Math
                            .round(Float.valueOf(row.getCell(cellIndex, Row.CREATE_NULL_AS_BLANK).toString()));

                    //System.out.println("No2 :"+ no2);
                    if (no1 < no2) {
                        int tempNo = no1;
                        no1 = no2;
                        no2 = tempNo;
                    }
                    statment[i] = "";
                    statment[i] += names[i][1];
                    statment[i] += no1 + ":" + no1;
                    statment[i] += "\n";
                    statment[i] += headBlock + ":" + no1 + ":" + no2 + "\n";
                    //System.out.println(statment[i]);
                } catch (Exception e) {
                    int rowin = row.getRowNum();
                    rowin += 1;
                    int celle = cellIndex + 1;
                    s += "Sheet: " + sheet.getSheetName() + " Row :" + rowin + " Cell:" + celle + "\n";
                    System.out.println(sheet.getSheetName() + "Row :" + rowin + " Cell:" + celle);

                }
                writer[i].write(statment[i]);

            }
        }

    }
    for (int i = 0; i < names.length; i++) {
        writer[i].write("End of MpartPos list");
        writer[i].close();

    }
    return s;
}

From source file:FileHelper.ExcelHelper.java

public DataSheet ReadTestCaseFileFromSheet(String fileName, String sheetName, MyDataHash myDataHash,
        String rawData) {//  w w  w. j  a v  a2s.  c o m
    try {
        File excel = new File(fileName);
        FileInputStream fis = new FileInputStream(excel);
        XSSFWorkbook book = new XSSFWorkbook(fis);
        XSSFSheet sheet = book.getSheet(sheetName);
        Iterator<Row> itr = sheet.iterator();
        DataSheet dataSheet = new DataSheet();
        ArrayList<RowDataFromFile> datas = new ArrayList<RowDataFromFile>();
        ArrayList<DataHash> dataHash = new ArrayList<>();
        int colmnDataStart = 0, colmnDataStop = 0, numReal = 0;
        ArrayList<NameDynamic> nameDynamic = new ArrayList<NameDynamic>();
        ArrayList<DataInput> listDataInput = new ArrayList<>();
        ArrayList<DataInputLevel2> dataInputLevel2 = new ArrayList<>();
        while (itr.hasNext()) {
            RowDataFromFile dataRow = new RowDataFromFile();
            JsonObject jObjReq = new JsonObject();
            String caller = "";

            Row row = itr.next();
            Iterator<Cell> cellIterator = row.cellIterator();
            Cell cell = cellIterator.next();
            switch (cell.getCellType()) {
            case Cell.CELL_TYPE_STRING: {
                String str = cell.getStringCellValue();
                if (str.equals("STT")) {
                    while (cellIterator.hasNext()) {
                        Cell cell1 = cellIterator.next();
                        switch (cell1.getCellType()) {
                        case Cell.CELL_TYPE_STRING: {
                            //                                        System.out.println(cell1.getStringCellValue());
                            if (cell1.getStringCellValue().equals("Data Request")) {
                                colmnDataStart = cell1.getColumnIndex();
                            }
                            if (cell1.getStringCellValue().equals("Threads")) {
                                colmnDataStop = cell1.getColumnIndex() - 1;
                            }
                            if (cell1.getStringCellValue().equals("Result Real")) {
                                //                                            System.out.println("Colmn Reail: " + cell1.getColumnIndex());
                                numReal = cell1.getColumnIndex();
                            }
                            break;
                        }
                        case Cell.CELL_TYPE_NUMERIC: {
                            System.out.println(cell1.getNumericCellValue());
                            break;
                        }
                        }
                    }
                    Row row1 = sheet.getRow(1);
                    Row row2 = sheet.getRow(2);
                    Row row3 = sheet.getRow(3);
                    Row row4 = sheet.getRow(4);
                    Cell cellColmn;
                    Cell cellColmn2;
                    int numColmn = colmnDataStart;
                    while (numColmn <= colmnDataStop) {
                        cellColmn = row1.getCell(numColmn);
                        String temp = GetValueStringFromCell(cellColmn);
                        cellColmn2 = row2.getCell(numColmn);
                        NameDynamic nameDy = CutStrGetNameDynamic(GetValueStringFromCell(cellColmn2));
                        if (nameDy.getIsDyn().equals("1")) { // Check Data is change when run Thread
                            nameDynamic.add(nameDy);
                        }
                        // Add to list save data api
                        listDataInput.add(new DataInput(temp, nameDy.getName()));
                        DataHash dataHt = myDataHash.CheckNameDataIsHash(sheetName, nameDy.getName());
                        if (dataHt != null) {
                            dataHt.setNumColumn(numColmn);
                            dataHash.add(dataHt);
                        }
                        if (temp.equals("Object")) { // Exist object group datas name
                            ArrayList<DataInput> listDataIputLevel2 = new ArrayList<>();
                            cellColmn = row3.getCell(numColmn);
                            cellColmn2 = row4.getCell(numColmn);
                            String tempT = GetValueStringFromCell(cellColmn);
                            if (!tempT.equals("")) {
                                while (!GetValueStringFromCell(cellColmn).equals("")) {
                                    nameDy = CutStrGetNameDynamic(GetValueStringFromCell(cellColmn2));
                                    if (nameDy.getIsDyn().equals("1")) { // Check Data is change when run Thread
                                        nameDynamic.add(nameDy);
                                    }
                                    dataHt = myDataHash.CheckNameDataIsHash(sheetName, nameDy.getName());
                                    if (dataHt != null) {
                                        dataHt.setNumColumn(numColmn);
                                        dataHash.add(dataHt);
                                    }
                                    listDataIputLevel2.add(
                                            new DataInput(GetValueStringFromCell(cellColmn), nameDy.getName()));
                                    numColmn++;
                                    cellColmn = row3.getCell(numColmn);
                                    cellColmn2 = row4.getCell(numColmn);
                                }
                                numColmn--;
                                dataInputLevel2.add(new DataInputLevel2(listDataIputLevel2));
                            } else {
                                dataInputLevel2.add(new DataInputLevel2(listDataIputLevel2));
                            }
                        }
                        numColmn++;
                    }
                    Gson gson = new Gson();
                    System.out.println(gson.toJson(listDataInput));
                    System.out.println(gson.toJson(dataHash));
                }
                break;
            }
            case Cell.CELL_TYPE_NUMERIC: {
                //                        System.out.println(cell.getNumericCellValue());
                if (cell.getNumericCellValue() > 0) {
                    dataRow.setId(row.getRowNum());
                    String isSecutiry = "no";
                    int arrIndex = 0;
                    int arrIndexReq = 0; // Object con
                    int arrIndexRow = 0;
                    while (cellIterator.hasNext()) {
                        Cell cell1 = cellIterator.next();
                        if ((cell1.getColumnIndex() >= colmnDataStart)
                                && (cell1.getColumnIndex() < colmnDataStop)) {
                            if (listDataInput.get(arrIndex).getType().equals("Object")) {
                                JsonObject jObj = new JsonObject();
                                int i = 0;
                                int size = dataInputLevel2.get(arrIndexReq).getListDataIputLevel2().size();
                                while (i < size) {
                                    if (dataInputLevel2.get(arrIndexReq).getListDataIputLevel2().get(i)
                                            .getType().equals("String")) {
                                        String value = GetValueStringFromCell(cell1);
                                        if (!dataHash.isEmpty()) {
                                            for (DataHash dataH : dataHash) {
                                                if (dataH.getNumColumn() == cell1.getColumnIndex()) {
                                                    value = EncryptHelper.EncryptData(value,
                                                            dataH.getAlgorithm(), dataH.getKey(),
                                                            dataH.getIv());
                                                }
                                            }
                                        }
                                        jObj.addProperty(dataInputLevel2.get(arrIndexReq)
                                                .getListDataIputLevel2().get(i).getName(), value);
                                    } else if (dataInputLevel2.get(arrIndexReq).getListDataIputLevel2().get(i)
                                            .getType().equals("Integer")) {
                                        int value = GetValueIntegerFromCell(cell1);
                                        jObj.addProperty(dataInputLevel2.get(arrIndexReq)
                                                .getListDataIputLevel2().get(i).getName(), value);
                                    } else if (dataInputLevel2.get(arrIndexReq).getListDataIputLevel2().get(i)
                                            .getType().equals("Object")) {
                                        String value = GetValueStringFromCell(cell1);
                                        Gson gson = new Gson();
                                        JsonObject obj = gson.fromJson(value, JsonObject.class);
                                        jObj.add(dataInputLevel2.get(arrIndexReq).getListDataIputLevel2().get(i)
                                                .getName(), obj);
                                    }
                                    i++;
                                    if (i < size) {
                                        cell1 = cellIterator.next();
                                    }
                                }
                                arrIndexReq++;
                                jObjReq.add(listDataInput.get(arrIndex).getName(), jObj);
                            } else if (listDataInput.get(arrIndex).getType().equals("String")) {
                                String value = GetValueStringFromCell(cell1);
                                if (!dataHash.isEmpty()) {
                                    for (DataHash dataH : dataHash) {
                                        if (dataH.getNumColumn() == cell1.getColumnIndex()) {
                                            value = EncryptHelper.EncryptData(value, dataH.getAlgorithm(),
                                                    dataH.getKey(), dataH.getIv());
                                        }
                                    }
                                }
                                jObjReq.addProperty(listDataInput.get(arrIndex).getName(), value);
                            } else if (listDataInput.get(arrIndex).getType().equals("Integer")) {
                                int value = GetValueIntegerFromCell(cell1);
                                jObjReq.addProperty(listDataInput.get(arrIndex).getName(), value);
                            }
                            arrIndex++;
                        } else if (cell1.getColumnIndex() == colmnDataStop) {
                            isSecutiry = GetValueStringFromCell(cell1);
                            dataRow.setNameAlgorithm(isSecutiry);
                        } else if (cell1.getColumnIndex() > colmnDataStop) {
                            if (arrIndexRow == 0) {
                                dataRow.setThread(GetValueIntegerFromCell(cell1));
                            } else if (arrIndexRow == 1) {
                                dataRow.setResultExpect(GetValueStringFromCell(cell1));
                            }
                            arrIndexRow++;
                        }
                    }
                    //                            System.out.println("data: " + jObj.toString());
                    //                            System.out.println("data Req: " + jObjReq.toString());
                    String[] arrR = rawData.split(",");
                    String rawDataNew = "";
                    char a = '"';
                    for (String str : arrR) {
                        if (str.charAt(0) == a) {
                            String value = str.substring(1, str.length() - 1);
                            rawDataNew += value;
                        } else {
                            JsonElement je = jObjReq.get(str);
                            if (je.isJsonObject()) {
                                String value = je.toString();
                                rawDataNew += value;
                            } else {
                                String value = je.getAsString();
                                rawDataNew += value;
                            }
                        }
                    }
                    String[] arr = isSecutiry.split("-");
                    if (arr[0].equals("chksum")) {
                        String chksum = CheckSumInquireCard.createCheckSum(isSecutiry, rawDataNew);
                        //                                System.out.println("chksum: " + chksum);
                        jObjReq.addProperty(listDataInput.get(arrIndex).getName(), chksum);
                    } else if (arr[0].equals("signature")) {
                        String signature = RSASHA1Signature.getSignature(isSecutiry, rawDataNew);
                        //                                System.out.println("signature: " + signature);
                        jObjReq.addProperty(listDataInput.get(arrIndex).getName(), signature);
                    }
                    //                            System.out.println("data Request: " + jObjReq.toString());
                    dataRow.setData(jObjReq);
                    dataRow.setNumReal(numReal);
                    Gson gson = new Gson();
                    System.out.println("data row: " + gson.toJson(dataRow));
                    datas.add(dataRow);
                }
                break;
            }
            }
        }
        dataSheet.setDatas(datas);
        dataSheet.setNameDynamic(nameDynamic);
        dataSheet.setListDataInput(listDataInput);
        dataSheet.setDataInputLevel2(dataInputLevel2);
        Gson gson = new Gson();
        //            System.out.println("save data: " + gson.toJson(datas));
        fis.close();
        return dataSheet;
    } catch (Throwable t) {
        System.out.println("Throwsable: " + t.getMessage());
        return new DataSheet();
    }
}

From source file:fr.amapj.service.engine.generator.excel.ExcelGeneratorTool.java

License:Open Source License

/**
 * Retourne le label de la cellule indiqu, par exemple A9
 * /*from  www .j av a2  s . co  m*/
 * @param row
 * @param colIndex
 * @return
 */
public String getCellLabel(Row row, int colIndex) {
    return CellReference.convertNumToColString(colIndex) + (row.getRowNum() + 1);
}