List of usage examples for org.apache.poi.ss.usermodel Cell getRichStringCellValue
RichTextString getRichStringCellValue();
For numeric cells we throw an exception.
From source file:com.b2international.snowowl.snomed.importer.net4j.SnomedSubsetImportUtil.java
License:Apache License
private String getStringValue(final Cell cell) { String value = ""; // empty cell if (cell == null) { return ""; }/* w w w. ja va 2s . c o m*/ switch (cell.getCellType()) { case Cell.CELL_TYPE_STRING: value = cell.getRichStringCellValue().getString(); break; case Cell.CELL_TYPE_NUMERIC: if (DateUtil.isCellDateFormatted(cell)) { value = cell.getDateCellValue().toString(); } else { value = Integer.toString((int) cell.getNumericCellValue()); } break; case Cell.CELL_TYPE_BOOLEAN: value = Boolean.toString(cell.getBooleanCellValue()); break; case Cell.CELL_TYPE_FORMULA: value = cell.getCellFormula(); break; } return value; }
From source file:com.bayareasoftware.chartengine.ds.ExcelDataStream.java
License:Apache License
private Object getCellData(Cell cell, int rowNum, int type, int index) { evaluator.evaluate(cell);/*from w w w .j av a 2 s . co m*/ if (cell == null || cell.getCellType() == HSSFCell.CELL_TYPE_BLANK || type == UNKNOWN) { return null; } //p("getting from row#" + rowNum + " col#" + cell.getCellNum()); switch (type) { case STRING: return getCellString(cell); case DATE: { if (ExcelInference.isCellDateFormatted(cell)) { Date d = cell.getDateCellValue(); if (d != null) { return d; } } // figure out date format Date ret = null; String dstr = getCellString(cell); if (dfmts[index - 1] != null) { ret = this.parseDate(dstr, dfmts[index - 1], -1, index); } else if (metadata.getColumnFormat(index) != null) { dfmts[index - 1] = DateUtil.createDateFormat(metadata.getColumnFormat(index)); ret = this.parseDate(dstr, dfmts[index - 1], -1, index); } else if (dstr != null) { if (dateRecognizer == null) { dateRecognizer = new DateRecognizer(); } dateRecognizer.reset(); dateRecognizer.parse(dstr); if (!dateRecognizer.failed()) { dfmts[index - 1] = dateRecognizer.getSimpleDateFormat(); ret = this.parseDate(dstr, dfmts[index - 1], -1, index); } } return ret; } case DOUBLE: return cell.getNumericCellValue(); case INTEGER: double d = cell.getNumericCellValue(); return new Double(d).intValue(); case BOOLEAN: String s = cell.getRichStringCellValue().getString(); return "true".equalsIgnoreCase(s); case IGNORE: return null; default: throw new AssertionError("unexpected type: " + DataType.toString(type)); } }
From source file:com.bayareasoftware.chartengine.ds.util.ExcelInference.java
License:Apache License
public static String getCellString(Cell cell, HSSFFormulaEvaluator eval, DateFormat dfmt) { if (cell == null) { return null; }/*from w w w. ja v a 2s.co m*/ String ret = null; eval.evaluate(cell); switch (cell.getCellType()) { case HSSFCell.CELL_TYPE_NUMERIC: case HSSFCell.CELL_TYPE_FORMULA: // ? if (isCellDateFormatted(cell)) { if (dfmt == null) { dfmt = new SimpleDateFormat("yyyy-MM-dd"); } Date d = cell.getDateCellValue(); if (d != null) { ret = dfmt.format(d); } else { ret = ""; } } else { try { ret = "" + cell.getNumericCellValue(); } catch (IllegalStateException ise) { int errVal = cell.getErrorCellValue(); String formula = cell.getCellFormula(); int cacheType = cell.getCachedFormulaResultType(); throw new RuntimeException(ise.getMessage() + ": errVal=" + errVal + " formula='" + formula + "' cacheType=" + cacheType); } } break; case HSSFCell.CELL_TYPE_BLANK: ret = null; break; case HSSFCell.CELL_TYPE_BOOLEAN: ret = "" + cell.getBooleanCellValue(); break; case HSSFCell.CELL_TYPE_STRING: default: ret = cell.getRichStringCellValue().getString(); } return ret; }
From source file:com.beyondb.io.ExcelControl.java
@Override public Object[][] readTableContent() throws IOException, InvalidFormatException, Exception { try {// w w w .ja v a 2 s . co m //OPCPackage pkg = OPCPackage.open(file); // InputStream m_InputStream = new FileInputStream(m_File); Sheet sheet = null; // if (!m_InputStream.markSupported()) { // m_InputStream = new PushbackInputStream(m_InputStream, 8); // } // if (POIFSFileSystem.hasPOIFSHeader(m_InputStream)) { // HSSFWorkbook hSSFWorkbook = new HSSFWorkbook(m_InputStream); // sheet = (Sheet)hSSFWorkbook.getSheetAt(0); // // } else if (POIXMLDocument.hasOOXMLHeader(m_InputStream)) { // XSSFWorkbook xSSFWorkbook = new XSSFWorkbook(OPCPackage.open(m_File)); // sheet = (Sheet)xSSFWorkbook.getSheetAt(0); // } // else { // throw new IllegalArgumentException("excel?poi??"); // } sheet = getSheet(); if (sheet != null) { if (sheet.getLastRowNum() == 0) { throw new Exception("Excel"); } //? m_RowNum = sheet.getLastRowNum() + 1; // m_ColumnNum = sheet.getRow(0).getPhysicalNumberOfCells(); m_ColumnNum = sheet.getRow(0).getLastCellNum(); m_TableStr = new Object[m_RowNum][m_ColumnNum]; for (int rindex = 0; rindex < m_RowNum; rindex++) { Row row = sheet.getRow(rindex); for (int cindex = 0; cindex < m_ColumnNum; cindex++) { Cell cell = row.getCell(cindex); if (cell == null) { m_TableStr[rindex][cindex] = ""; } else { String value = ""; switch (cell.getCellType()) { case Cell.CELL_TYPE_STRING: // System.out.println(cell.getRichStringCellValue().getString()); value = cell.getRichStringCellValue().getString().replace("\n", ""); break; case Cell.CELL_TYPE_NUMERIC: if (DateUtil.isCellDateFormatted(cell)) { // System.out.println(cell.getDateCellValue()); value = cell.getDateCellValue().toString(); } else { DecimalFormat df = new DecimalFormat("#"); value = String.valueOf(cell.getNumericCellValue()); double d = cell.getNumericCellValue(); int dInt = (int) d; BigDecimal b1 = new BigDecimal(value); BigDecimal b2 = new BigDecimal(Integer.toString(dInt)); double dPoint = b1.subtract(b2).doubleValue(); if (dPoint == 0) { //? value = df.format(cell.getNumericCellValue()); } } break; case Cell.CELL_TYPE_BOOLEAN: // System.out.println(cell.getBooleanCellValue()); value = cell.getBooleanCellValue() + ""; break; case Cell.CELL_TYPE_FORMULA: // System.out.println(cell.getCellFormula()); value = cell.getCellFormula(); break; case Cell.CELL_TYPE_BLANK: value = ""; default: // System.out.println(); value = ""; } m_TableStr[row.getRowNum()][cell.getColumnIndex()] = value; } } } } } catch (IOException | InvalidFormatException e) { Logger.getLogger(ExcelControl.class.getName()).log(Level.SEVERE, "excel??", e); throw e; } catch (Exception ex) { Logger.getLogger(ExcelControl.class.getName()).log(Level.SEVERE, "excel??", ex); throw ex; } finally { m_InputStream.close(); } return m_TableStr; }
From source file:com.canoo.webtest.plugins.exceltest.ExcelCellUtils.java
License:Open Source License
public static String getCellValueAt(final Cell cell) { if (null == cell) { return ""; }/*from w ww. java2 s . c o m*/ switch (cell.getCellType()) { case Cell.CELL_TYPE_STRING: return cell.getRichStringCellValue().getString(); case Cell.CELL_TYPE_NUMERIC: return asStringTrimInts(cell.getNumericCellValue()); case Cell.CELL_TYPE_BLANK: return ""; case Cell.CELL_TYPE_BOOLEAN: return String.valueOf(cell.getBooleanCellValue()); case Cell.CELL_TYPE_FORMULA: return cell.getCellFormula(); case Cell.CELL_TYPE_ERROR: return "Error Code " + String.valueOf(cell.getErrorCellValue() & 0xFF); ///CLOVER:OFF there are currently no other types. Potentially more in future? default: LOG.warn("Cell Type not supported: " + cell.getCellType()); return ""; ///CLOVER:ON } }
From source file:com.cn.util.Units.java
/** * ?Cell?/* www. ja va 2s . c o m*/ * * @param cell * @return */ public static String getCellFormatValue(Cell cell) { String cellvalue = ""; if (cell != null) { // ?CellType switch (cell.getCellType()) { // ?CellTypeNUMERIC case Cell.CELL_TYPE_NUMERIC: case Cell.CELL_TYPE_FORMULA: { // ?cell?Date if (DateUtil.isCellDateFormatted(cell)) { // DateData? //1?data?2011-10-12 0:00:00 //cellvalue = cell.getDateCellValue().toLocaleString(); //2?data??2011-10-12 Date date = cell.getDateCellValue(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); cellvalue = sdf.format(date); } // else { // ??Cell cellvalue = String.valueOf(cell.getNumericCellValue()); } break; } // ?CellTypeSTRIN case Cell.CELL_TYPE_STRING: // ??Cell cellvalue = cell.getRichStringCellValue().getString(); break; // Cell default: cellvalue = " "; } } else { cellvalue = ""; } return cellvalue; }
From source file:com.coast.controler.Controler.java
public static ArrayList<Product> readProductsFromMyExcel(String file, ResultMSG resultMSG) throws Exception { ArrayList<Product> products = new ArrayList<Product>(); InputStream is = null;//from w w w. j a va2 s . c o m int sum = 0; int row = 1; try { File f = new File(file); is = new FileInputStream(f); Workbook wb = WorkbookFactory.create(is); Sheet sheet = wb.getSheetAt(0); POIUtil poiUtil = new POIUtil(); int lastRowNum = sheet.getLastRowNum(); while (row <= lastRowNum) { //?? Cell firstCell = sheet.getRow(row).getCell(0); if (firstCell == null) break; if (firstCell.getRichStringCellValue().toString().toUpperCase() == "") break; //fullsn Cell fullSnCell = sheet.getRow(row).getCell(0); String fullSn = poiUtil.getCellContentToString(fullSnCell); int len = fullSn.length(); String snCode = fullSn.substring(0, len - 3); String colorCode = fullSn.substring(len - 3, len - 1); String sizeCode = fullSn.substring(len - 1, len); // String sizeRegex = convertSizeToRegex(sizeCode); //type Cell typeCell = sheet.getRow(row).getCell(1); String type = poiUtil.getCellContentToString(typeCell); //color Cell colorCell = sheet.getRow(row).getCell(2); String color = poiUtil.getCellContentToString(colorCell); //size Cell sizeCell = sheet.getRow(row).getCell(3); String size = poiUtil.getCellContentToString(sizeCell); //price Cell priceCell = sheet.getRow(row).getCell(5); String orgPrice = poiUtil.getCellContentToString(priceCell); //amount Cell amountCell = sheet.getRow(row).getCell(10); int amount = Integer.parseInt(poiUtil.getCellContentToString(amountCell)); //Porduct Product product = new Product(fullSn, snCode, colorCode, sizeCode, type, color, size, orgPrice, amount); products.add(product); sum += product.getAmount(); row++; } resultMSG.setReadMessage("??,:" + sum + "!"); } catch (Exception e) { System.out.println( "readProductsFromMyExcel:=" + row + "=?" + e.toString()); products = null; e.printStackTrace(); resultMSG.setReadMessage("?,:" + sum + "!:" + e.toString()); } finally { is.close(); System.out.println("?:" + sum); return products; } }
From source file:com.crimelab.service.ChemistryServiceImpl.java
public Cell findCell(Sheet sheet, String cellContent) { for (Row row : sheet) { for (Cell cell : row) { if (cell.getCellType() == Cell.CELL_TYPE_STRING) { if (cell.getRichStringCellValue().getString().trim().equals(cellContent)) { return cell; }/*w w w . j av a 2 s. c o m*/ } } } return null; }
From source file:com.dituiba.excel.DefaultInputAdapter.java
License:Apache License
public static Object getCellValue(Cell cell, Class type) throws AdapterException { if (cell == null) return ""; try {// ww w .j a va2 s.c om switch (cell.getCellType()) { case Cell.CELL_TYPE_BOOLEAN: return cell.getBooleanCellValue(); case Cell.CELL_TYPE_BLANK: return ""; case Cell.CELL_TYPE_NUMERIC: if (String.class.isAssignableFrom(type)) { cell.setCellType(Cell.CELL_TYPE_STRING); return trim(cell.getStringCellValue()); } else if (Date.class.isAssignableFrom(type)) { return cell.getDateCellValue(); } return cell.getNumericCellValue(); case Cell.CELL_TYPE_STRING: return trim(cell.getStringCellValue()); case Cell.CELL_TYPE_FORMULA: try { return String.valueOf(cell.getNumericCellValue()); } catch (IllegalStateException e) { return trim(String.valueOf(cell.getRichStringCellValue())); } } } catch (Exception e) { throw new AdapterException(Message.INPUT_CELL_DATA_ERROR, cell); } throw new AdapterException(Message.INPUT_CELL_DATA_ERROR, cell); }
From source file:com.dituiba.excel.ExcelUtility.java
License:Apache License
public static void copyCell(Cell srcCell, Cell distCell) { distCell.setCellStyle(srcCell.getCellStyle()); if (srcCell.getCellComment() != null) { distCell.setCellComment(srcCell.getCellComment()); }//from w w w .j a va 2s. c om int srcCellType = srcCell.getCellType(); distCell.setCellType(srcCellType); if (srcCellType == Cell.CELL_TYPE_NUMERIC) { if (DateUtil.isCellDateFormatted(srcCell)) { distCell.setCellValue(srcCell.getDateCellValue()); } else { distCell.setCellValue(srcCell.getNumericCellValue()); } } else if (srcCellType == Cell.CELL_TYPE_STRING) { distCell.setCellValue(srcCell.getRichStringCellValue()); } else if (srcCellType == Cell.CELL_TYPE_BLANK) { //nothing } else if (srcCellType == Cell.CELL_TYPE_BOOLEAN) { distCell.setCellValue(srcCell.getBooleanCellValue()); } else if (srcCellType == Cell.CELL_TYPE_ERROR) { distCell.setCellErrorValue(srcCell.getErrorCellValue()); } else if (srcCellType == Cell.CELL_TYPE_FORMULA) { distCell.setCellFormula(srcCell.getCellFormula()); } else { //nothing } }