Search in sources :

Example 21 with Row

use of org.apache.poi.ss.usermodel.Row in project poi by apache.

the class SheetBuilder method build.

/**
     * Builds sheet from parent workbook and 2D array with cell
     * values. Creates rows anyway (even if row contains only null
     * cells), creates cells if either corresponding array value is not
     * null or createEmptyCells property is true.
     * The conversion is performed in the following way:
     * <p/>
     * <ul>
     * <li>Numbers become numeric cells.</li>
     * <li><code>java.util.Date</code> or <code>java.util.Calendar</code>
     * instances become date cells.</li>
     * <li>String with leading '=' char become formulas (leading '='
     * will be truncated).</li>
     * <li>Other objects become strings via <code>Object.toString()</code>
     * method call.</li>
     * </ul>
     *
     * @return newly created sheet
     */
public Sheet build() {
    Sheet sheet = (sheetName == null) ? workbook.createSheet() : workbook.createSheet(sheetName);
    Row currentRow = null;
    Cell currentCell = null;
    for (int rowIndex = 0; rowIndex < cells.length; ++rowIndex) {
        Object[] rowArray = cells[rowIndex];
        currentRow = sheet.createRow(rowIndex);
        for (int cellIndex = 0; cellIndex < rowArray.length; ++cellIndex) {
            Object cellValue = rowArray[cellIndex];
            if (cellValue != null || shouldCreateEmptyCells) {
                currentCell = currentRow.createCell(cellIndex);
                setCellValue(currentCell, cellValue);
            }
        }
    }
    return sheet;
}
Also used : Row(org.apache.poi.ss.usermodel.Row) Sheet(org.apache.poi.ss.usermodel.Sheet) Cell(org.apache.poi.ss.usermodel.Cell)

Example 22 with Row

use of org.apache.poi.ss.usermodel.Row in project poi by apache.

the class SheetUtil method getColumnWidth.

/**
     * Compute width of a column based on a subset of the rows and return the result
     *
     * @param sheet the sheet to calculate
     * @param column    0-based index of the column
     * @param useMergedCells    whether to use merged cells
     * @param firstRow  0-based index of the first row to consider (inclusive)
     * @param lastRow   0-based index of the last row to consider (inclusive)
     * @return  the width in pixels or -1 if cell is empty
     */
public static double getColumnWidth(Sheet sheet, int column, boolean useMergedCells, int firstRow, int lastRow) {
    DataFormatter formatter = new DataFormatter();
    int defaultCharWidth = getDefaultCharWidth(sheet.getWorkbook());
    double width = -1;
    for (int rowIdx = firstRow; rowIdx <= lastRow; ++rowIdx) {
        Row row = sheet.getRow(rowIdx);
        if (row != null) {
            double cellWidth = getColumnWidthForRow(row, column, defaultCharWidth, formatter, useMergedCells);
            width = Math.max(width, cellWidth);
        }
    }
    return width;
}
Also used : Row(org.apache.poi.ss.usermodel.Row) DataFormatter(org.apache.poi.ss.usermodel.DataFormatter)

Example 23 with Row

use of org.apache.poi.ss.usermodel.Row in project poi by apache.

the class SXSSFPicture method getRowHeightInPixels.

private float getRowHeightInPixels(int rowIndex) {
    // THE FOLLOWING THREE LINES ARE THE MAIN CHANGE compared to the non-streaming version: use the SXSSF sheet,
    // not the XSSF sheet (which never contais rows when using SXSSF)
    XSSFSheet xssfSheet = getSheet();
    SXSSFSheet sheet = _wb.getSXSSFSheet(xssfSheet);
    Row row = sheet.getRow(rowIndex);
    float height = row != null ? row.getHeightInPoints() : sheet.getDefaultRowHeightInPoints();
    return height * XSSFShape.PIXEL_DPI / XSSFShape.POINT_DPI;
}
Also used : XSSFSheet(org.apache.poi.xssf.usermodel.XSSFSheet) Row(org.apache.poi.ss.usermodel.Row)

Example 24 with Row

use of org.apache.poi.ss.usermodel.Row in project poi by apache.

the class ExcelAntWorkbookUtil method getCell.

/**
     * Returns a cell reference based on a String in standard Excel format
     * (SheetName!CellId).  This method will create a new cell if the
     * requested cell isn't initialized yet.
     *
     * @param cellName
     * @return
     */
