Example usage for org.apache.poi.xssf.usermodel XSSFRow getLastCellNum

List of usage examples for org.apache.poi.xssf.usermodel XSSFRow getLastCellNum

Introduction

In this page you can find the example usage for org.apache.poi.xssf.usermodel XSSFRow getLastCellNum.

Prototype

@Override
public short getLastCellNum() 

Source Link

Document

Gets the index of the last cell contained in this row PLUS ONE.

Usage

From source file:org.xframium.page.keyWord.provider.ExcelKeyWordProvider.java

License:Open Source License

private void readElements(InputStream inputStream) {
    List<MatrixTest> testList = new ArrayList<MatrixTest>(10);
    XSSFWorkbook workbook = null;//from   w  w w .  j a v  a 2 s  . c  o m

    try {
        workbook = new XSSFWorkbook(inputStream);

        XSSFSheet sheet = workbook.getSheet("Model");
        //
        // Extract the Tests
        //
        for (int i = 1; i <= sheet.getLastRowNum(); i++) {
            XSSFRow currentRow = sheet.getRow(i);

            String pageName = getCellValue(currentRow.getCell(0));

            if (pageName.toLowerCase().equals("name"))
                continue;
            String className = getCellValue(currentRow.getCell(1));

            try {
                Class useClass = KeyWordPage.class;
                if (className != null && !className.isEmpty())
                    useClass = (Class<Page>) Class.forName(className);

                if (log.isDebugEnabled())
                    log.debug("Creating page as " + useClass.getSimpleName() + " for " + pageName);

                KeyWordDriver.instance().addPage(pageName, useClass);
            } catch (Exception e) {
                log.error("Error creating instance of [" + className + "]");
            }
        }

        sheet = workbook.getSheet("Tests");

        //
        // Extract the Tests
        //
        for (int i = 1; i <= sheet.getLastRowNum(); i++) {
            XSSFRow currentRow = sheet.getRow(i);

            List<String> testDefinition = new ArrayList<String>(10);
            for (int j = 0; j < currentRow.getLastCellNum(); j++)
                testDefinition.add(getCellValue(currentRow.getCell(j)));

            MatrixTest currentTest = new MatrixTest(testDefinition.toArray(new String[0]));
            if (currentTest.getName() != null && !currentTest.getName().isEmpty() && currentTest.isActive())
                testList.add(currentTest);
        }

        for (MatrixTest currentTest : testList) {
            List<String[]> stepList = new ArrayList<String[]>(20);
            sheet = workbook.getSheet(currentTest.getName());
            if (sheet != null) {
                for (int i = 1; i <= sheet.getLastRowNum(); i++) {
                    XSSFRow currentRow = sheet.getRow(i);

                    List<String> stepDefinition = new ArrayList<String>(10);
                    for (int j = 0; j < currentRow.getLastCellNum(); j++)
                        stepDefinition.add(getCellValue(currentRow.getCell(j)));

                    stepList.add(stepDefinition.toArray(new String[0]));
                }
            }

            currentTest.setStepDefinition((String[][]) stepList.toArray(new String[0][0]));
        }

        for (MatrixTest currentTest : testList) {
            if (currentTest.getType().equals("function"))
                KeyWordDriver.instance().addFunction(currentTest.createTest());
            else
                KeyWordDriver.instance().addTest(currentTest.createTest());
        }
    } catch (Exception e) {
        log.fatal("Error reading Excel Element File", e);
    } finally {
        try {
            workbook.close();
        } catch (Exception e) {
        }
    }
}

From source file:se.minstrel.tools.xssfbuilder.impl.SheetBuilderImpl.java

License:Open Source License

@Override
public SheetBuilder autoWidth(boolean evaluateFormulas) {
    if (evaluateFormulas) {
        support.getFormulaEvaluator().clearAllCachedResultValues();
        support.getFormulaEvaluator().evaluateAll();
    }/*w  w  w  .java  2  s.  c  o  m*/
    int lastCol = 0;
    for (int r = 0; r <= sheet.getLastRowNum(); r++) {
        XSSFRow row = sheet.getRow(r);
        if (row != null) {
            lastCol = lastCol < row.getLastCellNum() ? row.getLastCellNum() : lastCol;
        }
    }
    for (int c = 0; c <= lastCol; c++) {
        col(c).autoWidth(false);
    }
    return this;
}

From source file:steffen.haertlein.file.FileObject.java

License:Apache License

