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

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

Introduction

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

Prototype

short getLastCellNum();

Source Link

Document

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

Usage

From source file:org.primefaces.extensions.showcase.util.ExcelCustomExporter.java

License:Apache License

protected void addColumnValue(Row row, List<UIComponent> components, String type) {
    int cellIndex = row.getLastCellNum() == -1 ? 0 : row.getLastCellNum();
    Cell cell = row.createCell(cellIndex);
    StringBuilder builder = new StringBuilder();
    FacesContext context = FacesContext.getCurrentInstance();

    for (UIComponent component : components) {
        if (component.isRendered()) {
            String value = exportValue(context, component);

            if (value != null) {
                builder.append(value);//from   w w  w  . j  a v  a  2  s  .co  m
            }
        }
    }

    cell.setCellValue(new XSSFRichTextString(builder.toString()));

    if (type.equalsIgnoreCase("facet")) {
        // addColumnAlignments(components,facetStyle);
        cell.setCellStyle(facetStyle);
    } else {
        CellStyle cellStyle = this.cellStyle;
        for (UIComponent component : components) {
            cellStyle = addColumnAlignments(component, cellStyle);
            cell.setCellStyle(cellStyle);
        }
    }

}

From source file:org.projectforge.excel.ExportRow.java

License:Open Source License

public ExportRow(final ContentProvider contentProvider, final ExportSheet sheet, final Row poiRow,
        final int rowNum) {
    this.contentProvider = contentProvider;
    this.sheet = sheet;
    this.poiRow = poiRow;
    this.rowNum = rowNum;
    cellMap = new HashMap<Integer, ExportCell>();
    if (poiRow.getLastCellNum() > 0) {
        // poiRow does already exists.
        for (int i = poiRow.getFirstCellNum(); i < poiRow.getLastCellNum(); i++) {
            final Cell poiCell = poiRow.getCell(i);
            if (poiCell != null) {
                addPoiCell(i, poiCell);//w w w.ja  v a2 s.  co m
            }
        }
    }
}

From source file:org.projectforge.excel.ExportSheet.java

License:Open Source License

private static Row copyRow(Sheet worksheet, int rowNum) {
    Row sourceRow = worksheet.getRow(rowNum);

    //Save the text of any formula before they are altered by row shifting
    String[] formulasArray = new String[sourceRow.getLastCellNum()];
    for (int i = 0; i < sourceRow.getLastCellNum(); i++) {
        if (sourceRow.getCell(i) != null && sourceRow.getCell(i).getCellType() == Cell.CELL_TYPE_FORMULA)
            formulasArray[i] = sourceRow.getCell(i).getCellFormula();
    }/*  w ww  .j  a va 2s.  co m*/

    worksheet.shiftRows(rowNum, worksheet.getLastRowNum(), 1);
    Row newRow = sourceRow; //Now sourceRow is the empty line, so let's rename it
    sourceRow = worksheet.getRow(rowNum + 1); //Now the source row is at rowNum+1

    // Loop through source columns to add to new row
    for (int i = 0; i < sourceRow.getLastCellNum(); i++) {
        // Grab a copy of the old/new cell
        Cell oldCell = sourceRow.getCell(i);
        Cell newCell;

        // If the old cell is null jump to next cell
        if (oldCell == null) {
            continue;
        } else {
            newCell = newRow.createCell(i);
        }

        // Copy style from old cell and apply to new cell
        CellStyle newCellStyle = worksheet.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:
            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(formulasArray[i]);
            break;
        case Cell.CELL_TYPE_NUMERIC:
            newCell.setCellValue(oldCell.getNumericCellValue());
            break;
        case Cell.CELL_TYPE_STRING:
            newCell.setCellValue(oldCell.getRichStringCellValue());
            break;
        default:
            break;
        }
    }

    // If there are any merged regions in the source row, copy to new row
    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);
        }
    }
    return newRow;
}

From source file:org.projectforge.export.ExportRow.java

License:Open Source License

