List of usage examples for org.apache.poi.hssf.usermodel HSSFCell getStringCellValue
public String getStringCellValue()
From source file:org.apache.directory.studio.ldapbrowser.core.jobs.ExportXlsRunnable.java
License:Apache License
/** * {@inheritDoc}/*from www. j a v a 2 s. c om*/ */ public void run(StudioProgressMonitor monitor) { monitor.beginTask(BrowserCoreMessages.jobs__export_xls_task, 2); monitor.reportProgress(" "); //$NON-NLS-1$ monitor.worked(1); Preferences coreStore = BrowserCorePlugin.getDefault().getPluginPreferences(); String valueDelimiter = coreStore.getString(BrowserCoreConstants.PREFERENCE_FORMAT_XLS_VALUEDELIMITER); int binaryEncoding = coreStore.getInt(BrowserCoreConstants.PREFERENCE_FORMAT_XLS_BINARYENCODING); HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet = wb.createSheet("Export"); //$NON-NLS-1$ // header HSSFRow headerRow = sheet.createRow(0); LinkedHashMap<String, Integer> attributeNameMap = new LinkedHashMap<String, Integer>(); if (this.exportDn) { int cellNum = 0; attributeNameMap.put("dn", cellNum); //$NON-NLS-1$ createStringCell(headerRow, cellNum).setCellValue("dn"); //$NON-NLS-1$ } // max export if (searchParameter.getCountLimit() < 1 || searchParameter.getCountLimit() > MAX_COUNT_LIMIT) { searchParameter.setCountLimit(MAX_COUNT_LIMIT); } // export try { int count = 0; exportToXls(browserConnection, searchParameter, sheet, headerRow, count, monitor, attributeNameMap, valueDelimiter, binaryEncoding, this.exportDn); } catch (Exception e) { monitor.reportError(e); } // column width for (int i = 0; i <= sheet.getLastRowNum(); i++) { HSSFRow row = sheet.getRow(i); for (short j = 0; row != null && j <= row.getLastCellNum(); j++) { HSSFCell cell = row.getCell(j); if (cell != null && cell.getCellType() == HSSFCell.CELL_TYPE_STRING) { String value = cell.getStringCellValue(); if ((short) (value.length() * 256 * 1.1) > sheet.getColumnWidth(j)) { sheet.setColumnWidth(j, (short) (value.length() * 256 * 1.1)); } } } } try { FileOutputStream fileOut = new FileOutputStream(exportXlsFilename); wb.write(fileOut); fileOut.close(); } catch (Exception e) { monitor.reportError(e); } }
From source file:org.apache.jackrabbit.extractor.MsExcelTextExtractor.java
License:Apache License
/** * {@inheritDoc}// w w w . j a v a 2 s . c o m */ public Reader extractText(InputStream stream, String type, String encoding) throws IOException { CharArrayWriter writer = new CharArrayWriter(); try { POIFSFileSystem fs = new POIFSFileSystem(stream); HSSFWorkbook workbook = new HSSFWorkbook(fs); for (int i = 0; i < workbook.getNumberOfSheets(); i++) { HSSFSheet sheet = workbook.getSheetAt(i); Iterator rows = sheet.rowIterator(); while (rows.hasNext()) { HSSFRow row = (HSSFRow) rows.next(); Iterator cells = row.cellIterator(); while (cells.hasNext()) { HSSFCell cell = (HSSFCell) cells.next(); switch (cell.getCellType()) { case HSSFCell.CELL_TYPE_NUMERIC: String num = Double.toString(cell.getNumericCellValue()).trim(); if (num.length() > 0) { writer.write(num + " "); } break; case HSSFCell.CELL_TYPE_STRING: String text = cell.getStringCellValue().trim(); if (text.length() > 0) { writer.write(text + " "); } break; } } } } return new CharArrayReader(writer.toCharArray()); } catch (RuntimeException e) { logger.warn("Failed to extract Excel text content", e); return new StringReader(""); } finally { stream.close(); } }
From source file:org.apache.nutch.parse.msexcel.ExcelExtractor.java
License:Apache License
protected String extractText(InputStream input) throws Exception { StringBuffer resultText = new StringBuffer(SBUF_SIZE); // TODO FIXME MC HSSFWorkbook wb = new HSSFWorkbook(input); if (wb == null) { return resultText.toString(); }// w w w .ja v a2 s .com HSSFSheet sheet; HSSFRow row; HSSFCell cell; int sNum = 0; int rNum = 0; int cNum = 0; sNum = wb.getNumberOfSheets(); for (int i = 0; i < sNum; i++) { if ((sheet = wb.getSheetAt(i)) == null) { continue; } rNum = sheet.getLastRowNum(); for (int j = 0; j <= rNum; j++) { if ((row = sheet.getRow(j)) == null) { continue; } cNum = row.getLastCellNum(); for (int k = 0; k < cNum; k++) { if ((cell = row.getCell((short) k)) != null) { /*if(HSSFDateUtil.isCellDateFormatted(cell) == true) { resultText += cell.getDateCellValue().toString() + " "; } else */ if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) { resultText.append(cell.getStringCellValue()).append(" "); } else if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) { Double d = new Double(cell.getNumericCellValue()); resultText.append(d.toString()).append(" "); } /* else if(cell.getCellType() == HSSFCell.CELL_TYPE_FORMULA){ resultText += cell.getCellFormula() + " "; } */ } } } } return resultText.toString(); }
From source file:org.apache.slide.extractor.MSExcelExtractor.java
License:Apache License
public Reader extract(InputStream content) throws ExtractorException { try {/* w w w . jav a2 s . co m*/ CharArrayWriter writer = new CharArrayWriter(); POIFSFileSystem fs = new POIFSFileSystem(content); HSSFWorkbook workbook = new HSSFWorkbook(fs); for (int i = 0; i < workbook.getNumberOfSheets(); i++) { HSSFSheet sheet = workbook.getSheetAt(i); Iterator rows = sheet.rowIterator(); while (rows.hasNext()) { HSSFRow row = (HSSFRow) rows.next(); Iterator cells = row.cellIterator(); while (cells.hasNext()) { HSSFCell cell = (HSSFCell) cells.next(); switch (cell.getCellType()) { case HSSFCell.CELL_TYPE_NUMERIC: String num = Double.toString(cell.getNumericCellValue()).trim(); if (num.length() > 0) writer.write(num + " "); break; case HSSFCell.CELL_TYPE_STRING: String text = cell.getStringCellValue().trim(); if (text.length() > 0) writer.write(text + " "); break; } } } } return new CharArrayReader(writer.toCharArray()); } catch (Exception e) { throw new ExtractorException(e.getMessage()); } }
From source file:org.apdplat.platform.generator.ModelGenerator.java
License:Open Source License
/** * ???EXCEL/*from w w w. j av a2s. c om*/ * ?EXCELJAVA * @param inputStream ??EXCEL * @return JAVA */ private static List<ModelInfo> readModelInfos(InputStream inputStream) { List<ModelInfo> models = new ArrayList<>(); try { HSSFWorkbook workbook = new HSSFWorkbook(inputStream); for (int i = 0; i < workbook.getNumberOfSheets(); i++) { HSSFSheet sheet = workbook.getSheetAt(i); try { HSSFRow row = sheet.getRow(2); if (row == null) { LOG.info("???" + sheet.getSheetName()); continue; } HSSFCell cell = row.getCell(1); //?? String modelPackage = cell.getStringCellValue(); row = sheet.getRow(3); cell = row.getCell(1); //?? String modelEnglish = cell.getStringCellValue(); row = sheet.getRow(4); cell = row.getCell(1); //?? String modelChinese = cell.getStringCellValue(); ModelInfo modelInfo = new ModelInfo(); modelInfo.setModelPackage(modelPackage); modelInfo.setModelEnglish(modelEnglish); modelInfo.setModelChinese(modelChinese); int rows = sheet.getPhysicalNumberOfRows(); //8?? for (int rowNumber = 7; rowNumber < rows; rowNumber++) { HSSFRow oneRow = sheet.getRow(rowNumber); if (oneRow == null) { continue; } Attr attr = new Attr(); //?? HSSFCell oneCell = oneRow.getCell(0); if (oneCell != null) { String cellValue = oneCell.getStringCellValue(); if (cellValue != null && !"".equals(cellValue.trim()) && !"null".equals(cellValue.trim().toLowerCase())) { attr.setDes(cellValue); } else { continue; } } //?? oneCell = oneRow.getCell(1); if (oneCell != null) { String cellValue = oneCell.getStringCellValue(); if (cellValue != null && !"".equals(cellValue.trim()) && !"null".equals(cellValue.trim().toLowerCase())) { attr.setName(cellValue); } else { continue; } } // oneCell = oneRow.getCell(2); if (oneCell != null) { String cellValue = oneCell.getStringCellValue(); if (cellValue != null && !"".equals(cellValue.trim()) && !"null".equals(cellValue.trim().toLowerCase())) { attr.setType(AttrType.validType(cellValue)); } else { attr.setType(AttrType.validType("String")); } } //?string oneCell = oneRow.getCell(3); if (oneCell != null) { if (oneCell.getCellType() == HSSFCell.CELL_TYPE_STRING) { String cellValue = oneCell.getStringCellValue(); if (cellValue != null && !"".equals(cellValue.trim()) && !"null".equals(cellValue.trim().toLowerCase())) { try { int length = Integer.parseInt(cellValue); attr.setLength(length); } catch (Exception e) { LOG.error("?" + cellValue); } } } if (oneCell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) { double length = oneCell.getNumericCellValue(); attr.setLength((int) length); } } //?? oneCell = oneRow.getCell(4); if (oneCell != null) { boolean cellValue = oneCell.getBooleanCellValue(); attr.setSearchable(cellValue); } //?? oneCell = oneRow.getCell(5); if (oneCell != null) { boolean cellValue = oneCell.getBooleanCellValue(); attr.setRenderIgnore(cellValue); } //?? oneCell = oneRow.getCell(6); if (oneCell != null) { String cellValue = oneCell.getStringCellValue(); if (cellValue != null && !"".equals(cellValue.trim()) && !"null".equals(cellValue.trim().toLowerCase())) { attr.setDic(DicType.validType(cellValue)); } } //??? oneCell = oneRow.getCell(7); if (oneCell != null) { String cellValue = oneCell.getStringCellValue(); if (cellValue != null && !"".equals(cellValue.trim()) && !"null".equals(cellValue.trim().toLowerCase())) { attr.setDicName(cellValue); } } // oneCell = oneRow.getCell(8); if (oneCell != null) { String cellValue = oneCell.getStringCellValue(); if (cellValue != null && !"".equals(cellValue.trim()) && !"null".equals(cellValue.trim().toLowerCase())) { attr.setMap(MapType.validType(cellValue)); } } // oneCell = oneRow.getCell(9); if (oneCell != null) { String cellValue = oneCell.getStringCellValue(); if (cellValue != null && !"".equals(cellValue.trim()) && !"null".equals(cellValue.trim().toLowerCase())) { attr.setAttrRef(cellValue); } } if ("Date".equals(attr.getType()) || "Time".equals(attr.getType())) { modelInfo.setHasDateTime(true); } if ("DicItem".equals(attr.getType())) { modelInfo.setHasDicItem(true); //DicItemdicSimpleDic if (!"SimpleDic".equals(attr.getDic()) && !"TreeDic".equals(attr.getDic())) { attr.setDic("SimpleDic"); } if (attr.getDicName() == null || "".equals(attr.getDicName())) { attr.setDicName(attr.getName()); } } modelInfo.addAttr(attr); } models.add(modelInfo); } catch (Exception e) { LOG.error("?:" + sheet.getSheetName() + " ", e); } } } catch (IOException e) { LOG.error("?MODEL", e); } return models; }
From source file:org.databene.platform.xls.XLSEntityExporterTest.java
License:Open Source License
private static void checkCells(HSSFRow row, Object... values) { if (ArrayUtil.isEmpty(values)) assertNull(row);//from www. j a va 2 s. c o m for (int i = 0; i < values.length; i++) { HSSFCell cell = row.getCell(i); Object expectedContent = values[i]; if (expectedContent == null) assertNull(cell); else if (expectedContent instanceof String) { assertEquals(HSSFCell.CELL_TYPE_STRING, cell.getCellType()); assertEquals(expectedContent, cell.getStringCellValue()); } else if (expectedContent instanceof Number) { assertEquals(HSSFCell.CELL_TYPE_NUMERIC, cell.getCellType()); assertEquals(((Number) expectedContent).doubleValue(), cell.getNumericCellValue()); } else if (expectedContent instanceof Boolean) { assertEquals(HSSFCell.CELL_TYPE_BOOLEAN, cell.getCellType()); assertEquals(expectedContent, cell.getBooleanCellValue()); } else if (expectedContent instanceof Date) { assertEquals(HSSFCell.CELL_TYPE_NUMERIC, cell.getCellType()); assertEquals(((Date) expectedContent).getTime() / 1000, cell.getDateCellValue().getTime() / 1000); // cut off milliseconds } else throw new RuntimeException("Type not supported: " + expectedContent.getClass()); } }
From source file:org.devgateway.eudevfin.importing.metadata.streamprocessors.ExcelStreamProcessor.java
License:Open Source License
@SuppressWarnings({ "rawtypes", "unchecked" }) private Object generateObject(final MapperInterface<?> mapper, final HSSFRow row) throws ClassNotFoundException, InstantiationException, IllegalAccessException { boolean allCellsAreNull = true; final List<String> values = new ArrayList<String>(); for (int j = OFFSET; j < this.metadataInfoList.size() + OFFSET; j++) { final HSSFCell cell = row.getCell(j); if (cell != null) { allCellsAreNull = false;//from ww w .j a v a 2s. com String val = null; if (HSSFCell.CELL_TYPE_STRING == cell.getCellType()) { val = cell.getStringCellValue(); } else if (HSSFCell.CELL_TYPE_NUMERIC == cell.getCellType()) { // HSSFDataFormatter dataFormatter = new HSSFDataFormatter(); // val = dataFormatter.formatCellValue(cell); val = new BigDecimal(cell.getNumericCellValue()).toString(); } if (val != null && val.trim().length() > 0) { values.add(val.trim()); } else { values.add(null); } } else { values.add(null); } } if (allCellsAreNull) { return null; } final Object result = mapper.createEntity(values); return result; }
From source file:org.devgateway.eudevfin.importing.metadata.streamprocessors.ExcelStreamProcessor.java
License:Open Source License
private void findMapperClassName() { final HSSFRow row = this.sheet.getRow(MAPPER_CLASS_ROW_NUM); final HSSFCell cell = row.getCell(MAPPER_CLASS_COL_NUM); if (HSSFCell.CELL_TYPE_STRING == cell.getCellType()) { this.mapperClassName = cell.getStringCellValue(); } else {//from www. j av a2 s .co m throw new InvalidDataException("Expecting mapper name in cell B1"); } }
From source file:org.devgateway.eudevfin.importing.metadata.streamprocessors.ExcelStreamProcessor.java
License:Open Source License
private void generateMetadataInfoList() { this.metadataInfoList = new ArrayList<String>(); final HSSFRow row = this.sheet.getRow(METADATA_INFO_ROW_NUM); final short end = row.getLastCellNum(); for (short i = 1; i < end; i++) { final HSSFCell cell = row.getCell((int) i); if (cell != null) { if (HSSFCell.CELL_TYPE_STRING == cell.getCellType()) { final String value = cell.getStringCellValue(); this.metadataInfoList.add(value); } else { throw new InvalidDataException("Expecting mapper name in cell B1"); }// w ww . j a v a2 s. co m } else { break; } } }
From source file:org.egov.egf.web.actions.brs.AutoReconcileHelper.java
License:Open Source License
private String getStrValue(final HSSFCell cell) { if (cell == null) return null; double numericCellValue = 0d; String strValue = ""; switch (cell.getCellType()) { case HSSFCell.CELL_TYPE_NUMERIC: numericCellValue = cell.getNumericCellValue(); final DecimalFormat decimalFormat = new DecimalFormat("#"); strValue = decimalFormat.format(numericCellValue); break;//from w w w . ja v a2s. c om case HSSFCell.CELL_TYPE_STRING: strValue = cell.getStringCellValue(); break; } return strValue; }