Example usage for org.apache.poi.ss.usermodel Sheet getLastRowNum

List of usage examples for org.apache.poi.ss.usermodel Sheet getLastRowNum

Introduction

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

Prototype

int getLastRowNum();

Source Link

Document

Gets the last row on the sheet Note: rows which had content before and were set to empty later might still be counted as rows by Excel and Apache POI, so the result of this method will include such rows and thus the returned value might be higher than expected!

Usage

From source file:org.seasar.fisshplate.wrapper.SheetWrapper.java

License:Apache License

public SheetWrapper(Sheet sheet, WorkbookWrapper workbook, int sheetIndex) {
    this.workbook = workbook;
    this.hssfSheet = sheet;
    this.sheetIndex = sheetIndex;
    for (int i = 0; i <= sheet.getLastRowNum(); i++) {
        rowList.add(new RowWrapper(sheet.getRow(i), this));
    }//from   w ww. j  a v a  2 s.  co m
}

From source file:org.spionen.james.jamesfile.ExcelJamesFile.java

License:Open Source License

public Map<Long, Subscriber> readFile(File file) throws IOException {
    Map<Long, Subscriber> subscribers = new TreeMap<Long, Subscriber>();
    try {//from  ww w  .j a v  a  2  s  .c  om
        Workbook wb = WorkbookFactory.create(file);
        Sheet s = wb.getSheetAt(0);

        // Take first row, use to check order of fields
        Row firstRow = s.getRow(s.getFirstRowNum());
        FieldType[] order = new FieldType[firstRow.getLastCellNum() - firstRow.getFirstCellNum()];
        int j = 0;
        for (int i = firstRow.getFirstCellNum(); i < firstRow.getLastCellNum(); i++, j++) {
            Cell c = firstRow.getCell(i);
            if (c != null) {
                if (c.getCellType() == Cell.CELL_TYPE_NUMERIC) {
                    String val = new Integer(new Double(c.getNumericCellValue()).intValue()).toString();
                    order[j] = FieldType.getFieldType(val);
                } else {
                    order[j] = FieldType.getFieldType(c.getStringCellValue());
                }
            }
        }
        // Then iterate through the rest of the rows
        if (s.getLastRowNum() > 0) {
            // LastRowNum is 0-indexed, so add 1
            for (int i = s.getFirstRowNum() + 1; i < s.getLastRowNum() + 1; i++) {
                Row r = s.getRow(i);
                Subscriber sub = new Subscriber();
                j = 0;
                // LastCellNum is also 0-indexed
                for (int k = r.getFirstCellNum(); k < r.getLastCellNum() + 1; k++, j++) {
                    Cell c = r.getCell(k);
                    if (c != null) {
                        if (c.getCellType() == Cell.CELL_TYPE_NUMERIC) {
                            String val = new Integer(new Double(c.getNumericCellValue()).intValue()).toString();
                            sub.setByField(order[j], val);
                        } else {
                            sub.setByField(order[j], c.getStringCellValue());
                        }
                    }
                }
                subscribers.put(sub.getAbNr(), sub);
            }
        }
        return subscribers;
    } catch (InvalidFormatException ife) {
        // If the file was badly formatted
        throw new IOException(ife);
    }
}

From source file:org.squashtest.tm.service.internal.batchexport.ExcelExporter.java

License:Open Source License

private void appendTestCases(ExportModel model) {
    List<TestCaseModel> models = model.getTestCases();
    Sheet tcSheet = workbook.getSheet(TC_SHEET);
    Row r;/*from   w w w .  j a v  a2s .  co m*/
    int rIdx = tcSheet.getLastRowNum() + 1;
    int cIdx = 0;

    for (TestCaseModel tcm : models) {

        r = tcSheet.createRow(rIdx);

        try {
            r.createCell(cIdx++).setCellValue(tcm.getProjectId());
            r.createCell(cIdx++).setCellValue(tcm.getProjectName());
            r.createCell(cIdx++).setCellValue(tcm.getPath());
            r.createCell(cIdx++).setCellValue(tcm.getOrder());
            r.createCell(cIdx++).setCellValue(tcm.getId());
            r.createCell(cIdx++).setCellValue(tcm.getReference());
            r.createCell(cIdx++).setCellValue(tcm.getName());
            if (milestonesEnabled) {
                r.createCell(cIdx++).setCellValue(tcm.getMilestone());
            }
            r.createCell(cIdx++).setCellValue(tcm.getWeightAuto());
            r.createCell(cIdx++).setCellValue(tcm.getWeight().toString());
            r.createCell(cIdx++).setCellValue(tcm.getNature().getCode());
            r.createCell(cIdx++).setCellValue(tcm.getType().getCode());
            r.createCell(cIdx++).setCellValue(tcm.getStatus().toString());
            r.createCell(cIdx++).setCellValue(tcm.getDescription());
            r.createCell(cIdx++).setCellValue(tcm.getPrerequisite());
            r.createCell(cIdx++).setCellValue(tcm.getNbReq());
            r.createCell(cIdx++).setCellValue(tcm.getNbCaller());
            r.createCell(cIdx++).setCellValue(tcm.getNbAttachments());
            r.createCell(cIdx++).setCellValue(format(tcm.getCreatedOn()));
            r.createCell(cIdx++).setCellValue(tcm.getCreatedBy());
            r.createCell(cIdx++).setCellValue(format(tcm.getLastModifiedOn()));
            r.createCell(cIdx++).setCellValue(tcm.getLastModifiedBy());

            appendCustomFields(r, "TC_CUF_", tcm.getCufs());
            cIdx = doOptionnalAppendTestCases(r, cIdx, tcm);

        } catch (IllegalArgumentException wtf) {
            if (LOGGER.isWarnEnabled()) {
                LOGGER.warn("cannot export content for test case '" + tcm.getId()
                        + "' : some data exceed the maximum size of an excel cell");
            }
            if (LOGGER.isTraceEnabled()) {
                LOGGER.trace("", wtf);
            }
            tcSheet.removeRow(r);
            r = tcSheet.createRow(rIdx);

            r.createCell(0).setCellValue(errorCellTooLargeMessage);

        }
        rIdx++;
        cIdx = 0;
    }
}