public ExportRow(ContentProvider contentProvider, ExportSheet sheet, Row poiRow, int rowNum) {
    this.contentProvider = contentProvider;
    this.sheet = sheet;
    this.poiRow = poiRow;
    this.rowNum = rowNum;
    cellMap = new HashMap<Integer, ExportCell>();
    if (poiRow.getLastCellNum() > 0) {
        // poiRow does already exists.
        for (int i = poiRow.getFirstCellNum(); i < poiRow.getLastCellNum(); i++) {
            Cell poiCell = poiRow.getCell(i);
            if (poiCell != null) {
                addPoiCell(i, poiCell);/*from   w w w  . ja v  a  2 s.co  m*/
            }
        }
    }
}

From source file:org.ramadda.data.docs.TabularOutputHandler.java

License:Apache License

/**
 * _more_/*from  ww w .  j  a v a 2 s  . c  o m*/
 *
 * @param request _more_
 * @param entry _more_
 * @param suffix _more_
 * @param inputStream _more_
 * @param visitInfo _more_
 * @param visitor _more_
 *
 * @throws Exception _more_
 */
private void visitXls(Request request, Entry entry, String suffix, InputStream inputStream,
        TextReader visitInfo, TabularVisitor visitor) throws Exception {
    //        System.err.println("visitXls: making workbook");
    Workbook wb = makeWorkbook(suffix, inputStream);
    //        System.err.println("visitXls:" + visitInfo.getSkip() + " max rows:" + visitInfo.getMaxRows()+ " #sheets:" + wb.getNumberOfSheets());
    int maxRows = visitInfo.getMaxRows();
    for (int sheetIdx = 0; sheetIdx < wb.getNumberOfSheets(); sheetIdx++) {
        if (!visitInfo.okToShowSheet(sheetIdx)) {
            continue;
        }
        Sheet sheet = wb.getSheetAt(sheetIdx);
        //            System.err.println("\tsheet:" + sheet.getSheetName() + " #rows:" + sheet.getLastRowNum());
        List<List<Object>> rows = new ArrayList<List<Object>>();
        int sheetSkip = visitInfo.getSkip();
        for (int rowIdx = sheet.getFirstRowNum(); (rows.size() < maxRows)
                && (rowIdx <= sheet.getLastRowNum()); rowIdx++) {
            if (sheetSkip-- > 0) {
                continue;
            }

            Row row = sheet.getRow(rowIdx);
            if (row == null) {
                continue;
            }
            List<Object> cols = new ArrayList<Object>();
            short firstCol = row.getFirstCellNum();
            for (short col = firstCol; (col < MAX_COLS) && (col < row.getLastCellNum()); col++) {
                Cell cell = row.getCell(col);
                if (cell == null) {
                    break;
                }
                Object value = null;
                int type = cell.getCellType();
                if (type == cell.CELL_TYPE_NUMERIC) {
                    value = new Double(cell.getNumericCellValue());
                } else if (type == cell.CELL_TYPE_BOOLEAN) {
                    value = new Boolean(cell.getBooleanCellValue());
                } else if (type == cell.CELL_TYPE_ERROR) {
                    value = "" + cell.getErrorCellValue();
                } else if (type == cell.CELL_TYPE_BLANK) {
                    value = "";
                } else if (type == cell.CELL_TYPE_FORMULA) {
                    value = cell.getCellFormula();
                } else {
                    value = cell.getStringCellValue();
                }
                cols.add(value);
            }

            /**
             * ** TODO
             * org.ramadda.util.text.Row row = new Row(cols);
             *
             * if ( !visitInfo.rowOk(row)) {
             *   if (rows.size() == 0) {
             *       //todo: check for the header line
             *   } else {
             *       continue;
             *   }
             * }
             */
            rows.add(cols);
        }
        if (!visitor.visit(visitInfo, sheet.getSheetName(), rows)) {
            break;
        }
    }
}

From source file:org.ramadda.data.docs.TabularOutputHandler.java

License:Apache License

/**
 * _more_/*from   w w w.j av  a  2s  .  c  om*/
 *
 * @param args _more_
 *
 * @throws Exception _more_
 */
