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;
}
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;
}
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;
}
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;
}
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);
}
}
}
}
Aggregations