From source file:org.squashtest.tm.service.internal.batchexport.ExcelExporter.java

License:Open Source License

private void appendTestSteps(ExportModel model) {

    List<TestStepModel> models = model.getTestSteps();
    Sheet stSheet = workbook.getSheet(ST_SHEET);

    Row r;//w  w  w.j av  a 2  s .  c  om
    int rIdx = stSheet.getLastRowNum() + 1;
    int cIdx = 0;

    for (TestStepModel tsm : models) {

        r = stSheet.createRow(rIdx);

        try {
            r.createCell(cIdx++).setCellValue(tsm.getTcOwnerPath());
            r.createCell(cIdx++).setCellValue(tsm.getTcOwnerId());
            r.createCell(cIdx++).setCellValue(tsm.getId());
            r.createCell(cIdx++).setCellValue(tsm.getOrder());
            r.createCell(cIdx++).setCellValue(tsm.getIsCallStep());
            r.createCell(cIdx++).setCellValue(tsm.getDsName());
            r.createCell(cIdx++).setCellValue(tsm.getAction());
            r.createCell(cIdx++).setCellValue(tsm.getResult());
            r.createCell(cIdx++).setCellValue(tsm.getNbReq());
            r.createCell(cIdx++).setCellValue(tsm.getNbAttach());

            appendCustomFields(r, "TC_STEP_CUF_", tsm.getCufs());
        } catch (IllegalArgumentException wtf) {
            if (LOGGER.isWarnEnabled()) {
                LOGGER.warn("cannot export content for test step '" + tsm.getId()
                        + "' : some data exceed the maximum size of an excel cell");
            }
            if (LOGGER.isTraceEnabled()) {
                LOGGER.trace("", wtf);
            }
            stSheet.removeRow(r);
            r = stSheet.createRow(rIdx);

            r.createCell(0).setCellValue(errorCellTooLargeMessage);

        }

        rIdx++;
        cIdx = 0;
    }
}

From source file:org.squashtest.tm.service.internal.batchexport.ExcelExporter.java

License:Open Source License

private void appendParameters(ExportModel model) {

    List<ParameterModel> models = model.getParameters();
    Sheet pSheet = workbook.getSheet(PRM_SHEET);

    Row r;//from   w  w w.  j av  a 2 s. co  m
    int rIdx = pSheet.getLastRowNum() + 1;
    int cIdx = 0;

    for (ParameterModel pm : models) {
        r = pSheet.createRow(rIdx);

        try {
            r.createCell(cIdx++).setCellValue(pm.getTcOwnerPath());
            r.createCell(cIdx++).setCellValue(pm.getTcOwnerId());
            r.createCell(cIdx++).setCellValue(pm.getId());
            r.createCell(cIdx++).setCellValue(pm.getName());
            r.createCell(cIdx++).setCellValue(pm.getDescription());
        } catch (IllegalArgumentException wtf) {

            if (LOGGER.isWarnEnabled()) {
                LOGGER.warn("cannot export content for parameter '" + pm.getId()
                        + "' : some data exceed the maximum size of an excel cell");
            }
            if (LOGGER.isTraceEnabled()) {
                LOGGER.trace("", wtf);
            }
            pSheet.removeRow(r);
            r = pSheet.createRow(rIdx);

            r.createCell(0).setCellValue(errorCellTooLargeMessage);

        }

        rIdx++;
        cIdx = 0;
    }
}

From source file:org.squashtest.tm.service.internal.batchexport.ExcelExporter.java

License:Open Source License

