List of usage examples for org.apache.poi.hssf.usermodel HSSFCell getErrorCellValue
@Override public byte getErrorCellValue()
From source file:com.learn.core.utils.HSSFReadWrite.java
License:Apache License
/** * Method main//from w w w . ja va 2 s . co m * * Given 1 argument takes that as the filename, inputs it and dumps the * cell values/types out to sys.out.<br> * * given 2 arguments where the second argument is the word "write" and the * first is the filename - writes out a sample (test) spreadsheet * see {@link HSSFReadWrite#testCreateSampleSheet(String)}.<br> * * given 2 arguments where the first is an input filename and the second * an output filename (not write), attempts to fully read in the * spreadsheet and fully write it out.<br> * * given 3 arguments where the first is an input filename and the second an * output filename (not write) and the third is "modify1", attempts to read in the * spreadsheet, deletes rows 0-24, 74-99. Changes cell at row 39, col 3 to * "MODIFIED CELL" then writes it out. Hence this is "modify test 1". If you * take the output from the write test, you'll have a valid scenario. */ public static void main(String[] args) { if (args.length < 1) { System.err.println("At least one argument expected"); return; } String fileName = args[0]; try { if (args.length < 2) { try (HSSFWorkbook wb = HSSFReadWrite.readFile(fileName)) { System.out.println("Data dump:\n"); for (int k = 0; k < wb.getNumberOfSheets(); k++) { HSSFSheet sheet = wb.getSheetAt(k); int rows = sheet.getPhysicalNumberOfRows(); System.out .println("Sheet " + k + " \"" + wb.getSheetName(k) + "\" has " + rows + " row(s)."); for (int r = 0; r < rows; r++) { HSSFRow row = sheet.getRow(r); if (row == null) { continue; } System.out.println("\nROW " + row.getRowNum() + " has " + row.getPhysicalNumberOfCells() + " cell(s)."); for (int c = 0; c < row.getLastCellNum(); c++) { HSSFCell cell = row.getCell(c); String value; if (cell != null) { switch (cell.getCellTypeEnum()) { case FORMULA: value = "FORMULA value=" + cell.getCellFormula(); break; case NUMERIC: value = "NUMERIC value=" + cell.getNumericCellValue(); break; case STRING: value = "STRING value=" + cell.getStringCellValue(); break; case BLANK: value = "<BLANK>"; break; case BOOLEAN: value = "BOOLEAN value-" + cell.getBooleanCellValue(); break; case ERROR: value = "ERROR value=" + cell.getErrorCellValue(); break; default: value = "UNKNOWN value of type " + cell.getCellTypeEnum(); } System.out.println("CELL col=" + cell.getColumnIndex() + " VALUE=" + value); } } } } } } else if (args.length == 2) { if (args[1].toLowerCase(Locale.ROOT).equals("write")) { System.out.println("Write mode"); long time = System.currentTimeMillis(); HSSFReadWrite.testCreateSampleSheet(fileName); System.out.println("" + (System.currentTimeMillis() - time) + " ms generation time"); } else { System.out.println("readwrite test"); try (HSSFWorkbook wb = HSSFReadWrite.readFile(fileName)) { try (FileOutputStream stream = new FileOutputStream(args[1])) { wb.write(stream); } } } } else if (args.length == 3 && args[2].equalsIgnoreCase("modify1")) { // delete row 0-24, row 74 - 99 && change cell 3 on row 39 to string "MODIFIED CELL!!" try (HSSFWorkbook wb = HSSFReadWrite.readFile(fileName)) { HSSFSheet sheet = wb.getSheetAt(0); for (int k = 0; k < 25; k++) { HSSFRow row = sheet.getRow(k); sheet.removeRow(row); } for (int k = 74; k < 100; k++) { HSSFRow row = sheet.getRow(k); sheet.removeRow(row); } HSSFRow row = sheet.getRow(39); HSSFCell cell = row.getCell(3); cell.setCellValue("MODIFIED CELL!!!!!"); try (FileOutputStream stream = new FileOutputStream(args[1])) { wb.write(stream); } } } } catch (Exception e) { e.printStackTrace(); } }
From source file:com.primovision.lutransport.service.ImportMainSheetServiceImpl.java
/** * This is a helper method to retrieve the value of a cell regardles of its * type, which will be converted into a String. * //w ww. ja v a 2s . c o m * @param cell * @return */ private Object getCellValue(HSSFCell cell) { if (cell == null) { return null; } Object result = null; int cellType = cell.getCellType(); switch (cellType) { case HSSFCell.CELL_TYPE_BLANK: result = ""; break; case HSSFCell.CELL_TYPE_BOOLEAN: result = cell.getBooleanCellValue() ? Boolean.TRUE : Boolean.FALSE; break; case HSSFCell.CELL_TYPE_ERROR: result = "ERROR: " + cell.getErrorCellValue(); break; case HSSFCell.CELL_TYPE_FORMULA: result = cell.getCellFormula(); break; case HSSFCell.CELL_TYPE_NUMERIC: HSSFCellStyle cellStyle = cell.getCellStyle(); short dataFormat = cellStyle.getDataFormat(); // assumption is made that dataFormat = 14, // when cellType is HSSFCell.CELL_TYPE_NUMERIC // is equal to a DATE format. if (dataFormat == 164) { result = cell.getDateCellValue(); } else { result = cell.getNumericCellValue(); } break; case HSSFCell.CELL_TYPE_STRING: result = cell.getStringCellValue(); break; default: break; } if (result instanceof Double) { return String.valueOf(((Double) result).longValue()); } if (result instanceof Date) { return result; } return result.toString(); }
From source file:com.primovision.lutransport.service.ImportMainSheetServiceImpl.java
private Object getCellValue(HSSFCell cell, boolean resolveFormula) { if (cell == null) { return null; }/*from ww w . j a va 2s .co m*/ Object result = null; int cellType = cell.getCellType(); switch (cellType) { case HSSFCell.CELL_TYPE_BLANK: result = ""; break; case HSSFCell.CELL_TYPE_BOOLEAN: result = cell.getBooleanCellValue() ? Boolean.TRUE : Boolean.FALSE; break; case HSSFCell.CELL_TYPE_ERROR: result = "ERROR: " + cell.getErrorCellValue(); break; case HSSFCell.CELL_TYPE_FORMULA: switch (cell.getCachedFormulaResultType()) { case HSSFCell.CELL_TYPE_NUMERIC: /*System.out.println("Last evaluated as: " + cell.getNumericCellValue()); result = cell.getNumericCellValue(); break;*/ if (DateUtil.isCellDateFormatted(cell)) { result = cell.getDateCellValue(); } else { result = cell.getNumericCellValue(); } System.out.println("Numeric cell value == " + result); break; case HSSFCell.CELL_TYPE_STRING: System.out.println("Last evaluated as \"" + cell.getRichStringCellValue() + "\""); result = cell.getRichStringCellValue(); break; } //result = cell.getCellFormula(); break; case HSSFCell.CELL_TYPE_NUMERIC: HSSFCellStyle cellStyle = cell.getCellStyle(); short dataFormat = cellStyle.getDataFormat(); System.out.println("Data format for " + cell.getColumnIndex() + " = " + dataFormat); // assumption is made that dataFormat = 14, // when cellType is HSSFCell.CELL_TYPE_NUMERIC // is equal to a DATE format. //if (dataFormat == 165 || dataFormat == 164 || dataFormat == 14) { if (DateUtil.isCellDateFormatted(cell)) { result = cell.getDateCellValue(); } else { result = cell.getNumericCellValue(); } if (dataFormat == 0) { // alternative way of getting value : can this be replaced for the entire block result = new HSSFDataFormatter().formatCellValue(cell); } System.out.println("Numeric cell value == " + result); break; case HSSFCell.CELL_TYPE_STRING: //result = cell.getStringCellValue(); result = cell.getRichStringCellValue(); System.out.println("String -> " + result); break; default: break; } if (result instanceof Integer) { return String.valueOf((Integer) result); } else if (result instanceof Double) { return String.valueOf(((Double) result)); //.longValue()); } if (result instanceof Date) { return result; } return result.toString(); }
From source file:com.pureinfo.studio.db.xls2srm.impl.XlsObjectsImpl.java
License:Open Source License
/** * @see com.pureinfo.dolphin.model.IObjects#next() */// w ww . jav a2 s. com public DolphinObject next() throws PureException { HSSFRow row = m_sheet.getRow(m_nCurrent++); if (row == null) return null; //else DolphinObject obj = new DolphinObject(); Object oValue; HSSFCell cell; int nCellNum = row.getLastCellNum(); if (nCellNum > m_heads.length) { nCellNum = m_heads.length; } for (int i = 0; i < nCellNum; i++) { cell = row.getCell((short) i); if (cell == null) { oValue = null; } else { switch (cell.getCellType()) { case HSSFCell.CELL_TYPE_NUMERIC: oValue = new Double(cell.getNumericCellValue()); break; case HSSFCell.CELL_TYPE_STRING: oValue = cell.getStringCellValue(); if (oValue != null) oValue = ((String) oValue).trim(); break; case HSSFCell.CELL_TYPE_FORMULA: oValue = new Double(cell.getNumericCellValue()); break; case HSSFCell.CELL_TYPE_BOOLEAN: oValue = new Boolean(cell.getBooleanCellValue()); break; case HSSFCell.CELL_TYPE_ERROR: throw new PureException(PureException.INVALID_VALUE, "error value in cell[" + i + "]-" + m_heads[i] + ": " + String.valueOf(cell.getErrorCellValue())); //case HSSFCell.CELL_TYPE_BLANK: default: oValue = null; }//endcase if (oValue instanceof Number) { int nFormat = cell.getCellStyle().getDataFormat(); if (nFormat >= 0xe && nFormat <= 0x16) { oValue = cell.getDateCellValue(); } else if (nFormat == 1) { oValue = new Long(((Number) oValue).intValue()); } } } obj.setProperty(m_heads[i], oValue); } return obj; }
From source file:com.report.excel.ExcelToHtmlConverter.java
License:Apache License
protected boolean processCell(HSSFCell cell, Element tableCellElement, int normalWidthPx, int maxSpannedWidthPx, float normalHeightPt) { final HSSFCellStyle cellStyle = cell.getCellStyle(); String value;/*from www. j a va 2s . c om*/ switch (cell.getCellType()) { case HSSFCell.CELL_TYPE_STRING: // XXX: enrich value = cell.getRichStringCellValue().getString(); break; case HSSFCell.CELL_TYPE_FORMULA: /*switch (evaluator.evaluateFormulaCell(cell)) { case Cell.CELL_TYPE_BOOLEAN: value = cell.getBooleanCellValue(); break; case Cell.CELL_TYPE_NUMERIC: value = cell.getNumericCellValue(); break; case Cell.CELL_TYPE_STRING: System.out.println(cell.getStringCellValue()); break; case Cell.CELL_TYPE_BLANK: break; case Cell.CELL_TYPE_ERROR: System.out.println(cell.getErrorCellValue()); break; case Cell.CELL_TYPE_FORMULA: break; }*/ switch (cell.getCachedFormulaResultType()) { case HSSFCell.CELL_TYPE_STRING: HSSFRichTextString str = cell.getRichStringCellValue(); if (str != null && str.length() > 0) { value = (str.toString()); } else { value = ExcelToHtmlUtils.EMPTY; } break; case HSSFCell.CELL_TYPE_NUMERIC: HSSFCellStyle style = cellStyle; if (style == null) { value = String.valueOf(cell.getNumericCellValue()); } else { value = (_formatter.formatRawCellContents(cell.getNumericCellValue(), style.getDataFormat(), style.getDataFormatString())); } break; case HSSFCell.CELL_TYPE_BOOLEAN: value = String.valueOf(cell.getBooleanCellValue()); break; case HSSFCell.CELL_TYPE_ERROR: value = ErrorEval.getText(cell.getErrorCellValue()); break; default: logger.log(POILogger.WARN, "Unexpected cell cachedFormulaResultType (" + cell.getCachedFormulaResultType() + ")"); value = ExcelToHtmlUtils.EMPTY; break; } break; case HSSFCell.CELL_TYPE_BLANK: value = ExcelToHtmlUtils.EMPTY; break; case HSSFCell.CELL_TYPE_NUMERIC: value = _formatter.formatCellValue(cell); break; case HSSFCell.CELL_TYPE_BOOLEAN: value = String.valueOf(cell.getBooleanCellValue()); break; case HSSFCell.CELL_TYPE_ERROR: value = ErrorEval.getText(cell.getErrorCellValue()); break; default: logger.log(POILogger.WARN, "Unexpected cell type (" + cell.getCellType() + ")"); return true; } final boolean noText = ExcelToHtmlUtils.isEmpty(value); final boolean wrapInDivs = !noText && isUseDivsToSpan() && !cellStyle.getWrapText(); final short cellStyleIndex = cellStyle.getIndex(); if (cellStyleIndex != 0) { HSSFWorkbook workbook = cell.getRow().getSheet().getWorkbook(); String mainCssClass = getStyleClassName(workbook, cellStyle); if (wrapInDivs) { tableCellElement.setAttribute("class", mainCssClass + " " + cssClassContainerCell); } else { tableCellElement.setAttribute("class", mainCssClass); } if (noText) { /* * if cell style is defined (like borders, etc.) but cell text * is empty, add " " to output, so browser won't collapse * and ignore cell */ value = "\u00A0"; } } if (isOutputLeadingSpacesAsNonBreaking() && value.startsWith(" ")) { StringBuilder builder = new StringBuilder(); for (int c = 0; c < value.length(); c++) { if (value.charAt(c) != ' ') break; builder.append('\u00a0'); } if (value.length() != builder.length()) builder.append(value.substring(builder.length())); value = builder.toString(); } Text text = htmlDocumentFacade.createText(value); if (wrapInDivs) { Element outerDiv = htmlDocumentFacade.createBlock(); outerDiv.setAttribute("class", this.cssClassContainerDiv); Element innerDiv = htmlDocumentFacade.createBlock(); StringBuilder innerDivStyle = new StringBuilder(); innerDivStyle.append("position:absolute;min-width:"); innerDivStyle.append(normalWidthPx); innerDivStyle.append("px;"); if (maxSpannedWidthPx != Integer.MAX_VALUE) { innerDivStyle.append("max-width:"); innerDivStyle.append(maxSpannedWidthPx); innerDivStyle.append("px;"); } innerDivStyle.append("overflow:hidden;max-height:"); innerDivStyle.append(normalHeightPt); innerDivStyle.append("pt;white-space:nowrap;"); ExcelToHtmlUtils.appendAlign(innerDivStyle, cellStyle.getAlignment()); htmlDocumentFacade.addStyleClass(outerDiv, cssClassPrefixDiv, innerDivStyle.toString()); innerDiv.appendChild(text); outerDiv.appendChild(innerDiv); tableCellElement.appendChild(outerDiv); } else { tableCellElement.appendChild(text); } return ExcelToHtmlUtils.isEmpty(value) && cellStyleIndex == 0; }
From source file:com.siva.javamultithreading.ExcelUtil.java
public static void copyCell(HSSFWorkbook newWorkbook, HSSFCell oldCell, HSSFCell newCell, Map<Integer, HSSFCellStyle> styleMap) { if (styleMap != null) { int stHashCode = oldCell.getCellStyle().hashCode(); HSSFCellStyle newCellStyle = styleMap.get(stHashCode); if (newCellStyle == null) { newCellStyle = newWorkbook.createCellStyle(); newCellStyle.cloneStyleFrom(oldCell.getCellStyle()); styleMap.put(stHashCode, newCellStyle); }/*w ww . j av a2 s. c om*/ newCell.setCellStyle(newCellStyle); } switch (oldCell.getCellType()) { case HSSFCell.CELL_TYPE_STRING: newCell.setCellValue(oldCell.getRichStringCellValue()); break; case HSSFCell.CELL_TYPE_NUMERIC: newCell.setCellValue(oldCell.getNumericCellValue()); break; case HSSFCell.CELL_TYPE_BLANK: newCell.setCellType(HSSFCell.CELL_TYPE_BLANK); break; case HSSFCell.CELL_TYPE_BOOLEAN: newCell.setCellValue(oldCell.getBooleanCellValue()); break; case HSSFCell.CELL_TYPE_ERROR: newCell.setCellErrorValue(oldCell.getErrorCellValue()); break; case HSSFCell.CELL_TYPE_FORMULA: newCell.setCellFormula(oldCell.getCellFormula()); break; default: break; } }
From source file:com.wangzhu.poi.ExcelToHtmlConverter.java
License:Apache License
protected boolean processCell(HSSFCell cell, Element tableCellElement, int normalWidthPx, int maxSpannedWidthPx, float normalHeightPt) { final HSSFCellStyle cellStyle = cell.getCellStyle(); String value;// www . ja va 2 s.c o m switch (cell.getCellType()) { case Cell.CELL_TYPE_STRING: // XXX: enrich value = cell.getRichStringCellValue().getString(); break; case Cell.CELL_TYPE_FORMULA: switch (cell.getCachedFormulaResultType()) { case Cell.CELL_TYPE_STRING: HSSFRichTextString str = cell.getRichStringCellValue(); if ((str != null) && (str.length() > 0)) { value = (str.toString()); } else { value = ExcelToHtmlUtils.EMPTY; } break; case Cell.CELL_TYPE_NUMERIC: HSSFCellStyle style = cellStyle; if (style == null) { value = String.valueOf(cell.getNumericCellValue()); } else { value = (this._formatter.formatRawCellContents(cell.getNumericCellValue(), style.getDataFormat(), style.getDataFormatString())); } break; case Cell.CELL_TYPE_BOOLEAN: value = String.valueOf(cell.getBooleanCellValue()); break; case Cell.CELL_TYPE_ERROR: value = ErrorEval.getText(cell.getErrorCellValue()); break; default: ExcelToHtmlConverter.logger.log(POILogger.WARN, "Unexpected cell cachedFormulaResultType (" + cell.getCachedFormulaResultType() + ")"); value = ExcelToHtmlUtils.EMPTY; break; } break; case Cell.CELL_TYPE_BLANK: value = ExcelToHtmlUtils.EMPTY; break; case Cell.CELL_TYPE_NUMERIC: value = this._formatter.formatCellValue(cell); break; case Cell.CELL_TYPE_BOOLEAN: value = String.valueOf(cell.getBooleanCellValue()); break; case Cell.CELL_TYPE_ERROR: value = ErrorEval.getText(cell.getErrorCellValue()); break; default: ExcelToHtmlConverter.logger.log(POILogger.WARN, "Unexpected cell type (" + cell.getCellType() + ")"); return true; } final boolean noText = ExcelToHtmlUtils.isEmpty(value); final boolean wrapInDivs = !noText && this.isUseDivsToSpan() && !cellStyle.getWrapText(); final short cellStyleIndex = cellStyle.getIndex(); if (cellStyleIndex != 0) { HSSFWorkbook workbook = cell.getRow().getSheet().getWorkbook(); String mainCssClass = this.getStyleClassName(workbook, cellStyle); if (wrapInDivs) { tableCellElement.setAttribute("class", mainCssClass + " " + this.cssClassContainerCell); } else { tableCellElement.setAttribute("class", mainCssClass); } if (noText) { /* * if cell style is defined (like borders, etc.) but cell text * is empty, add " " to output, so browser won't collapse * and ignore cell */ value = "\u00A0"; } } if (this.isOutputLeadingSpacesAsNonBreaking() && value.startsWith(" ")) { StringBuffer builder = new StringBuffer(); for (int c = 0; c < value.length(); c++) { if (value.charAt(c) != ' ') { break; } builder.append('\u00a0'); } if (value.length() != builder.length()) { builder.append(value.substring(builder.length())); } value = builder.toString(); } Text text = this.htmlDocumentFacade.createText(value); if (wrapInDivs) { Element outerDiv = this.htmlDocumentFacade.createBlock(); outerDiv.setAttribute("class", this.cssClassContainerDiv); Element innerDiv = this.htmlDocumentFacade.createBlock(); StringBuffer innerDivStyle = new StringBuffer(); innerDivStyle.append("position:absolute;min-width:"); innerDivStyle.append(normalWidthPx); innerDivStyle.append("px;"); if (maxSpannedWidthPx != Integer.MAX_VALUE) { innerDivStyle.append("max-width:"); innerDivStyle.append(maxSpannedWidthPx); innerDivStyle.append("px;"); } innerDivStyle.append("overflow:hidden;max-height:"); innerDivStyle.append(normalHeightPt); innerDivStyle.append("pt;white-space:nowrap;"); ExcelToHtmlUtils.appendAlign(innerDivStyle, cellStyle.getAlignment()); this.htmlDocumentFacade.addStyleClass(outerDiv, this.cssClassPrefixDiv, innerDivStyle.toString()); innerDiv.appendChild(text); outerDiv.appendChild(innerDiv); tableCellElement.appendChild(outerDiv); } else { tableCellElement.appendChild(text); } return ExcelToHtmlUtils.isEmpty(value) && (cellStyleIndex == 0); }
From source file:de.viaboxx.nlstools.formats.MBExcelPersistencer.java
License:Apache License
private Object getValue(HSSFCell cell, CellType cellType) { switch (cellType) { case NUMERIC: if (HSSFDateUtil.isCellDateFormatted(cell)) { return cell.getDateCellValue(); } else {/*from w w w. j a v a 2s. c o m*/ return cell.getNumericCellValue(); } case FORMULA: return getValue(cell, cell.getCachedFormulaResultTypeEnum()); case BOOLEAN: return cell.getBooleanCellValue(); case STRING: return cell.getStringCellValue(); case ERROR: return cell.getErrorCellValue(); default: return null; // do not handle Formular, Error, Blank, ... } }
From source file:edu.fcps.hutchison.LabelBuilder.java
License:Open Source License
protected String getCellValStr(HSSFCell cell) { if (cell == null) { return ""; } else if (cell.getCellType() == HSSFCell.CELL_TYPE_BOOLEAN) { return Boolean.toString(cell.getBooleanCellValue()); } else if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) { return Double.toString(cell.getNumericCellValue()); } else if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) { return cell.getRichStringCellValue().toString(); } else if (cell.getCellType() == HSSFCell.CELL_TYPE_BLANK) { return ""; } else if (cell.getCellType() == HSSFCell.CELL_TYPE_FORMULA) { return cell.getCellFormula(); } else if (cell.getCellType() == HSSFCell.CELL_TYPE_ERROR) { return String.valueOf(cell.getErrorCellValue()); } else {/*from ww w . j ava 2 s . co m*/ return ""; } }
From source file:gda.hrpd.data.ExcelWorkbook.java
License:Open Source License
/** * gets value from the specified cell and return it as String. * //from www . j ava 2s . c o m * @param cell * @return value from cell as a String */ public String getCellValue(HSSFCell cell) { // If the cell is null return an empty string if (cell == null) { return ""; } String value = null; if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) { value = cell.getRichStringCellValue().toString(); } else if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) { value = String.valueOf(cell.getNumericCellValue()); } else if (cell.getCellType() == HSSFCell.CELL_TYPE_BLANK) { value = " "; } else if (cell.getCellType() == HSSFCell.CELL_TYPE_BOOLEAN) { value = String.valueOf(cell.getBooleanCellValue()); } else if (cell.getCellType() == HSSFCell.CELL_TYPE_ERROR) { value = String.valueOf(cell.getErrorCellValue()); } else if (cell.getCellType() == HSSFCell.CELL_TYPE_FORMULA) { value = cell.getCellFormula(); } return value; }