private Cell getCell(String cellName) {
    CellReference cellRef = new CellReference(cellName);
    String sheetName = cellRef.getSheetName();
    Sheet sheet = workbook.getSheet(sheetName);
    if (sheet == null) {
        throw new BuildException("Sheet not found: " + sheetName);
    }
    int rowIdx = cellRef.getRow();
    int colIdx = cellRef.getCol();
    Row row = sheet.getRow(rowIdx);
    if (row == null) {
        row = sheet.createRow(rowIdx);
    }
    Cell cell = row.getCell(colIdx);
    if (cell == null) {
        cell = row.createCell(colIdx);
    }
    return cell;
}
Also used : BuildException(org.apache.tools.ant.BuildException) Row(org.apache.poi.ss.usermodel.Row) CellReference(org.apache.poi.ss.util.CellReference) Sheet(org.apache.poi.ss.usermodel.Sheet) Cell(org.apache.poi.ss.usermodel.Cell)

Example 25 with Row

use of org.apache.poi.ss.usermodel.Row in project poi by apache.

the class UpdateEmbeddedDoc method checkUpdatedDoc.

/**
     * Called to test whether or not the embedded workbook was correctly
     * updated. This method simply recovers the first cell from the first row
     * of the first workbook and tests the value it contains.
     * <p/>
     * Note that execution will not continue up to the assertion as the
     * embedded workbook is now corrupted and causes an IllegalArgumentException
     * with the following message
     * <p/>
     * <em>java.lang.IllegalArgumentException: Your InputStream was neither an
     * OLE2 stream, nor an OOXML stream</em>
     * <p/>
     * to be thrown when the WorkbookFactory.createWorkbook(InputStream) method
     * is executed.
     *
     * @throws org.apache.poi.openxml4j.exceptions.OpenXML4JException
     *                             Rather
     *                             than use the specific classes (HSSF/XSSF) to handle the embedded
     *                             workbook this method uses those defeined in the SS stream. As
     *                             a result, it might be the case that a SpreadsheetML file is
     *                             opened for processing, throwing this exception if that file is
     *                             invalid.
     * @throws java.io.IOException Thrown if a problem occurs in the underlying
     *                             file system.
     */
public void checkUpdatedDoc() throws OpenXML4JException, IOException {
    for (PackagePart pPart : this.doc.getAllEmbedds()) {
        String ext = pPart.getPartName().getExtension();
        if (BINARY_EXTENSION.equals(ext) || OPENXML_EXTENSION.equals(ext)) {
            InputStream is = pPart.getInputStream();
            Workbook workbook = null;
            try {
                workbook = WorkbookFactory.create(is);
                Sheet sheet = workbook.getSheetAt(SHEET_NUM);
                Row row = sheet.getRow(ROW_NUM);
                Cell cell = row.getCell(CELL_NUM);
                assertEquals(cell.getNumericCellValue(), NEW_VALUE, 0.0001);
            } finally {
                IOUtils.closeQuietly(workbook);
                IOUtils.closeQuietly(is);
            }
        }
    }
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Row(org.apache.poi.ss.usermodel.Row) PackagePart(org.apache.poi.openxml4j.opc.PackagePart) Sheet(org.apache.poi.ss.usermodel.Sheet) Cell(org.apache.poi.ss.usermodel.Cell) Workbook(org.apache.poi.ss.usermodel.Workbook)

Aggregations

Row (org.apache.poi.ss.usermodel.Row)289 Cell (org.apache.poi.ss.usermodel.Cell)218 Sheet (org.apache.poi.ss.usermodel.Sheet)174 Workbook (org.apache.poi.ss.usermodel.Workbook)122 Test (org.junit.Test)112 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)55 XSSFWorkbook (org.apache.poi.xssf.usermodel.XSSFWorkbook)39 CellRangeAddress (org.apache.poi.ss.util.CellRangeAddress)33 CellStyle (org.apache.poi.ss.usermodel.CellStyle)27 CellReference (org.apache.poi.ss.util.CellReference)22 FileOutputStream (java.io.FileOutputStream)19 ArrayList (java.util.ArrayList)19 XSSFColor (org.apache.poi.xssf.usermodel.XSSFColor)17 XSSFFont (org.apache.poi.xssf.usermodel.XSSFFont)17 HashMap (java.util.HashMap)16 RichTextString (org.apache.poi.ss.usermodel.RichTextString)16 XSSFCellStyle (org.apache.poi.xssf.usermodel.XSSFCellStyle)16 IOException (java.io.IOException)14 List (java.util.List)13 FormulaEvaluator (org.apache.poi.ss.usermodel.FormulaEvaluator)13