private void appendDatasets(ExportModel model) {

    List<DatasetModel> models = model.getDatasets();
    Sheet dsSheet = workbook.getSheet(DS_SHEET);

    Row r;// ww w . ja v a 2  s . c o m
    int rIdx = dsSheet.getLastRowNum() + 1;
    int cIdx = 0;

    for (DatasetModel dm : models) {
        r = dsSheet.createRow(rIdx);

        try {
            r.createCell(cIdx++).setCellValue(dm.getTcOwnerPath());
            r.createCell(cIdx++).setCellValue(dm.getOwnerId());
            r.createCell(cIdx++).setCellValue(dm.getId());
            r.createCell(cIdx++).setCellValue(dm.getName());
            r.createCell(cIdx++).setCellValue(dm.getParamOwnerPath());
            r.createCell(cIdx++).setCellValue(dm.getParamOwnerId());
            r.createCell(cIdx++).setCellValue(dm.getParamName());
            r.createCell(cIdx++).setCellValue(dm.getParamValue());
        } catch (IllegalArgumentException wtf) {
            if (LOGGER.isWarnEnabled()) {
                LOGGER.warn("cannot export content for dataset '" + dm.getId()
                        + "' : some data exceed the maximum size of an excel cell");
            }
            if (LOGGER.isTraceEnabled()) {
                LOGGER.trace("", wtf);
            }
            dsSheet.removeRow(r);
            r = dsSheet.createRow(rIdx);

            r.createCell(0).setCellValue(errorCellTooLargeMessage);

        }

        rIdx++;
        cIdx = 0;
    }

}

From source file:org.squashtest.tm.service.internal.batchexport.ExcelExporter.java

License:Open Source License

private void appendCoverage(ExportModel model) {
    List<CoverageModel> models = model.getCoverages();
    Sheet covSheet = workbook.getSheet(COV_SHEET);

    Row r;/*from w w w.j  ava 2  s  .  c om*/
    int rIdx = covSheet.getLastRowNum() + 1;
    int cIdx = 0;

    for (CoverageModel cm : models) {
        r = covSheet.createRow(rIdx);

        r.createCell(cIdx++).setCellValue(cm.getReqPath());
        r.createCell(cIdx++).setCellValue(cm.getReqVersion());
        r.createCell(cIdx++).setCellValue(cm.getTcPath());

        rIdx++;
        cIdx = 0;
    }

}

From source file:org.squashtest.tm.service.internal.batchexport.RequirementExcelExporter.java

License:Open Source License

private void appendCoverage(RequirementExportModel model) {
    List<CoverageModel> models = model.getCoverages();
    Sheet covSheet = workbook.getSheet(COV_SHEET);

    Row r;//from   ww w. ja  va  2 s.c o  m
    int rIdx = covSheet.getLastRowNum() + 1;
    int cIdx = 0;

    for (CoverageModel cm : models) {
        r = covSheet.createRow(rIdx);

        r.createCell(cIdx++).setCellValue(cm.getReqPath());
        r.createCell(cIdx++).setCellValue(cm.getRequirementVersionNumber());
        r.createCell(cIdx++).setCellValue(cm.getTcPath());

        rIdx++;
        cIdx = 0;
    }

}

From source file:org.squashtest.tm.service.internal.batchexport.RequirementExcelExporter.java

License:Open Source License

private void appendRequirementModel(RequirementExportModel model) {
    List<RequirementModel> models = model.getRequirementsModels();
    Sheet reqSheet = workbook.getSheet(REQUIREMENT_SHEET);
    int rowIndex = reqSheet.getLastRowNum() + 1;

    for (RequirementModel reqModel : models) {
        appendOneRequirement(reqSheet, rowIndex, reqModel);
        rowIndex++;//from  w w w  .j  av a  2 s  . com
    }

}

From source file:org.squashtest.tm.service.internal.batchimport.testcase.excel.ExcelWorkbookParser.java

License:Open Source License

@SuppressWarnings({ "rawtypes", "unchecked" })
private void processWorksheet(WorksheetDef<?> worksheetDef) {
    LOGGER.debug("Processing worksheet {}", worksheetDef.getWorksheetType());

    Sheet sheet = workbook.getSheet(worksheetDef.getSheetName());

    InstructionBuilder<?, ?> instructionBuilder = instructionBuilderFactoryByWorksheet
            .get(worksheetDef.getWorksheetType()).create((WorksheetDef) worksheetDef); // useless (WorksheetDef) cast
    // required for compiler not to whine

    for (int i = 1; i <= sheet.getLastRowNum(); i++) {
        LOGGER.trace("Creating instruction for row {}", i);
        Row row = sheet.getRow(i);/*from  w ww.  j  a  v a2  s  .  c  om*/
        if (!isEmpty(row)) {
            Instruction instruction = instructionBuilder.build(row);
            instructionsByWorksheet.get(worksheetDef.getWorksheetType()).add(instruction);
        }

    }
}