private void readExcelDocument() {
    try {//from w  w w  .java 2s . co m
        FileInputStream fs = new FileInputStream(f);
        XSSFWorkbook wb = new XSSFWorkbook(fs);
        XSSFSheet sh;
        String text = "";
        for (int i = 0; i < wb.getNumberOfSheets(); i++) {
            sh = wb.getSheetAt(i);
            for (int j = sh.getFirstRowNum(); j <= sh.getLastRowNum(); j++) {
                XSSFRow currRow = sh.getRow(j);
                if (currRow == null || currRow.getFirstCellNum() == -1) {
                    continue;
                } else {
                    for (int k = currRow.getFirstCellNum(); k < currRow.getLastCellNum(); k++) {
                        if (currRow.getCell(k, Row.RETURN_BLANK_AS_NULL) == null) {
                            continue;
                        } else {
                            text += currRow.getCell(k) + "; ";
                        }
                    }
                    text += System.lineSeparator();
                }
            }
        }
        fs.close();
        wb.close();
        String[] xlsxLines = text.split(System.lineSeparator());
        for (String line : xlsxLines) {
            lines.add(line);
        }
    } catch (IOException e) {
        JOptionPane.showMessageDialog(null, "Fehler in readExcelDocument", "Fehler", JOptionPane.ERROR_MESSAGE);
        e.printStackTrace();
    }
}

From source file:sv.com.mined.sieni.controller.GestionNotasController.java

public static void copyRow(XSSFSheet worksheetSource, XSSFSheet worksheetDestination, int sourceRowNum,
        int destinationRowNum) {
    // Get the source / new row
    XSSFRow origen = worksheetSource.getRow(sourceRowNum);
    XSSFRow destino = worksheetDestination.createRow(destinationRowNum);

    // Loop through source columns to add to new row
    for (int i = 0; i < origen.getLastCellNum(); i++) {
        // Grab a copy of the old/new cell
        XSSFCell oldCell = origen.getCell(i);
        XSSFCell newCell = destino.createCell(i);
        // If the old cell is null jump to next cell
        if (oldCell == null) {
            newCell = null;// w w w.j  a v  a2s.co m
            continue;
        }

        //Ajustar tamaos columnas
        worksheetDestination.setColumnWidth(i, worksheetSource.getColumnWidth(i));

        // Copy style from old cell and apply to new cell
        XSSFCellStyle newCellStyle = newCell.getSheet().getWorkbook().createCellStyle();
        newCellStyle.cloneStyleFrom(oldCell.getCellStyle());
        newCell.setCellStyle(newCellStyle);

        // If there is a cell comment, copy
        if (oldCell.getCellComment() != null) {
            newCell.setCellComment(oldCell.getCellComment());
        }

        // If there is a cell hyperlink, copy
        if (oldCell.getHyperlink() != null) {
            newCell.setHyperlink(oldCell.getHyperlink());
        }

        // Set the cell data type
        newCell.setCellType(oldCell.getCellType());
        // Set the cell data value
        switch (oldCell.getCellType()) {
        case Cell.CELL_TYPE_BLANK:
            newCell.setCellValue(oldCell.getStringCellValue());
            break;
        case Cell.CELL_TYPE_BOOLEAN:
            newCell.setCellValue(oldCell.getBooleanCellValue());
            break;
        case Cell.CELL_TYPE_ERROR:
            newCell.setCellErrorValue(oldCell.getErrorCellValue());
            break;
        case Cell.CELL_TYPE_FORMULA:
            newCell.setCellFormula(oldCell.getCellFormula());
            break;
        case Cell.CELL_TYPE_NUMERIC:
            newCell.setCellValue(oldCell.getNumericCellValue());
            break;
        case Cell.CELL_TYPE_STRING:
            newCell.setCellValue(oldCell.getRichStringCellValue());
            break;
        }

    }

}

From source file:tan.jam.jsf.Shifting.java