public static void main(String[] args) throws Exception {
    Workbook wb = makeWorkbook(IOUtil.getFileExtension(args[0]), new FileInputStream(args[0]));
    for (int sheetIdx = 0; sheetIdx < wb.getNumberOfSheets(); sheetIdx++) {
        Sheet sheet = wb.getSheetAt(sheetIdx);
        System.err.println(sheet.getSheetName());
        for (int rowIdx = sheet.getFirstRowNum(); rowIdx <= sheet.getLastRowNum(); rowIdx++) {
            Row row = sheet.getRow(rowIdx);
            if (row == null) {
                continue;
            }
            short firstCol = row.getFirstCellNum();
            int colCnt = 0;
            for (short col = firstCol; col < row.getLastCellNum(); col++) {
                Cell cell = row.getCell(col);
                if (cell == null) {
                    break;
                }
                Object value = null;
                int type = cell.getCellType();
                if (type == cell.CELL_TYPE_NUMERIC) {
                    value = new Double(cell.getNumericCellValue());
                } else if (type == cell.CELL_TYPE_BOOLEAN) {
                    value = new Boolean(cell.getBooleanCellValue());
                } else if (type == cell.CELL_TYPE_ERROR) {
                    value = "" + cell.getErrorCellValue();
                } else if (type == cell.CELL_TYPE_BLANK) {
                    value = "";
                } else if (type == cell.CELL_TYPE_FORMULA) {
                    value = cell.getCellFormula();
                } else {
                    value = cell.getStringCellValue();
                }
                if (colCnt++ > 0)
                    System.out.print(",");
                System.out.print(value);
            }
            System.out.println("");
        }
    }
}

From source file:org.ramadda.plugins.media.TabularOutputHandler.java

License:Open Source License

/**
 * _more_//from  w w w. j a v a 2s .  co  m
 *
 * @param request _more_
 * @param entry _more_
 * @param suffix _more_
 * @param inputStream _more_
 * @param visitInfo _more_
 * @param visitor _more_
 *
 * @throws Exception _more_
 */
private void visitXls(Request request, Entry entry, String suffix, InputStream inputStream, Visitor visitInfo,
        TabularVisitor visitor) throws Exception {
    //        System.err.println("visitXls: making workbook");
    Workbook wb = makeWorkbook(suffix, inputStream);
    //        System.err.println("visitXls:" + visitInfo.getSkip() + " max rows:" + visitInfo.getMaxRows()+ " #sheets:" + wb.getNumberOfSheets());
    int maxRows = visitInfo.getMaxRows();
    for (int sheetIdx = 0; sheetIdx < wb.getNumberOfSheets(); sheetIdx++) {
        if (!visitInfo.okToShowSheet(sheetIdx)) {
            continue;
        }
        Sheet sheet = wb.getSheetAt(sheetIdx);
        //            System.err.println("\tsheet:" + sheet.getSheetName() + " #rows:" + sheet.getLastRowNum());
        List<List<Object>> rows = new ArrayList<List<Object>>();
        int sheetSkip = visitInfo.getSkip();
        for (int rowIdx = sheet.getFirstRowNum(); (rows.size() < maxRows)
                && (rowIdx <= sheet.getLastRowNum()); rowIdx++) {
            if (sheetSkip-- > 0) {
                continue;
            }

            Row row = sheet.getRow(rowIdx);
            if (row == null) {
                continue;
            }
            List<Object> cols = new ArrayList<Object>();
            short firstCol = row.getFirstCellNum();
            for (short col = firstCol; (col < MAX_COLS) && (col < row.getLastCellNum()); col++) {
                Cell cell = row.getCell(col);
                if (cell == null) {
                    break;
                }
                Object value = null;
                int type = cell.getCellType();
                if (type == cell.CELL_TYPE_NUMERIC) {
                    value = new Double(cell.getNumericCellValue());
                } else if (type == cell.CELL_TYPE_BOOLEAN) {
                    value = new Boolean(cell.getBooleanCellValue());
                } else if (type == cell.CELL_TYPE_ERROR) {
                    value = "" + cell.getErrorCellValue();
                } else if (type == cell.CELL_TYPE_BLANK) {
                    value = "";
                } else if (type == cell.CELL_TYPE_FORMULA) {
                    value = cell.getCellFormula();
                } else {
                    value = cell.getStringCellValue();
                }
                cols.add(value);
            }

            /**
             * ** TODO
             * org.ramadda.util.text.Row row = new Row(cols);
             *
             * if ( !visitInfo.rowOk(row)) {
             *   if (rows.size() == 0) {
             *       //todo: check for the header line
             *   } else {
             *       continue;
             *   }
             * }
             */
            rows.add(cols);
        }
        if (!visitor.visit(visitInfo, sheet.getSheetName(), rows)) {
            break;
        }
    }
}

