import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.formula.eval.ErrorEval;
import org.apache.poi.ss.usermodel.HeaderFooter;
+import org.apache.poi.ss.usermodel.Row.MissingCellPolicy;
/**
* A text extractor for Excel files.
extractor.setIncludeHeadersFooters(cmdArgs.shouldIncludeHeadersFooters());
System.out.println(extractor.getText());
extractor.close();
+ wb.close();
}
/**
* Should sheet names be included? Default is true
// We don't care about the difference between
// null (missing) and blank cells
- _wb.setMissingCellPolicy(HSSFRow.RETURN_BLANK_AS_NULL);
+ _wb.setMissingCellPolicy(MissingCellPolicy.RETURN_BLANK_AS_NULL);
// Process each sheet in turn
for(int i=0;i<_wb.getNumberOfSheets();i++) {
* @throws IllegalArgumentException if columnIndex < 0 or greater than 255,
* the maximum number of columns supported by the Excel binary format (.xls)
*/
+ @Override
public HSSFCell createCell(int column)
{
- return this.createCell(column,HSSFCell.CELL_TYPE_BLANK);
+ return this.createCell(column,Cell.CELL_TYPE_BLANK);
}
/**
* @throws IllegalArgumentException if columnIndex < 0 or greater than 255,
* the maximum number of columns supported by the Excel binary format (.xls)
*/
+ @Override
public HSSFCell createCell(int columnIndex, int type)
{
short shortCellNum = (short)columnIndex;
* remove the HSSFCell from this row.
* @param cell to remove
*/
+ @Override
public void removeCell(Cell cell) {
if(cell == null) {
throw new IllegalArgumentException("cell must not be null");
* @param rowIndex the row number (0-based)
* @throws IndexOutOfBoundsException if the row number is not within the range 0-65535.
*/
+ @Override
public void setRowNum(int rowIndex) {
int maxrow = SpreadsheetVersion.EXCEL97.getLastRowIndex();
if ((rowIndex < 0) || (rowIndex > maxrow)) {
* get row number this row represents
* @return the row number (0 based)
*/
+ @Override
public int getRowNum()
{
return rowNum;
*
* @return the HSSFSheet that owns this row
*/
+ @Override
public HSSFSheet getSheet()
{
return sheet;
* put it into more groups (outlines), reduced as
* you take it out of them.
*/
+ @Override
public int getOutlineLevel() {
return row.getOutlineLevel();
}
* @param cellnum 0 based column number
* @return HSSFCell representing that column or null if undefined.
*/
+ @Override
public HSSFCell getCell(int cellnum) {
return getCell(cellnum, book.getMissingCellPolicy());
}
* @param policy Policy on blank / missing cells
* @return representing that column or null if undefined + policy allows.
*/
+ @Override
public HSSFCell getCell(int cellnum, MissingCellPolicy policy) {
HSSFCell cell = retrieveCell(cellnum);
- if(policy == RETURN_NULL_AND_BLANK) {
- return cell;
- }
- if(policy == RETURN_BLANK_AS_NULL) {
- if(cell == null) return cell;
- if(cell.getCellType() == HSSFCell.CELL_TYPE_BLANK) {
- return null;
- }
- return cell;
+ switch (policy) {
+ case RETURN_NULL_AND_BLANK:
+ return cell;
+ case RETURN_BLANK_AS_NULL:
+ boolean isBlank = (cell != null && cell.getCellType() == Cell.CELL_TYPE_BLANK);
+ return (isBlank) ? null : cell;
+ case CREATE_NULL_AS_BLANK:
+ return (cell == null) ? createCell(cellnum, Cell.CELL_TYPE_BLANK) : cell;
+ default:
+ throw new IllegalArgumentException("Illegal policy " + policy + " (" + policy.id + ")");
}
- if(policy == CREATE_NULL_AS_BLANK) {
- if(cell == null) {
- return createCell(cellnum, HSSFCell.CELL_TYPE_BLANK);
- }
- return cell;
- }
- throw new IllegalArgumentException("Illegal policy " + policy + " (" + policy.id + ")");
}
/**
* get the number of the first cell contained in this row.
* @return short representing the first logical cell in the row, or -1 if the row does not contain any cells.
*/
+ @Override
public short getFirstCellNum() {
if (row.isEmpty()) {
return -1;
* @return short representing the last logical cell in the row <b>PLUS ONE</b>, or -1 if the
* row does not contain any cells.
*/
+ @Override
public short getLastCellNum() {
if (row.isEmpty()) {
return -1;
* @return int representing the number of defined cells in the row.
*/
+ @Override
public int getPhysicalNumberOfCells()
{
int count=0;
* @param height rowheight or -1 for undefined (use sheet default)
*/
+ @Override
public void setHeight(short height)
{
if(height == -1){
* set whether or not to display this row with 0 height
* @param zHeight height is zero or not.
*/
+ @Override
public void setZeroHeight(boolean zHeight) {
row.setZeroHeight(zHeight);
}
* get whether or not to display this row with 0 height
* @return - zHeight height is zero or not.
*/
+ @Override
public boolean getZeroHeight() {
return row.getZeroHeight();
}
* @param height row height in points, <code>-1</code> means to use the default height
*/
+ @Override
public void setHeightInPoints(float height)
{
if(height == -1){
* @return rowheight or 0xff for undefined (use sheet default)
*/
+ @Override
public short getHeight()
{
short height = row.getHeight();
* @return rowheight or 0xff for undefined (use sheet default)
*/
+ @Override
public float getHeightInPoints()
{
return ((float)getHeight() / 20);
* do have whole-row styles. For those that do, you
* can get the formatting from {@link #getRowStyle()}
*/
+ @Override
public boolean isFormatted() {
return row.getFormatted();
}
* have one of these, so will return null. Call
* {@link #isFormatted()} to check first.
*/
+ @Override
public HSSFCellStyle getRowStyle() {
if(!isFormatted()) { return null; }
short styleIndex = row.getXFIndex();
/**
* Applies a whole-row cell styling to the row.
*/
+ @Override
public void setRowStyle(CellStyle style) {
setRowStyle((HSSFCellStyle)style);
}
* As this only ever works on physically defined cells,
* the {@link org.apache.poi.ss.usermodel.Row.MissingCellPolicy} has no effect.
*/
+ @Override
public Iterator<Cell> cellIterator()
{
return new CellIterator();
* Alias for {@link #cellIterator} to allow
* foreach loops
*/
+ @Override
public Iterator<Cell> iterator() {
return cellIterator();
}
findNext();
}
- public boolean hasNext() {
+ @Override
+ public boolean hasNext() {
return nextId<cells.length;
}
- public Cell next() {
+ @Override
+ public Cell next() {
if (!hasNext())
throw new NoSuchElementException("At last element");
HSSFCell cell=cells[nextId];
return cell;
}
- public void remove() {
+ @Override
+ public void remove() {
if (thisId == -1)
throw new IllegalStateException("remove() called before next()");
cells[thisId]=null;
* blank cells when fetching from a row.
* See {@link MissingCellPolicy}
*/
- private MissingCellPolicy missingCellPolicy = HSSFRow.RETURN_NULL_AND_BLANK;
+ private MissingCellPolicy missingCellPolicy = MissingCellPolicy.RETURN_NULL_AND_BLANK;
private static POILogger log = POILogFactory.getLogger(HSSFWorkbook.class);
*
* @return an iterator of the sheets.
*/
+ @Override
public Iterator<Sheet> sheetIterator() {
Iterator<Sheet> result = new SheetIterator<Sheet>();
return result;
* Alias for {@link #sheetIterator()} to allow
* foreach loops
*/
+ @Override
public Iterator<Sheet> iterator() {
return sheetIterator();
}
* Finds a font that matches the one with the supplied attributes
* @deprecated 3.15 beta 2. Use {@link #findFont(boolean, short, short, String, boolean, boolean, short, byte)} instead.
*/
+ @Deprecated
@Override
public HSSFFont findFont(short boldWeight, short color, short fontHeight,
String name, boolean italic, boolean strikeout,
/**
* Finds a font that matches the one with the supplied attributes
*/
+ @Override
public HSSFFont findFont(boolean bold, short color, short fontHeight,
String name, boolean italic, boolean strikeout,
short typeOffset, byte underline)
break;
}
- blipRecord.setRecordId((short) (EscherBitmapBlip.RECORD_ID_START + format));
+ blipRecord.setRecordId((short) (EscherBlipRecord.RECORD_ID_START + format));
switch (format)
{
case PICTURE_TYPE_EMF:
*
* @param column - the column number this cell represents
* @return Cell a high level representation of the created cell.
- * @throws IllegalArgumentException if columnIndex < 0 or greater than the maximum number of supported columns
+ * @throws IllegalArgumentException if columnIndex < 0 or greater than the maximum number of supported columns
* (255 for *.xls, 1048576 for *.xlsx)
*/
Cell createCell(int column);
* @param column - the column number this cell represents
* @param type - the cell's data type
* @return Cell a high level representation of the created cell.
- * @throws IllegalArgumentException if columnIndex < 0 or greate than a maximum number of supported columns
+ * @throws IllegalArgumentException if columnIndex < 0 or greater than a maximum number of supported columns
* (255 for *.xls, 1048576 for *.xlsx)
* @see Cell#CELL_TYPE_BLANK
* @see Cell#CELL_TYPE_BOOLEAN
* Set the row number of this row.
*
* @param rowNum the row number (0-based)
- * @throws IllegalArgumentException if rowNum < 0
+ * @throws IllegalArgumentException if rowNum < 0
*/
void setRowNum(int rowNum);
* Returns the cell at the given (0 based) index, with the specified {@link org.apache.poi.ss.usermodel.Row.MissingCellPolicy}
*
* @return the cell at the given (0 based) index
- * @throws IllegalArgumentException if cellnum < 0 or the specified MissingCellPolicy is invalid
- * @see Row#RETURN_NULL_AND_BLANK
- * @see Row#RETURN_BLANK_AS_NULL
- * @see Row#CREATE_NULL_AS_BLANK
+ * @throws IllegalArgumentException if cellnum < 0 or the specified MissingCellPolicy is invalid
*/
Cell getCell(int cellnum, MissingCellPolicy policy);
* Used to specify the different possible policies
* if for the case of null and blank cells
*/
- public static enum MissingCellPolicy {
- RETURN_NULL_AND_BLANK(),
- RETURN_BLANK_AS_NULL(),
- CREATE_NULL_AS_BLANK();
+ public enum MissingCellPolicy {
+ RETURN_NULL_AND_BLANK(1),
+ RETURN_BLANK_AS_NULL(2),
+ CREATE_NULL_AS_BLANK(3);
- private int NEXT_ID = 1;
+ /**
+ * @deprecated as of POI 3.15-beta2, scheduled for removal in 3.17 - the id has no function and will be removed
+ */
+ @Deprecated
public final int id;
- private MissingCellPolicy() {
- this.id = NEXT_ID++;
+ private MissingCellPolicy(int id) {
+ this.id = id;
}
}
- /** Missing cells are returned as null, Blank cells are returned as normal */
+
+ /**
+ * Missing cells are returned as null, Blank cells are returned as normal
+ *
+ * @deprecated as of POI 3.15-beta2, scheduled for removal in 3.17 - use the MissingCellPolicy enum
+ **/
+ @Deprecated
public static final MissingCellPolicy RETURN_NULL_AND_BLANK = MissingCellPolicy.RETURN_NULL_AND_BLANK;
- /** Missing cells and blank cells are returned as null */
+ /**
+ * Missing cells and blank cells are returned as null
+ *
+ * @deprecated as of POI 3.15-beta2, scheduled for removal in 3.17 - use the MissingCellPolicy enum
+ **/
+ @Deprecated
public static final MissingCellPolicy RETURN_BLANK_AS_NULL = MissingCellPolicy.RETURN_BLANK_AS_NULL;
- /** A new, blank cell is created for missing cells. Blank cells are returned as normal */
+ /**
+ * A new, blank cell is created for missing cells. Blank cells are returned as normal
+ *
+ * @deprecated as of POI 3.15-beta2, scheduled for removal in 3.17 - use the MissingCellPolicy enum
+ **/
+ @Deprecated
public static final MissingCellPolicy CREATE_NULL_AS_BLANK = MissingCellPolicy.CREATE_NULL_AS_BLANK;
/**
public boolean isRightToLeft();
/**
- * Set the width (in units of 1/256th of a character width)
+ * Set the width (in units of 1/256th of a character width)<p>
*
- * <p>
* The maximum column width for an individual cell is 255 characters.
* This value represents the number of characters that can be displayed
- * in a cell that is formatted with the standard font (first font in the workbook).
- * </p>
+ * in a cell that is formatted with the standard font (first font in the workbook).<p>
*
- * <p>
* Character width is defined as the maximum digit width
* of the numbers <code>0, 1, 2, ... 9</code> as rendered
- * using the default font (first font in the workbook).
- * <br/>
+ * using the default font (first font in the workbook).<p>
+ *
* Unless you are using a very special font, the default character is '0' (zero),
- * this is true for Arial (default font font in HSSF) and Calibri (default font in XSSF)
- * </p>
+ * this is true for Arial (default font font in HSSF) and Calibri (default font in XSSF)<p>
*
- * <p>
* Please note, that the width set by this method includes 4 pixels of margin padding (two on each side),
* plus 1 pixel padding for the gridlines (Section 3.3.1.12 of the OOXML spec).
- * This results is a slightly less value of visible characters than passed to this method (approx. 1/2 of a character).
- * </p>
- * <p>
+ * This results is a slightly less value of visible characters than passed to this method (approx. 1/2 of a character).<p>
+ *
* To compute the actual number of visible characters,
- * Excel uses the following formula (Section 3.3.1.12 of the OOXML spec):
- * </p>
+ * Excel uses the following formula (Section 3.3.1.12 of the OOXML spec):<p>
+ *
* <code>
* width = Truncate([{Number of Visible Characters} *
* {Maximum Digit Width} + {5 pixel padding}]/{Maximum Digit Width}*256)/256
* </code>
- * <p>Using the Calibri font as an example, the maximum digit width of 11 point font size is 7 pixels (at 96 dpi).
- * If you set a column width to be eight characters wide, e.g. <code>setColumnWidth(columnIndex, 8*256)</code>,
- * then the actual value of visible characters (the value shown in Excel) is derived from the following equation:
+ *
+ * Using the Calibri font as an example, the maximum digit width of 11 point font size is 7 pixels (at 96 dpi).
+ * If you set a column width to be eight characters wide, e.g. <code>setColumnWidth(columnIndex, 8*256)</code>,
+ * then the actual value of visible characters (the value shown in Excel) is derived from the following equation:
* <code>
- Truncate([numChars*7+5]/7*256)/256 = 8;
+ * Truncate([numChars*7+5]/7*256)/256 = 8;
* </code>
*
- * which gives <code>7.29</code>.
+ * which gives <code>7.29</code>.
*
* @param columnIndex - the column to set (0-based)
* @param width - the width in units of 1/256th of a character width
- * @throws IllegalArgumentException if width > 255*256 (the maximum column width in Excel is 255 characters)
+ * @throws IllegalArgumentException if width > 255*256 (the maximum column width in Excel is 255 characters)
*/
void setColumnWidth(int columnIndex, int width);
PrintSetup getPrintSetup();
/**
- * Gets the user model for the default document header.
- * <p/>
+ * Gets the user model for the default document header.<p>
+ *
* Note that XSSF offers more kinds of document headers than HSSF does
- * </p>
+ *
* @return the document header. Never <code>null</code>
*/
Header getHeader();
/**
- * Gets the user model for the default document footer.
- * <p/>
+ * Gets the user model for the default document footer.<p>
+ *
* Note that XSSF offers more kinds of document footers than HSSF does.
*
* @return the document footer. Never <code>null</code>
Footer getFooter();
/**
- * Sets a flag indicating whether this sheet is selected.
- *<p>
+ * Sets a flag indicating whether this sheet is selected.<p>
+ *
* Note: multiple sheets can be selected, but only one sheet can be active at one time.
- *</p>
+ *
* @param value <code>true</code> if this sheet is selected
* @see Workbook#setActiveSheet(int)
*/
/**
* Answer whether protection is enabled or disabled
*
- * @return true => protection enabled; false => protection disabled
+ * @return true => protection enabled; false => protection disabled
*/
boolean getProtect();
/**
* Answer whether scenario protection is enabled or disabled
*
- * @return true => protection enabled; false => protection disabled
+ * @return true => protection enabled; false => protection disabled
*/
boolean getScenarioProtect();
* @param denominator The denominator for the zoom magnification.
* @deprecated 2015-11-23 (circa POI 3.14beta1). Use {@link #setZoom(int)} instead.
*/
+ @Deprecated
void setZoom(int numerator, int denominator);
/**
* Window zoom magnification for current view representing percent values.
- * Valid values range from 10 to 400. Horizontal & Vertical scale together.
+ * Valid values range from 10 to 400. Horizontal & Vertical scale together.
*
* For example:
* <pre>
* @return cell comment or <code>null</code> if not found
* @deprecated as of 2015-11-23 (circa POI 3.14beta1). Use {@link #getCellComment(CellAddress)} instead.
*/
+ @Deprecated
Comment getCellComment(int row, int column);
/**
/**
* Gets the repeating rows used when printing the sheet, as found in
- * File->PageSetup->Sheet.
- * <p/>
+ * File->PageSetup->Sheet.<p>
+ *
* Repeating rows cover a range of contiguous rows, e.g.:
* <pre>
* Sheet1!$1:$1
* </pre>
* The {@link CellRangeAddress} returned contains a column part which spans
* all columns, and a row part which specifies the contiguous range of
- * repeating rows.
- * <p/>
+ * repeating rows.<p>
+ *
* If the Sheet does not have any repeating rows defined, null is returned.
*
* @return an {@link CellRangeAddress} containing the repeating rows for the
/**
* Gets the repeating columns used when printing the sheet, as found in
- * File->PageSetup->Sheet.
- * <p/>
+ * File->PageSetup->Sheet.<p>
+ *
* Repeating columns cover a range of contiguous columns, e.g.:
* <pre>
* Sheet1!$A:$A
* </pre>
* The {@link CellRangeAddress} returned contains a row part which spans all
* rows, and a column part which specifies the contiguous range of
- * repeating columns.
- * <p/>
+ * repeating columns.<p>
+ *
* If the Sheet does not have any repeating columns defined, null is
* returned.
*
/**
* Sets the repeating rows used when printing the sheet, as found in
- * File->PageSetup->Sheet.
- * <p/>
+ * File->PageSetup->Sheet.<p>
+ *
* Repeating rows cover a range of contiguous rows, e.g.:
* <pre>
* Sheet1!$1:$1
/**
* Sets the repeating columns used when printing the sheet, as found in
- * File->PageSetup->Sheet.
- * <p/>
+ * File->PageSetup->Sheet.<p>
+ *
* Repeating columns cover a range of contiguous columns, e.g.:
* <pre>
* Sheet1!$A:$A
/**
* Streaming version of XSSFRow implementing the "BigGridDemo" strategy.
- *
- * @author Alex Geller, Four J's Development Tools
-*/
+ */
public class SXSSFRow implements Row, Comparable<SXSSFRow>
{
private static final Boolean UNDEFINED = null;
* @param initialSize - no longer needed
* @deprecated 2015-11-30 (circa POI 3.14beta1). Use {@link #SXSSFRow(SXSSFSheet)} instead.
*/
+ @Deprecated
public SXSSFRow(SXSSFSheet sheet, @SuppressWarnings("UnusedParameters") int initialSize)
{
this(sheet);
return _height!=-1;
}
+ @Override
public int getOutlineLevel(){
return _outlineLevel;
}
*
* @return the cell at the given (0 based) index
* @throws IllegalArgumentException if cellnum < 0 or the specified MissingCellPolicy is invalid
- * @see Row#RETURN_NULL_AND_BLANK
- * @see Row#RETURN_BLANK_AS_NULL
- * @see Row#CREATE_NULL_AS_BLANK
*/
@Override
public SXSSFCell getCell(int cellnum, MissingCellPolicy policy)
{
checkBounds(cellnum);
- // FIXME: replace with switch(enum)
final SXSSFCell cell = _cells.get(cellnum);
- if (policy == RETURN_NULL_AND_BLANK)
- {
- return cell;
- }
- else if (policy == RETURN_BLANK_AS_NULL)
- {
- if (cell == null || cell.getCellType() == Cell.CELL_TYPE_BLANK)
- {
- return null;
- }
- return cell;
- }
- else if (policy == CREATE_NULL_AS_BLANK)
- {
- if (cell == null)
- {
- return createCell(cellnum, Cell.CELL_TYPE_BLANK);
- }
- return cell;
+ switch (policy) {
+ case RETURN_NULL_AND_BLANK:
+ return cell;
+ case RETURN_BLANK_AS_NULL:
+ boolean isBlank = (cell != null && cell.getCellType() == Cell.CELL_TYPE_BLANK);
+ return (isBlank) ? null : cell;
+ case CREATE_NULL_AS_BLANK:
+ return (cell == null) ? createCell(cellnum, Cell.CELL_TYPE_BLANK) : cell;
+ default:
+ throw new IllegalArgumentException("Illegal policy " + policy + " (" + policy.id + ")");
}
- throw new IllegalArgumentException("Illegal policy " + policy + " (" + policy.id + ")");
}
/**
import org.apache.poi.ss.usermodel.FormulaError;
import org.apache.poi.ss.usermodel.Hyperlink;
import org.apache.poi.ss.usermodel.RichTextString;
-import org.apache.poi.ss.usermodel.Row;
+import org.apache.poi.ss.usermodel.Row.MissingCellPolicy;
import org.apache.poi.ss.util.CellAddress;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.CellReference;
} else {
int prevNum = row.getLastCellNum();
if(prevNum != -1){
- _cellNum = row.getCell(prevNum-1, Row.RETURN_NULL_AND_BLANK).getColumnIndex() + 1;
+ _cellNum = row.getCell(prevNum-1, MissingCellPolicy.RETURN_NULL_AND_BLANK).getColumnIndex() + 1;
}
}
_sharedStringSource = row.getSheet().getWorkbook().getSharedStringSource();
cellFormula.setRef(range.formatAsString());
}
+ @SuppressWarnings("resource")
private void setFormula(String formula, int formulaType) {
XSSFWorkbook wb = _row.getSheet().getWorkbook();
if (formula == null) {
*
* @return the XSSFSheet that owns this row
*/
+ @Override
public XSSFSheet getSheet() {
return this._sheet;
}
*
* @return an iterator over cells in this row.
*/
+ @Override
@SuppressWarnings("unchecked")
public Iterator<Cell> cellIterator() {
return (Iterator<Cell>)(Iterator<? extends Cell>)_cells.values().iterator();
*
* @return an iterator over cells in this row.
*/
+ @Override
public Iterator<Cell> iterator() {
return cellIterator();
}
* @throws IllegalArgumentException if columnIndex < 0 or greater than 16384,
* the maximum number of columns supported by the SpreadsheetML format (.xlsx)
*/
+ @Override
public XSSFCell createCell(int columnIndex) {
return createCell(columnIndex, Cell.CELL_TYPE_BLANK);
}
* @see Cell#CELL_TYPE_NUMERIC
* @see Cell#CELL_TYPE_STRING
*/
+ @Override
public XSSFCell createCell(int columnIndex, int type) {
// Performance optimization for bug 57840: explicit boxing is slightly faster than auto-unboxing, though may use more memory
final Integer colI = new Integer(columnIndex); // NOSONAR
*
* @return the cell at the given (0 based) index
*/
+ @Override
public XSSFCell getCell(int cellnum) {
return getCell(cellnum, _sheet.getWorkbook().getMissingCellPolicy());
}
* Returns the cell at the given (0 based) index, with the specified {@link org.apache.poi.ss.usermodel.Row.MissingCellPolicy}
*
* @return the cell at the given (0 based) index
- * @throws IllegalArgumentException if cellnum < 0 or the specified MissingCellPolicy is invalid
- * @see Row#RETURN_NULL_AND_BLANK
- * @see Row#RETURN_BLANK_AS_NULL
- * @see Row#CREATE_NULL_AS_BLANK
+ * @throws IllegalArgumentException if cellnum < 0 or the specified MissingCellPolicy is invalid
*/
+ @Override
public XSSFCell getCell(int cellnum, MissingCellPolicy policy) {
if(cellnum < 0) throw new IllegalArgumentException("Cell index must be >= 0");
// Performance optimization for bug 57840: explicit boxing is slightly faster than auto-unboxing, though may use more memory
final Integer colI = new Integer(cellnum); // NOSONAR
XSSFCell cell = _cells.get(colI);
- if(policy == RETURN_NULL_AND_BLANK) {
- return cell;
- }
- if(policy == RETURN_BLANK_AS_NULL) {
- if(cell == null) return cell;
- if(cell.getCellType() == Cell.CELL_TYPE_BLANK) {
- return null;
- }
- return cell;
- }
- if(policy == CREATE_NULL_AS_BLANK) {
- if(cell == null) {
- return createCell((short)cellnum, Cell.CELL_TYPE_BLANK);
- }
- return cell;
- }
- throw new IllegalArgumentException("Illegal policy " + policy + " (" + policy.id + ")");
+ switch (policy) {
+ case RETURN_NULL_AND_BLANK:
+ return cell;
+ case RETURN_BLANK_AS_NULL:
+ boolean isBlank = (cell != null && cell.getCellType() == Cell.CELL_TYPE_BLANK);
+ return (isBlank) ? null : cell;
+ case CREATE_NULL_AS_BLANK:
+ return (cell == null) ? createCell(cellnum, Cell.CELL_TYPE_BLANK) : cell;
+ default:
+ throw new IllegalArgumentException("Illegal policy " + policy + " (" + policy.id + ")");
+ }
}
/**
* @return short representing the first logical cell in the row,
* or -1 if the row does not contain any cells.
*/
+ @Override
public short getFirstCellNum() {
return (short)(_cells.size() == 0 ? -1 : _cells.firstKey());
}
* @return short representing the last logical cell in the row <b>PLUS ONE</b>,
* or -1 if the row does not contain any cells.
*/
+ @Override
public short getLastCellNum() {
return (short)(_cells.size() == 0 ? -1 : (_cells.lastKey() + 1));
}
*
* @return row height measured in twips (1/20th of a point)
*/
+ @Override
public short getHeight() {
return (short)(getHeightInPoints()*20);
}
* @return row height measured in point size
* @see org.apache.poi.xssf.usermodel.XSSFSheet#getDefaultRowHeightInPoints()
*/
+ @Override
public float getHeightInPoints() {
if (this._row.isSetHt()) {
return (float) this._row.getHt();
*
* @param height the height in "twips" or 1/20th of a point. <code>-1</code> resets to the default height
*/
+ @Override
public void setHeight(short height) {
if (height == -1) {
if (_row.isSetHt()) _row.unsetHt();
*
* @param height the height in points. <code>-1</code> resets to the default height
*/
+ @Override
public void setHeightInPoints(float height) {
setHeight((short)(height == -1 ? -1 : (height*20)));
}
*
* @return int representing the number of defined cells in the row.
*/
+ @Override
public int getPhysicalNumberOfCells() {
return _cells.size();
}
*
* @return the row number (0 based)
*/
+ @Override
public int getRowNum() {
return (int) (_row.getR() - 1);
}
* @param rowIndex the row number (0-based)
* @throws IllegalArgumentException if rowNum < 0 or greater than 1048575
*/
+ @Override
public void setRowNum(int rowIndex) {
int maxrow = SpreadsheetVersion.EXCEL2007.getLastRowIndex();
if (rowIndex < 0 || rowIndex > maxrow) {
*
* @return - height is zero or not.
*/
+ @Override
public boolean getZeroHeight() {
return this._row.getHidden();
}
*
* @param height height is zero or not.
*/
+ @Override
public void setZeroHeight(boolean height) {
this._row.setHidden(height);
* do have whole-row styles. For those that do, you
* can get the formatting from {@link #getRowStyle()}
*/
+ @Override
public boolean isFormatted() {
return _row.isSetS();
}
* have one of these, so will return null. Call
* {@link #isFormatted()} to check first.
*/
+ @Override
public XSSFCellStyle getRowStyle() {
if(!isFormatted()) return null;
* If the value is null then the style information is removed,
* causing the cell to used the default workbook style.
*/
+ @Override
public void setRowStyle(CellStyle style) {
if(style == null) {
if(_row.isSetS()) {
*
* @param cell the cell to remove
*/
+ @Override
public void removeCell(Cell cell) {
if (cell.getRow() != this) {
throw new IllegalArgumentException("Specified cell does not belong to this row");
}
}
+ @Override
public int getOutlineLevel() {
return _row.getOutlineLevel();
}
* blank cells when fetching from a row.
* See {@link org.apache.poi.ss.usermodel.Row.MissingCellPolicy}
*/
- private MissingCellPolicy _missingCellPolicy = Row.RETURN_NULL_AND_BLANK;
+ private MissingCellPolicy _missingCellPolicy = MissingCellPolicy.RETURN_NULL_AND_BLANK;
/**
* array of pictures for this workbook
* Finds a font that matches the one with the supplied attributes
* @deprecated POI 3.15. Use {@link #findFont(boolean, short, short, String, boolean, boolean, short, byte)} instead.
*/
+ @Deprecated
@Override
public XSSFFont findFont(short boldWeight, short color, short fontHeight, String name, boolean italic, boolean strikeout, short typeOffset, byte underline) {
return stylesSource.findFont(boldWeight, color, fontHeight, name, italic, strikeout, typeOffset, underline);
* @param idx index within the set of styles
* @return XSSFCellStyle object at the index
*/
+ @Override
public XSSFCellStyle getCellStyleAt(int idx) {
return stylesSource.getStyleAt(idx);
}
*
* @return count of cell styles
*/
+ @Override
public int getNumCellStyles() {
return stylesSource.getNumCellStyles();
}
*
* @return an iterator of the sheets.
*/
+ @Override
public Iterator<Sheet> sheetIterator() {
return new SheetIterator<Sheet>();
}
* @param name The name the workbook will be referenced as in formulas
* @param workbook The open workbook to fetch the link required information from
*/
+ @Override
@NotImplemented
public int linkExternalWorkbook(String name, Workbook workbook) {
throw new RuntimeException("Not Implemented - see bug #57184");
import org.apache.poi.hssf.HSSFITestDataProvider;
import org.apache.poi.ss.SpreadsheetVersion;
import org.apache.poi.ss.usermodel.BaseTestXCell;
+import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellCopyPolicy;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.RichTextString;
import org.apache.poi.ss.usermodel.Row;
+import org.apache.poi.ss.usermodel.Row.MissingCellPolicy;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCell;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STCellType;
-/**
- * @author Yegor Kozlov
- */
public final class TestXSSFCell extends BaseTestXCell {
public TestXSSFCell() {
CTCell ctCell = cell.getCTCell(); //low-level bean holding cell's xml
cell.setCellFormula("A2");
- assertEquals(XSSFCell.CELL_TYPE_FORMULA, cell.getCellType());
+ assertEquals(Cell.CELL_TYPE_FORMULA, cell.getCellType());
assertEquals("A2", cell.getCellFormula());
//the value is not set and cell's type='N' which means blank
assertEquals(STCellType.N, ctCell.getT());
//set cached formula value
cell.setCellValue("t='str'");
//we are still of 'formula' type
- assertEquals(XSSFCell.CELL_TYPE_FORMULA, cell.getCellType());
+ assertEquals(Cell.CELL_TYPE_FORMULA, cell.getCellType());
assertEquals("A2", cell.getCellFormula());
//cached formula value is set and cell's type='STR'
assertEquals(STCellType.STR, ctCell.getT());
//now remove the formula, the cached formula result remains
cell.setCellFormula(null);
- assertEquals(XSSFCell.CELL_TYPE_STRING, cell.getCellType());
+ assertEquals(Cell.CELL_TYPE_STRING, cell.getCellType());
assertEquals(STCellType.STR, ctCell.getT());
//the line below failed prior to fix of Bug #47889
assertEquals("t='str'", cell.getStringCellValue());
//revert to a blank cell
cell.setCellValue((String)null);
- assertEquals(XSSFCell.CELL_TYPE_BLANK, cell.getCellType());
+ assertEquals(Cell.CELL_TYPE_BLANK, cell.getCellType());
assertEquals(STCellType.N, ctCell.getT());
assertEquals("", cell.getStringCellValue());
} finally {
//try a string cell
cell = sh.getRow(0).getCell(0);
- assertEquals(XSSFCell.CELL_TYPE_STRING, cell.getCellType());
+ assertEquals(Cell.CELL_TYPE_STRING, cell.getCellType());
assertEquals("a", cell.getStringCellValue());
assertEquals("a", cell.toString());
//Gnumeric produces spreadsheets without styles
//try a numeric cell
cell = sh.getRow(1).getCell(0);
- assertEquals(XSSFCell.CELL_TYPE_NUMERIC, cell.getCellType());
+ assertEquals(Cell.CELL_TYPE_NUMERIC, cell.getCellType());
assertEquals(1.0, cell.getNumericCellValue(), 0);
assertEquals("1.0", cell.toString());
//Gnumeric produces spreadsheets without styles
public void testBug56644ReturnNull() throws IOException {
Workbook wb = XSSFTestDataSamples.openSampleWorkbook("56644.xlsx");
try {
- wb.setMissingCellPolicy(Row.RETURN_BLANK_AS_NULL);
+ wb.setMissingCellPolicy(MissingCellPolicy.RETURN_BLANK_AS_NULL);
Sheet sheet = wb.getSheet("samplelist");
Row row = sheet.getRow(20);
row.createCell(2);
public void testBug56644ReturnBlank() throws IOException {
Workbook wb = XSSFTestDataSamples.openSampleWorkbook("56644.xlsx");
try {
- wb.setMissingCellPolicy(Row.RETURN_NULL_AND_BLANK);
+ wb.setMissingCellPolicy(MissingCellPolicy.RETURN_NULL_AND_BLANK);
Sheet sheet = wb.getSheet("samplelist");
Row row = sheet.getRow(20);
row.createCell(2);
public void testBug56644CreateBlank() throws IOException {
Workbook wb = XSSFTestDataSamples.openSampleWorkbook("56644.xlsx");
try {
- wb.setMissingCellPolicy(Row.CREATE_NULL_AS_BLANK);
+ wb.setMissingCellPolicy(MissingCellPolicy.CREATE_NULL_AS_BLANK);
Sheet sheet = wb.getSheet("samplelist");
Row row = sheet.getRow(20);
row.createCell(2);
final CreationHelper createHelper = wb.getCreationHelper();
srcCell.setCellValue("URL LINK");
- Hyperlink link = createHelper.createHyperlink(Hyperlink.LINK_URL);
+ Hyperlink link = createHelper.createHyperlink(org.apache.poi.common.usermodel.Hyperlink.LINK_URL);
link.setAddress("http://poi.apache.org/");
srcCell.setHyperlink(link);
final CreationHelper createHelper = wb.getCreationHelper();
srcCell.setCellValue("URL LINK");
- Hyperlink link = createHelper.createHyperlink(Hyperlink.LINK_URL);
+ Hyperlink link = createHelper.createHyperlink(org.apache.poi.common.usermodel.Hyperlink.LINK_URL);
link.setAddress("http://poi.apache.org/");
destCell.setHyperlink(link);
srcCell.setCellFormula("2+3");
final CellStyle style = wb.createCellStyle();
- style.setBorderTop(CellStyle.BORDER_THICK);
+ style.setBorderTop(BorderStyle.THICK);
style.setFillBackgroundColor((short) 5);
srcCell.setCellStyle(style);
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.RichTextString;
import org.apache.poi.ss.usermodel.Row;
+import org.apache.poi.ss.usermodel.Row.MissingCellPolicy;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
// read-only mode works!
Workbook workbook = WorkbookFactory.create(OPCPackage.open(file, PackageAccess.READ));
- Date dateAct = workbook.getSheetAt(0).getRow(0).getCell(0, Row.CREATE_NULL_AS_BLANK).getDateCellValue();
+ Date dateAct = workbook.getSheetAt(0).getRow(0).getCell(0, MissingCellPolicy.CREATE_NULL_AS_BLANK).getDateCellValue();
assertEquals(dateExp, dateAct);
workbook.close();
workbook = null;
workbook = WorkbookFactory.create(OPCPackage.open(file, PackageAccess.READ));
- dateAct = workbook.getSheetAt(0).getRow(0).getCell(0, Row.CREATE_NULL_AS_BLANK).getDateCellValue();
+ dateAct = workbook.getSheetAt(0).getRow(0).getCell(0, MissingCellPolicy.CREATE_NULL_AS_BLANK).getDateCellValue();
assertEquals(dateExp, dateAct);
workbook.close();
workbook = null;
// now check read/write mode
workbook = WorkbookFactory.create(OPCPackage.open(file, PackageAccess.READ_WRITE));
- dateAct = workbook.getSheetAt(0).getRow(0).getCell(0, Row.CREATE_NULL_AS_BLANK).getDateCellValue();
+ dateAct = workbook.getSheetAt(0).getRow(0).getCell(0, MissingCellPolicy.CREATE_NULL_AS_BLANK).getDateCellValue();
assertEquals(dateExp, dateAct);
workbook.close();
workbook = null;
workbook = WorkbookFactory.create(OPCPackage.open(file, PackageAccess.READ_WRITE));
- dateAct = workbook.getSheetAt(0).getRow(0).getCell(0, Row.CREATE_NULL_AS_BLANK).getDateCellValue();
+ dateAct = workbook.getSheetAt(0).getRow(0).getCell(0, MissingCellPolicy.CREATE_NULL_AS_BLANK).getDateCellValue();
assertEquals(dateExp, dateAct);
workbook.close();
workbook = null;
import org.apache.poi.ss.ITestDataProvider;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
+import org.apache.poi.ss.usermodel.Row.MissingCellPolicy;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.util.LocaleUtil;
protected void openWorkbook(String workbookName)
throws IOException {
workbook = _testDataProvider.openSampleWorkbook(workbookName);
- workbook.setMissingCellPolicy(Row.CREATE_NULL_AS_BLANK);
+ workbook.setMissingCellPolicy(MissingCellPolicy.CREATE_NULL_AS_BLANK);
testFile = workbookName;
}
import java.util.Iterator;
import org.apache.poi.ss.ITestDataProvider;
+import org.apache.poi.ss.usermodel.Row.MissingCellPolicy;
import org.junit.Test;
/**
assertEquals(Cell.CELL_TYPE_NUMERIC, row.getCell(5).getCellType());
// RETURN_NULL_AND_BLANK - same as default
- assertEquals(Cell.CELL_TYPE_STRING, row.getCell(0, Row.RETURN_NULL_AND_BLANK).getCellType());
- assertEquals(Cell.CELL_TYPE_NUMERIC, row.getCell(1, Row.RETURN_NULL_AND_BLANK).getCellType());
- assertEquals(null, row.getCell(2, Row.RETURN_NULL_AND_BLANK));
- assertEquals(null, row.getCell(3, Row.RETURN_NULL_AND_BLANK));
- assertEquals(Cell.CELL_TYPE_BLANK, row.getCell(4, Row.RETURN_NULL_AND_BLANK).getCellType());
- assertEquals(Cell.CELL_TYPE_NUMERIC, row.getCell(5, Row.RETURN_NULL_AND_BLANK).getCellType());
+ assertEquals(Cell.CELL_TYPE_STRING, row.getCell(0, MissingCellPolicy.RETURN_NULL_AND_BLANK).getCellType());
+ assertEquals(Cell.CELL_TYPE_NUMERIC, row.getCell(1, MissingCellPolicy.RETURN_NULL_AND_BLANK).getCellType());
+ assertEquals(null, row.getCell(2, MissingCellPolicy.RETURN_NULL_AND_BLANK));
+ assertEquals(null, row.getCell(3, MissingCellPolicy.RETURN_NULL_AND_BLANK));
+ assertEquals(Cell.CELL_TYPE_BLANK, row.getCell(4, MissingCellPolicy.RETURN_NULL_AND_BLANK).getCellType());
+ assertEquals(Cell.CELL_TYPE_NUMERIC, row.getCell(5, MissingCellPolicy.RETURN_NULL_AND_BLANK).getCellType());
// RETURN_BLANK_AS_NULL - nearly the same
- assertEquals(Cell.CELL_TYPE_STRING, row.getCell(0, Row.RETURN_BLANK_AS_NULL).getCellType());
- assertEquals(Cell.CELL_TYPE_NUMERIC, row.getCell(1, Row.RETURN_BLANK_AS_NULL).getCellType());
- assertEquals(null, row.getCell(2, Row.RETURN_BLANK_AS_NULL));
- assertEquals(null, row.getCell(3, Row.RETURN_BLANK_AS_NULL));
- assertEquals(null, row.getCell(4, Row.RETURN_BLANK_AS_NULL));
- assertEquals(Cell.CELL_TYPE_NUMERIC, row.getCell(5, Row.RETURN_BLANK_AS_NULL).getCellType());
+ assertEquals(Cell.CELL_TYPE_STRING, row.getCell(0, MissingCellPolicy.RETURN_BLANK_AS_NULL).getCellType());
+ assertEquals(Cell.CELL_TYPE_NUMERIC, row.getCell(1, MissingCellPolicy.RETURN_BLANK_AS_NULL).getCellType());
+ assertEquals(null, row.getCell(2, MissingCellPolicy.RETURN_BLANK_AS_NULL));
+ assertEquals(null, row.getCell(3, MissingCellPolicy.RETURN_BLANK_AS_NULL));
+ assertEquals(null, row.getCell(4, MissingCellPolicy.RETURN_BLANK_AS_NULL));
+ assertEquals(Cell.CELL_TYPE_NUMERIC, row.getCell(5, MissingCellPolicy.RETURN_BLANK_AS_NULL).getCellType());
// CREATE_NULL_AS_BLANK - creates as needed
- assertEquals(Cell.CELL_TYPE_STRING, row.getCell(0, Row.CREATE_NULL_AS_BLANK).getCellType());
- assertEquals(Cell.CELL_TYPE_NUMERIC, row.getCell(1, Row.CREATE_NULL_AS_BLANK).getCellType());
- assertEquals(Cell.CELL_TYPE_BLANK, row.getCell(2, Row.CREATE_NULL_AS_BLANK).getCellType());
- assertEquals(Cell.CELL_TYPE_BLANK, row.getCell(3, Row.CREATE_NULL_AS_BLANK).getCellType());
- assertEquals(Cell.CELL_TYPE_BLANK, row.getCell(4, Row.CREATE_NULL_AS_BLANK).getCellType());
- assertEquals(Cell.CELL_TYPE_NUMERIC, row.getCell(5, Row.CREATE_NULL_AS_BLANK).getCellType());
+ assertEquals(Cell.CELL_TYPE_STRING, row.getCell(0, MissingCellPolicy.CREATE_NULL_AS_BLANK).getCellType());
+ assertEquals(Cell.CELL_TYPE_NUMERIC, row.getCell(1, MissingCellPolicy.CREATE_NULL_AS_BLANK).getCellType());
+ assertEquals(Cell.CELL_TYPE_BLANK, row.getCell(2, MissingCellPolicy.CREATE_NULL_AS_BLANK).getCellType());
+ assertEquals(Cell.CELL_TYPE_BLANK, row.getCell(3, MissingCellPolicy.CREATE_NULL_AS_BLANK).getCellType());
+ assertEquals(Cell.CELL_TYPE_BLANK, row.getCell(4, MissingCellPolicy.CREATE_NULL_AS_BLANK).getCellType());
+ assertEquals(Cell.CELL_TYPE_NUMERIC, row.getCell(5, MissingCellPolicy.CREATE_NULL_AS_BLANK).getCellType());
// Check created ones get the right column
- assertEquals(0, row.getCell(0, Row.CREATE_NULL_AS_BLANK).getColumnIndex());
- assertEquals(1, row.getCell(1, Row.CREATE_NULL_AS_BLANK).getColumnIndex());
- assertEquals(2, row.getCell(2, Row.CREATE_NULL_AS_BLANK).getColumnIndex());
- assertEquals(3, row.getCell(3, Row.CREATE_NULL_AS_BLANK).getColumnIndex());
- assertEquals(4, row.getCell(4, Row.CREATE_NULL_AS_BLANK).getColumnIndex());
- assertEquals(5, row.getCell(5, Row.CREATE_NULL_AS_BLANK).getColumnIndex());
+ assertEquals(0, row.getCell(0, MissingCellPolicy.CREATE_NULL_AS_BLANK).getColumnIndex());
+ assertEquals(1, row.getCell(1, MissingCellPolicy.CREATE_NULL_AS_BLANK).getColumnIndex());
+ assertEquals(2, row.getCell(2, MissingCellPolicy.CREATE_NULL_AS_BLANK).getColumnIndex());
+ assertEquals(3, row.getCell(3, MissingCellPolicy.CREATE_NULL_AS_BLANK).getColumnIndex());
+ assertEquals(4, row.getCell(4, MissingCellPolicy.CREATE_NULL_AS_BLANK).getColumnIndex());
+ assertEquals(5, row.getCell(5, MissingCellPolicy.CREATE_NULL_AS_BLANK).getColumnIndex());
// Now change the cell policy on the workbook, check
// that that is now used if no policy given
- workbook.setMissingCellPolicy(Row.RETURN_BLANK_AS_NULL);
+ workbook.setMissingCellPolicy(MissingCellPolicy.RETURN_BLANK_AS_NULL);
assertEquals(Cell.CELL_TYPE_STRING, row.getCell(0).getCellType());
assertEquals(Cell.CELL_TYPE_NUMERIC, row.getCell(1).getCellType());
import java.io.InputStreamReader;
import org.apache.poi.hssf.HSSFTestDataSamples;
+import org.apache.poi.ss.usermodel.Row.MissingCellPolicy;
import org.apache.poi.util.LocaleUtil;
import org.junit.Test;
String[] truths = truthLine.split("\t");
// Intentionally ignore the last column (tika-1132), for now
for (short j = 3; j < 12; j++){
- Cell cell = r.getCell(j, Row.CREATE_NULL_AS_BLANK);
+ Cell cell = r.getCell(j, MissingCellPolicy.CREATE_NULL_AS_BLANK);
String formatted = clean(formatter.formatCellValue(cell, evaluator));
if (truths.length <= j){
continue;