public static void InsertRow(XSSFWorkbook workbook, XSSFSheet worksheet, int sourceRowNum,
        int destinationRowNum) {

    worksheet.shiftRows(destinationRowNum, worksheet.getLastRowNum(), 1);
    XSSFRow newRow = worksheet.getRow(destinationRowNum);
    XSSFRow sourceRow = worksheet.getRow(sourceRowNum);

    if (newRow != null) {
        worksheet.shiftRows(destinationRowNum, worksheet.getLastRowNum(), 1);
    } else {//from  ww w  .j  a v a2s  .co m
        newRow = worksheet.createRow(destinationRowNum);
    }

    for (int i = 0; i < sourceRow.getLastCellNum(); i++) {
        // Grab a copy of the old/new cell
        XSSFCell oldCell = sourceRow.getCell(i);
        XSSFCell newCell = newRow.createCell(i);

        if (oldCell == null) {
            newCell = null;
            continue;
        }

        XSSFCellStyle newCellStyle = workbook.createCellStyle();
        newCellStyle.cloneStyleFrom(oldCell.getCellStyle());
        ;
        newCell.setCellStyle(newCellStyle);

        if (oldCell.getCellComment() != null) {
            newCell.setCellComment(oldCell.getCellComment());
        }

        if (oldCell.getHyperlink() != null) {
            newCell.setHyperlink(oldCell.getHyperlink());
        }

        newCell.setCellType(oldCell.getCellType());

        switch (oldCell.getCellType()) {
        case org.apache.poi.ss.usermodel.Cell.CELL_TYPE_BLANK:
            newCell.setCellValue(oldCell.getStringCellValue());
            break;
        case org.apache.poi.ss.usermodel.Cell.CELL_TYPE_BOOLEAN:
            newCell.setCellValue(oldCell.getBooleanCellValue());
            break;
        case org.apache.poi.ss.usermodel.Cell.CELL_TYPE_ERROR:
            newCell.setCellErrorValue(oldCell.getErrorCellValue());
            break;
        case org.apache.poi.ss.usermodel.Cell.CELL_TYPE_FORMULA:
            newCell.setCellFormula("+" + "F" + destinationRowNum + "*G" + destinationRowNum);
            break;
        case org.apache.poi.ss.usermodel.Cell.CELL_TYPE_NUMERIC:
            //newCell.setCellValue(oldCell.getNumericCellValue());
            break;
        case org.apache.poi.ss.usermodel.Cell.CELL_TYPE_STRING:
            newCell.setCellValue("");
            break;
        }
    }

    for (int i = 0; i < worksheet.getNumMergedRegions(); i++) {
        CellRangeAddress cellRangeAddress = worksheet.getMergedRegion(i);
        if (cellRangeAddress.getFirstRow() == sourceRow.getRowNum()) {
            CellRangeAddress newCellRangeAddress = new CellRangeAddress(newRow.getRowNum(),
                    (newRow.getRowNum() + (cellRangeAddress.getLastRow() - cellRangeAddress.getFirstRow())),
                    cellRangeAddress.getFirstColumn(), cellRangeAddress.getLastColumn());
            worksheet.addMergedRegion(newCellRangeAddress);
        }
    }
    int inc = destinationRowNum + 1;
    worksheet.getRow(destinationRowNum).getCell(7).setCellFormula("+F" + inc + "*G" + inc);
}

From source file:tan.jam.jsf.Shifting.java

private static void CopyData(XSSFWorkbook workbook, XSSFSheet worksheet, int sourceRowNum,
        int destinationRowNum, int Mov) {
    XSSFRow newRow = worksheet.getRow(destinationRowNum);
    XSSFRow sourceRow = worksheet.getRow(sourceRowNum);

    for (int i = sourceRow.getLastCellNum(); i > 8 + Mov; i--) {

        int d = i - 1;
        XSSFCell oldCell = sourceRow.getCell(d);
        XSSFCell newCell = newRow.createCell(i);

        if (oldCell == null) {
            newCell = null;//from  www  . j  a v  a  2 s  .  co m
            continue;
        }

        XSSFCellStyle newCellStyle = workbook.createCellStyle();
        newCellStyle.cloneStyleFrom(oldCell.getCellStyle());
        ;
        newCell.setCellStyle(newCellStyle);

        if (oldCell.getCellComment() != null) {
            newCell.setCellComment(oldCell.getCellComment());
        }

        if (oldCell.getHyperlink() != null) {
            newCell.setHyperlink(oldCell.getHyperlink());
        }

        newCell.setCellType(oldCell.getCellType());

        switch (oldCell.getCellType()) {
        case org.apache.poi.ss.usermodel.Cell.CELL_TYPE_BLANK:
            newCell.setCellValue(oldCell.getStringCellValue());
            break;
        case org.apache.poi.ss.usermodel.Cell.CELL_TYPE_BOOLEAN:
            newCell.setCellValue(oldCell.getBooleanCellValue());
            break;
        case org.apache.poi.ss.usermodel.Cell.CELL_TYPE_ERROR:
            newCell.setCellErrorValue(oldCell.getErrorCellValue());
            break;
        case org.apache.poi.ss.usermodel.Cell.CELL_TYPE_FORMULA:
            newCell.setCellFormula(oldCell.getCellFormula());
            break;
        case org.apache.poi.ss.usermodel.Cell.CELL_TYPE_NUMERIC:
            newCell.setCellValue(oldCell.getNumericCellValue());
            break;
        case org.apache.poi.ss.usermodel.Cell.CELL_TYPE_STRING:
            newCell.setCellValue(oldCell.getRichStringCellValue());
            break;
        }
    }
}