From source file:org.sakaiproject.gradebook.gwt.server.ImportExportUtilityImpl.java

License:Educational Community License

private void exportGradebookXLS(String title, OutputStream outStream, Gradebook2ComponentService service,
        String gradebookUid, final boolean includeStructure, final boolean includeComments,
        List<String> sectionUidList, boolean isXSSF) throws FatalException {

    final ImportExportDataFile file = exportGradebook(service, gradebookUid, includeStructure, includeComments,
            sectionUidList);//from  w  w  w  . j  av  a2  s . co m

    Map<StructureRow, String[]> structureColumnsMap = new HashMap<StructureRow, String[]>();
    ImportExportInformation ieInfo = new ImportExportInformation();

    int structureStop = 0;

    structureStop = readDataForStructureInformation(file, buildRowIndicatorMap(), structureColumnsMap);
    if (structureStop != -1)
        readInHeaderRow(file, ieInfo, structureStop);

    int studentId = -1;
    if (ieInfo.getHeaders() != null)
        for (int i = 0; i < ieInfo.getHeaders().length; ++i) {
            if (ieInfo.getHeaders()[i] != null) {
                String thisHeaderName = ieInfo.getHeaders()[i].getValue();
                for (int j = 0; j < idColumns.length; j++) {
                    String idColumn = idColumns[j];
                    if (idColumn != null && idColumn.equalsIgnoreCase(thisHeaderName)) {
                        studentId = i;
                        break;
                    }
                }
            }
            if (studentId != -1)
                break;
        }

    org.apache.poi.ss.usermodel.Workbook wb = isXSSF ? new XSSFWorkbook() : new HSSFWorkbook();

    CreationHelper helper = wb.getCreationHelper();
    // GRBK-1086 
    org.apache.poi.ss.usermodel.Sheet s = wb.createSheet(i18n.getString("exportSheetTitle"));

    file.startReading();
    String[] curRow = null;
    int row = 0;

    Row r = null;
    while ((curRow = file.readNext()) != null) {
        r = s.createRow(row);

        for (int i = 0; i < curRow.length; i++) {
            org.apache.poi.ss.usermodel.Cell cl = r.createCell(i);
            //GRBK-840 If the cell is numeric, we should make it numeric...
            // GRBK-979 .... unless it is the student id
            if (NumberUtils.isNumber(curRow[i]) && i != studentId) {
                cl.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
                cl.setCellValue(Double.valueOf(curRow[i]));
            } else {
                cl.setCellType(HSSFCell.CELL_TYPE_STRING);
                cl.setCellValue(helper.createRichTextString(curRow[i]));
            }
        }

        row++;
    }

    // Run autosize on last row's columns
    if (r != null) {
        for (int i = 0; i <= r.getLastCellNum(); i++) {
            s.autoSizeColumn((short) i);
        }
    }
    writeXLSResponse(wb, outStream);

}

From source file:org.sakaiproject.gradebook.gwt.server.ImportExportUtilityImpl.java

License:Educational Community License

private int getNumberOfColumnsFromSheet(org.apache.poi.ss.usermodel.Sheet cur) {
    int numCols = 0;
    Iterator<Row> rowIter = cur.rowIterator();
    while (rowIter.hasNext()) {
        Row curRow = rowIter.next();

        if (curRow.getLastCellNum() > numCols) {
            numCols = curRow.getLastCellNum();
        }/*w ww .j a va  2  s .  c  om*/
    }
    return numCols;
}

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

License:Apache License

private void addCellsToList(Row row) {
    for (int i = 0; i < row.getLastCellNum(); i++) {
        cellList.add(new CellWrapper(row.getCell(i), this));
    }//from ww  w .  j  av a 2s .c om
}