List of usage examples for org.apache.poi.ss.usermodel Cell getErrorCellValue
byte getErrorCellValue();
From source file:gov.nih.nci.evs.app.neopl.XLStoXLSX.java
License:Open Source License
/** * @param args// w w w . java 2 s . com * @throws InvalidFormatException * @throws IOException */ public static void run(String inputfile, String outputfile) throws IOException { InputStream in = new BufferedInputStream(new FileInputStream(inputfile)); try { Workbook wbIn = new HSSFWorkbook(in); File outFn = new File(outputfile); if (outFn.exists()) { outFn.delete(); } Workbook wbOut = new XSSFWorkbook(); int sheetCnt = wbIn.getNumberOfSheets(); for (int i = 0; i < sheetCnt; i++) { Sheet sIn = wbIn.getSheetAt(0); Sheet sOut = wbOut.createSheet(sIn.getSheetName()); Iterator<Row> rowIt = sIn.rowIterator(); while (rowIt.hasNext()) { Row rowIn = rowIt.next(); Row rowOut = sOut.createRow(rowIn.getRowNum()); Iterator<Cell> cellIt = rowIn.cellIterator(); while (cellIt.hasNext()) { Cell cellIn = cellIt.next(); Cell cellOut = rowOut.createCell(cellIn.getColumnIndex(), cellIn.getCellType()); switch (cellIn.getCellType()) { case Cell.CELL_TYPE_BLANK: break; case Cell.CELL_TYPE_BOOLEAN: cellOut.setCellValue(cellIn.getBooleanCellValue()); break; case Cell.CELL_TYPE_ERROR: cellOut.setCellValue(cellIn.getErrorCellValue()); break; case Cell.CELL_TYPE_FORMULA: cellOut.setCellFormula(cellIn.getCellFormula()); break; case Cell.CELL_TYPE_NUMERIC: cellOut.setCellValue(cellIn.getNumericCellValue()); break; case Cell.CELL_TYPE_STRING: cellOut.setCellValue(cellIn.getStringCellValue()); break; } { CellStyle styleIn = cellIn.getCellStyle(); CellStyle styleOut = cellOut.getCellStyle(); styleOut.setDataFormat(styleIn.getDataFormat()); } cellOut.setCellComment(cellIn.getCellComment()); // HSSFCellStyle cannot be cast to XSSFCellStyle // cellOut.setCellStyle(cellIn.getCellStyle()); } } } OutputStream out = new BufferedOutputStream(new FileOutputStream(outFn)); try { wbOut.write(out); } finally { out.close(); } } finally { in.close(); } }
From source file:gov.va.isaac.isaacDbProcessingRules.spreadsheet.SpreadsheetReader.java
License:Apache License
private static String toString(Cell cell) { switch (cell.getCellType()) { case Cell.CELL_TYPE_BLANK: return ""; case Cell.CELL_TYPE_BOOLEAN: return cell.getBooleanCellValue() + ""; case Cell.CELL_TYPE_NUMERIC: return cell.getNumericCellValue() + ""; case Cell.CELL_TYPE_STRING: return cell.getStringCellValue(); case Cell.CELL_TYPE_ERROR: return "_ERROR_ " + cell.getErrorCellValue(); case Cell.CELL_TYPE_FORMULA: return cell.getCellFormula() + ""; default:/*from w ww . jav a 2s . c om*/ throw new RuntimeException("No toString is available for the cell type!"); } }
From source file:hu.webhejj.commons.io.table.excel.ExcelRowValueConverter.java
License:Apache License
public <T> T getValue(Row row, int column, Class<T> valueType) { if (row == null) { return null; }//from ww w . j a va 2 s. co m Cell cell = row.getCell(column); if (cell == null) { return null; } CellValue cellValue = getCellValue(row, cell, column); if (cellValue == null) { return null; } switch (cellValue.getCellType()) { case Cell.CELL_TYPE_BLANK: return null; case Cell.CELL_TYPE_BOOLEAN: if (String.class.isAssignableFrom(valueType)) { return (T) Boolean.toString(cellValue.getBooleanValue()); } else if (Integer.class.isAssignableFrom(valueType)) { return (T) Integer.valueOf(cellValue.getBooleanValue() ? 1 : 0); } else if (Long.class.isAssignableFrom(valueType)) { return (T) Long.valueOf(cellValue.getBooleanValue() ? 1L : 0L); } else { throw new ClassCastException( "Can't convert " + cellValue.getBooleanValue() + " to " + valueType.getName()); } case Cell.CELL_TYPE_STRING: String stringValue = cellValue.getStringValue(); if (CompareUtils.isEmpty(stringValue)) { return null; } if ("null".equals(stringValue)) { return null; } if (String.class.isAssignableFrom(valueType)) { return (T) stringValue; } else if (Integer.class.isAssignableFrom(valueType)) { return (T) Integer.valueOf(stringValue); } else if (Long.class.isAssignableFrom(valueType)) { return (T) Long.valueOf(stringValue); } else if (valueType.isEnum()) { return (T) Enum.valueOf((Class<? extends Enum>) valueType, stringValue); } else if (BigDecimal.class.isAssignableFrom(valueType)) { return (T) (CompareUtils.isEmpty(stringValue) ? null : new BigDecimal(stringValue)); } else if (Boolean.class.isAssignableFrom(valueType)) { return (T) Boolean.valueOf("true".equalsIgnoreCase(stringValue) || (!CompareUtils.isEmpty(stringValue) && !"0".equals(stringValue))); } else { throw new ClassCastException("Can't convert " + stringValue + " to " + valueType.getName()); } case Cell.CELL_TYPE_NUMERIC: if (String.class.isAssignableFrom(valueType)) { Format format = formatter.createFormat(cell); if (format == null) { // TODO: do this without creating a BigDecimal each time return (T) new BigDecimal(cellValue.getNumberValue()).toString(); } else { return (T) format.format(cellValue.getNumberValue()); } } else if (Integer.class.isAssignableFrom(valueType)) { return (T) Integer.valueOf((int) cellValue.getNumberValue()); } else if (Long.class.isAssignableFrom(valueType)) { return (T) Long.valueOf((int) cellValue.getNumberValue()); } else { throw new ClassCastException( "Can't convert " + cellValue.getNumberValue() + " to " + valueType.getName()); } case Cell.CELL_TYPE_ERROR: FormulaError error = FormulaError.forInt(cell.getErrorCellValue()); if (FormulaError.NA.equals(error)) { return null; } else { // System.err.format(" Cell[%d,%d] error code %s\n", r.getRowNum(), column, error); return null; // throw new RuntimeException(String.format("Cell[%d,%d] error code %s", r.getRowNum(), column, error)); } } throw new IllegalArgumentException("Don't know how to convert cell of type " + cellValue.getCellType()); }
From source file:jexcel4py.Jexcel4py.java
private void copyCell(Cell rdCell, Cell wrCell) { // wrCell.setCellStyle(rdCell.getCellStyle()); // wrCell.setCellType(rdCell.getCellType()); int cellStyle = rdCell.getCellType(); String rdCellValue = rdCell.getStringCellValue(); switch (cellStyle) { case Cell.CELL_TYPE_BLANK: wrCell.setCellValue("heys"); break;// w ww. j a va2s . c o m case Cell.CELL_TYPE_BOOLEAN: wrCell.setCellValue(rdCell.getBooleanCellValue()); break; case Cell.CELL_TYPE_ERROR: wrCell.setCellValue(rdCell.getErrorCellValue()); break; case Cell.CELL_TYPE_FORMULA: wrCell.setCellValue(rdCell.getCellFormula()); break; case Cell.CELL_TYPE_NUMERIC: wrCell.setCellValue(rdCell.getNumericCellValue()); break; case Cell.CELL_TYPE_STRING: wrCell.setCellValue(rdCell.getStringCellValue()); break; default: wrCell.setCellValue("heys"); break; } }
From source file:jp.qpg.Tool.java
License:Apache License
/** * get cell value// w w w . j a va 2 s.c o m * * @param cell cell * @return cell value */ public static Optional<String> cellValue(Cell cell) { String text = null; try { text = formatter.formatAsString(cell, Locale.JAPAN); } catch (IllegalStateException e) { switch (cell.getCellType()) { case BOOLEAN: text = String.valueOf(cell.getBooleanCellValue()); break; case STRING: text = cell.getStringCellValue(); break; case NUMERIC: text = String.valueOf(cell.getNumericCellValue()); break; case ERROR: text = String.valueOf(cell.getErrorCellValue()); break; case BLANK: break; case FORMULA: break; case _NONE: break; } } return Optional.ofNullable(text).filter(((Predicate<String>) String::isEmpty).negate()); }
From source file:jp.ryoyamamoto.poiutils.Cells.java
License:Apache License
private static void copyCellValue(Cell source, Cell target) { switch (source.getCellType()) { case Cell.CELL_TYPE_NUMERIC: target.setCellValue(source.getNumericCellValue()); break;/*from w ww . ja va2 s .co m*/ case Cell.CELL_TYPE_STRING: target.setCellValue(source.getRichStringCellValue()); break; case Cell.CELL_TYPE_FORMULA: target.setCellFormula(source.getCellFormula()); break; case Cell.CELL_TYPE_BLANK: break; case Cell.CELL_TYPE_BOOLEAN: target.setCellValue(source.getBooleanCellValue()); break; case Cell.CELL_TYPE_ERROR: target.setCellErrorValue(source.getErrorCellValue()); break; } }
From source file:magicware.scm.redmine.tools.util.ExcelUtils.java
License:Apache License
public static String getCellContent(Cell cell, FormulaEvaluator evaluator) { String result = null;/*from w w w . j a va 2s .c o m*/ switch (cell.getCellType()) { case Cell.CELL_TYPE_STRING: result = cell.getRichStringCellValue().getString(); break; case Cell.CELL_TYPE_NUMERIC: if (DateUtil.isCellDateFormatted(cell)) { result = Constants.DATE_FORMAT.format(cell.getDateCellValue()); } else { result = String.valueOf(Double.valueOf(cell.getNumericCellValue()).intValue()); } break; case Cell.CELL_TYPE_BOOLEAN: result = String.valueOf(cell.getBooleanCellValue()); break; case Cell.CELL_TYPE_FORMULA: switch (evaluator.evaluateFormulaCell(cell)) { case Cell.CELL_TYPE_BOOLEAN: result = String.valueOf(cell.getBooleanCellValue()); break; case Cell.CELL_TYPE_NUMERIC: if (DateUtil.isCellDateFormatted(cell)) { result = Constants.DATE_FORMAT.format(cell.getDateCellValue()); } else { result = String.valueOf(Double.valueOf(cell.getNumericCellValue()).intValue()); } break; case Cell.CELL_TYPE_STRING: result = String.valueOf(cell.getStringCellValue()); break; case Cell.CELL_TYPE_BLANK: break; case Cell.CELL_TYPE_ERROR: result = String.valueOf(cell.getErrorCellValue()); break; case Cell.CELL_TYPE_FORMULA: break; } break; default: break; } return result; }
From source file:main.KeywordList.java
private String getCellDate(Cell cell) { switch (cell.getCellType()) { case CELL_TYPE_NUMERIC: if (DateUtil.isCellDateFormatted(cell)) { return ForcastUi.dateToString(cell.getDateCellValue()); } else {/* w w w .ja v a 2 s.c o m*/ return "01/01/2000"; } case CELL_TYPE_STRING: return cell.getStringCellValue(); case CELL_TYPE_FORMULA: return cell.getCellFormula(); case CELL_TYPE_BLANK: return ""; case CELL_TYPE_BOOLEAN: return Boolean.toString(cell.getBooleanCellValue()); case CELL_TYPE_ERROR: return Byte.toString(cell.getErrorCellValue()); default: return "01/01/2000"; } }
From source file:main.KeywordList.java
private String getCellValue(Cell cell) { switch (cell.getCellType()) { case CELL_TYPE_NUMERIC: return Double.toString(cell.getNumericCellValue()); case CELL_TYPE_STRING: return cell.getStringCellValue(); case CELL_TYPE_FORMULA: return cell.getCellFormula(); case CELL_TYPE_BLANK: return ""; case CELL_TYPE_BOOLEAN: return Boolean.toString(cell.getBooleanCellValue()); case CELL_TYPE_ERROR: return Byte.toString(cell.getErrorCellValue()); default:/*from w w w . ja v a 2s.c o m*/ return ""; } }
From source file:midas.sheeco.exceptions.SpreadsheetViolation.java
License:Apache License
private static Object getCellValue(final Cell cell) { switch (cell.getCellType()) { case Cell.CELL_TYPE_NUMERIC: if (DateUtil.isCellDateFormatted(cell)) { return cell.getDateCellValue(); }/*from w ww . ja va 2 s. c o m*/ return cell.getNumericCellValue(); case Cell.CELL_TYPE_STRING: return cell.getStringCellValue(); case Cell.CELL_TYPE_BLANK: return null; case Cell.CELL_TYPE_BOOLEAN: return cell.getBooleanCellValue(); case Cell.CELL_TYPE_ERROR: return cell.getErrorCellValue(); case Cell.CELL_TYPE_FORMULA: return cell.getCellFormula(); } throw new UnsupportedOperationException("CellType " + cell.getCellType() + " is invalid"); }