List of usage examples for org.apache.poi.hssf.usermodel HSSFCell getDateCellValue
public Date getDateCellValue()
From source file:at.spardat.xma.mdl.grid.GridPOIAdapter.java
License:Open Source License
/** * Transfers the spreadsheet data from the POI <code>HSSFWorkbook</code> into the <code>IGridWMServer</code>. * Only the sheet on the given sheetIndex is copied. /*from w w w .j av a 2 s . c o m*/ * @param igrid the XMA model where to copy the data * @param book the POI represntation of the data * @param sheetIndex the index of the sheet to copy * @return a list containing all SysExceptions describing problems with individual cell formulas or ranges */ public static List poi2xma(IGridWM igrid, HSSFWorkbook book, int sheetIndex) { GridWM grid = (GridWM) igrid; try { List errorList = new ArrayList(); grid.setSheetName(book.getSheetName(sheetIndex)); grid.colors.clear(); grid.initBuildInColors(); short ic = GridWM.HSSF_FIRST_COLOR_INDEX; HSSFPalette palette = book.getCustomPalette(); for (HSSFColor color = palette.getColor(ic); ic < 64 && color != null; color = palette.getColor(++ic)) { grid.colors.add(ic, new GridColor(color.getTriplet())); } grid.fonts.clear(); int numFonts = book.getNumberOfFonts(); if (numFonts > 4) { // adjust for "There is no 4" see code of org.apache.poi.hssf.model.Workbook.getFontRecordAt() numFonts += 1; } for (short i = 0; i < numFonts; i++) { HSSFFont font = book.getFontAt(i); byte fontstyle = GridFont.FONT_NORML; if (font.getBoldweight() >= HSSFFont.BOLDWEIGHT_BOLD) fontstyle |= GridFont.FONT_BOLD; if (font.getItalic()) fontstyle |= GridFont.FONT_ITALIC; grid.fonts.add(i, new GridFont(font.getFontName(), fontstyle, font.getColor())); } grid.styles.clear(); for (short i = 0, numStyles = book.getNumCellStyles(); i < numStyles; i++) { HSSFCellStyle style = book.getCellStyleAt(i); grid.styles.add(i, new GridCellStyle(style.getFontIndex(), style.getFillForegroundColor())); } grid.namedRanges.clear(); for (int i = 0, numRanges = book.getNumberOfNames(); i < numRanges; i++) { HSSFName name = book.getNameAt(i); String rangeName = name.getNameName(); String rangeRef = null; try { // ranges not defined but referenced by formulas have a name but no reference in HSSF rangeRef = name.getReference(); } catch (Exception exc) { errorList.add(new SysException(exc, ((GridWM) grid).getMessage("inconsistentRange", rangeName)) .setCode(GridWM.CODE_inconsistentRange)); } if (rangeRef != null) { try { GridRange range = grid.getJeksDelegate().toRange(rangeRef); range.setKey(rangeName); grid.namedRanges.put(rangeName, range); } catch (Exception exc) { errorList.add(new SysException(exc, ((GridWM) grid).getMessage("unsupportedReference", rangeName, rangeRef)) .setCode(GridWM.CODE_unsupportedReference)); } } } grid.rows.clear(); grid.cols.clear(); grid.cells.clear(); grid.delegate = new GridJeksDelegate(grid); HSSFSheet sheet = book.getSheetAt(sheetIndex); int firstColNum = Integer.MAX_VALUE; int lastColNum = Integer.MIN_VALUE; for (int i = sheet.getFirstRowNum(); i <= sheet.getLastRowNum(); i++) { HSSFRow row = sheet.getRow(i); if (row == null) continue; if (row.getFirstCellNum() >= 0) firstColNum = Math.min(firstColNum, row.getFirstCellNum()); lastColNum = Math.max(lastColNum, row.getLastCellNum()); if (lastColNum > 255) lastColNum = 255; for (short j = row.getFirstCellNum(); j <= row.getLastCellNum(); j++) { HSSFCell hssfcell = row.getCell(j); if (hssfcell == null) continue; GridCell gridcell = grid.getOrCreateCell(i, j); switch (hssfcell.getCellType()) { case HSSFCell.CELL_TYPE_BLANK: break; case HSSFCell.CELL_TYPE_BOOLEAN: gridcell.setValue(hssfcell.getBooleanCellValue()); break; case HSSFCell.CELL_TYPE_ERROR: // TODO: recherche error text byte errorCode = hssfcell.getErrorCellValue(); // gridcell.setValue(errorCode); gridcell.setValue("#ERROR"); errorList.add(new SysException(((GridWM) grid).getMessage("errorRecord", grid.getJeksDelegate().toExcelRef(i, j), Byte.toString(errorCode))) .setCode(GridWM.CODE_errorRecord)); break; case HSSFCell.CELL_TYPE_FORMULA: String formula = null; try { formula = hssfcell.getCellFormula(); gridcell.setFormula(formula); } catch (SysException e) { if (formula != null) gridcell.setValue("=" + formula); //set it as text without interpretation errorList.add(e); } break; case HSSFCell.CELL_TYPE_NUMERIC: if (isDateCell(book, hssfcell)) { gridcell.setValue(hssfcell.getDateCellValue()); } else { gridcell.setValue(hssfcell.getNumericCellValue()); } break; case HSSFCell.CELL_TYPE_STRING: gridcell.setValue(hssfcell.getStringCellValue()); break; default: throw new SysException("unknown cell type " + hssfcell.getCellType()); } gridcell.setEditable(!hssfcell.getCellStyle().getLocked()); gridcell.setStyle(hssfcell.getCellStyle().getIndex()); } } final int scalefactor = 256 / 7; // empirically testet // int width = sheet.getDefaultColumnWidth(); // returns nonsense // width = width/scalefactor; // grid.setDefaultColumnWidth(width); for (short i = (short) firstColNum; i <= lastColNum; i++) { int width = sheet.getColumnWidth(i); width = width / scalefactor; grid.getOrCreateColumn(i).setWidth(width); } if (firstColNum == Integer.MAX_VALUE) firstColNum = 0; if (lastColNum == Integer.MIN_VALUE) lastColNum = 0; grid.setMaxRange( new GridRange(grid, sheet.getFirstRowNum(), firstColNum, sheet.getLastRowNum(), lastColNum)); grid.setVisibleRange(grid.getMaxRange()); return errorList; } finally { grid.handle(grid.new GridReloadEvent()); } }
From source file:bean.GlReportExport.java
public void export() { Integer columnNo;/*from w w w. j a va2 s. c o m*/ HSSFWorkbook workbook; HSSFSheet sheet; HSSFRow dataRow; HSSFCell cell; HSSFCellStyle cellStyle, boldStyle; HSSFFont font; ThemeDisplay themeDisplay = LiferayFacesContext.getInstance().getThemeDisplay(); getExportData().createFolder(null, themeDisplay, "GL Report", "DESCRIPTION"); if (getExportData().getFilename() == null || getExportData().getFilename().length() == 0) { getExportData().setFilename("Default(" + new Date() + ")"); } getExportData().setFilename(getExportData().getFilename().replace(":", "")); try { getExportData().setFilename(getExportData().getFilename().concat(".xls")); workbook = new HSSFWorkbook(); cellStyle = workbook.createCellStyle(); cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("#,##0.00")); font = workbook.createFont(); font.setBoldweight(Font.BOLDWEIGHT_BOLD); boldStyle = workbook.createCellStyle(); boldStyle.setFont(font); sheet = workbook.createSheet("GL Report " + exportDate()); dataRow = sheet.createRow((short) 0); columnNo = 0; cell = dataRow.createCell(columnNo++); cell.setCellValue("GL REPORT"); cell.setCellStyle(boldStyle); dataRow = sheet.createRow(dataRow.getRowNum() + 1); columnNo = 0; cell = dataRow.createCell(columnNo++); cell.setCellValue(exportDate()); cell.setCellStyle(boldStyle); dataRow = sheet.createRow(dataRow.getRowNum() + 1); dataRow = sheet.createRow(dataRow.getRowNum() + 1); columnNo = 0; cell = dataRow.createCell(columnNo++); cell.setCellValue("Date"); cell.setCellStyle(boldStyle); columnNo = 1; cell = dataRow.createCell(columnNo++); cell.setCellValue("Account Title"); cell.setCellStyle(boldStyle); columnNo = 2; cell = dataRow.createCell(columnNo++); cell.setCellValue("Journal Type"); cell.setCellStyle(boldStyle); columnNo = 3; cell = dataRow.createCell(columnNo++); cell.setCellValue("Debit"); cell.setCellStyle(boldStyle); columnNo = 4; cell = dataRow.createCell(columnNo++); cell.setCellValue("Credit"); cell.setCellStyle(boldStyle); columnNo = 5; cell = dataRow.createCell(columnNo++); cell.setCellValue("Balance"); cell.setCellStyle(boldStyle); for (int i = 0; i < getGlReportData().getGlReport().size(); i++) { dataRow = sheet.createRow(dataRow.getRowNum() + 1); //DATE columnNo = 0; cell = dataRow.createCell(columnNo++); cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC); cell.setCellValue(((Date) getGlReportData().getGlReport().get(i)[0])); cell.setCellStyle(cellStyle); cell.getDateCellValue(); // dataRow.createCell(columnNo++).setCellValue(""); //ACCOUNT TITLE columnNo = 1; cell = dataRow.createCell(columnNo++); cell.setCellValue((String) getGlReportData().getGlReport().get(i)[1]); cell.setCellStyle(cellStyle); //JOURNAL TYPE columnNo = 2; cell = dataRow.createCell(columnNo++); cell.setCellValue((String) getGlReportData().getGlReport().get(i)[2]); cell.setCellStyle(cellStyle); //DEBIT columnNo = 3; cell = dataRow.createCell(columnNo++); cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC); cell.setCellValue(((BigDecimal) getGlReportData().getGlReport().get(i)[3]).doubleValue()); cell.setCellStyle(cellStyle); //CREDIT columnNo = 4; cell = dataRow.createCell(columnNo++); cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC); cell.setCellValue(((BigDecimal) getGlReportData().getGlReport().get(i)[4]).doubleValue()); cell.setCellStyle(cellStyle); //BALANCE columnNo = 5; cell = dataRow.createCell(columnNo++); cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC); cell.setCellValue(((BigDecimal) getGlReportData().getGlReport().get(i)[5]).doubleValue()); cell.setCellStyle(cellStyle); // if (i == getSlReportData().getSlReport().size() - 1) { // dataRow = sheet.createRow(dataRow.getRowNum() + 1); // dataRow = sheet.createRow(dataRow.getRowNum() + 1); // columnNo = 0; // // cell = dataRow.createCell(columnNo++); // cell.setCellValue("TOTAL:"); // cell.setCellStyle(boldStyle); // // dataRow.createCell(columnNo++).setCellValue(""); // // columnNo = 3; // // cell = dataRow.createCell(columnNo++); // cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC); // cell.setCellValue(getDataConvert().convertAmount(getTrialBalanceData().getTrialBalanceDebitTotal())); // cell.setCellStyle(boldStyle); // // columnNo = 4; // // cell = dataRow.createCell(columnNo++); // cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC); // cell.setCellValue(getDataConvert().convertAmount(getTrialBalanceData().getTrialBalanceCreditTotal())); // cell.setCellStyle(boldStyle); // } } FileOutputStream fileOutputStream = new FileOutputStream(getExportData().getFilename()); workbook.write(fileOutputStream); fileOutputStream.close(); getExportData().fileUploadByDL(getExportData().getFilename(), "GL Report", themeDisplay, null); File file = new File(getExportData().getFilename()); if (file.exists()) { file.delete(); } } catch (Exception e) { System.out.print("glReportExport().export() " + e); FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error", "An error occurred while generating excel file."); FacesContext.getCurrentInstance().addMessage(null, message); } }
From source file:blueprint.sdk.experimental.util.XlsReader.java
License:Open Source License
protected String getTimeValue(final HSSFCell cell) throws IOException { String result;//from ww w. j a v a 2s .co m Calendar cal = Calendar.getInstance(); try { switch (cell.getCellType()) { case HSSFCell.CELL_TYPE_NUMERIC: cal.setTime(cell.getDateCellValue()); result = StringUtil.lpadZero(Integer.toString(cal.get(Calendar.HOUR_OF_DAY)), 2) + StringUtil.lpadZero(Integer.toString(cal.get(Calendar.MINUTE)), 2) + StringUtil.lpadZero(Integer.toString(cal.get(Calendar.SECOND)), 2); break; default: throw new IOException(createTypeErrMsg(cell)); } } catch (IllegalStateException e) { e.printStackTrace(); throw new IOException(createTypeErrMsg(cell)); } return result; }
From source file:blueprint.sdk.experimental.util.XlsReader.java
License:Open Source License
protected String getDateValue(final HSSFCell cell) throws IOException { String result;//from w w w . ja v a2 s.co m Calendar cal = Calendar.getInstance(); try { switch (cell.getCellType()) { case HSSFCell.CELL_TYPE_NUMERIC: cal.setTime(cell.getDateCellValue()); result = StringUtil.lpadZero(Integer.toString(cal.get(Calendar.YEAR)), 4) + StringUtil.lpadZero(Integer.toString(cal.get(Calendar.MONTH) + 1), 2) + StringUtil.lpadZero(Integer.toString(cal.get(Calendar.DAY_OF_MONTH)), 2); break; default: throw new IOException(createTypeErrMsg(cell)); } } catch (IllegalStateException e) { e.printStackTrace(); throw new IOException(createTypeErrMsg(cell)); } return result; }
From source file:ch.elexis.core.importer.div.importers.ExcelWrapper.java
License:Open Source License
/** * Return a row of data from the sheet./*from w w w .ja v a 2 s . c o m*/ * * @param rowNr * zero based index of the desired row * @return a List of Strings with the row values or null if no such row exists. */ public List<String> getRow(final int rowNr) { HSSFRow row = sheet.getRow(rowNr); if (row == null) { return null; } ArrayList<String> ret = new ArrayList<String>(); short first = 0; short last = 100; if (types != null) { last = (short) (types.length); } else { first = row.getFirstCellNum(); last = row.getLastCellNum(); } for (short i = first; i < last; i++) { HSSFCell cell = row.getCell(i); if (cell != null) { switch (cell.getCellType()) { case HSSFCell.CELL_TYPE_BLANK: ret.add(""); //$NON-NLS-1$ break; case HSSFCell.CELL_TYPE_BOOLEAN: ret.add(Boolean.toString(cell.getBooleanCellValue())); break; case HSSFCell.CELL_TYPE_NUMERIC: if (types != null) { if (types[i].equals(Integer.class)) { ret.add(Long.toString(Math.round(cell.getNumericCellValue()))); } else if (types[i].equals(TimeTool.class)) { Date date = cell.getDateCellValue(); if (date != null) { TimeTool tt = new TimeTool(date.getTime()); ret.add(tt.toString(TimeTool.FULL_MYSQL)); } else { ret.add(""); //$NON-NLS-1$ } } else if (types[i].equals(Double.class)) { ret.add(Double.toString(cell.getNumericCellValue())); break; } else /* if(types[i].equals(String.class)) */ { double cv = cell.getNumericCellValue(); // String r=Double.toString(cv); String r = NumberFormat.getNumberInstance().format(cv); ret.add(r); } break; } // else fall thru case HSSFCell.CELL_TYPE_FORMULA: ret.add(Double.toString(cell.getNumericCellValue())); break; case HSSFCell.CELL_TYPE_STRING: ret.add(cell.toString()); break; default: ret.add(Messages.ExcelWrapper_ErrorUnknownCellType); } } else { // empty cell ret.add(""); //$NON-NLS-1$ } } return ret; }
From source file:cn.baiweigang.qtaf.toolkit.dbunit.data.excel.XlsTable.java
License:Open Source License
protected Object getDateValue(HSSFCell cell) { // Simplification and the use of Chinese date format return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(cell.getDateCellValue()); }
From source file:cn.fql.template.poi.MergeInvoiceSheet.java
License:Open Source License
private static void writeEffortTotal() { HSSFSheet templateSheet = templateWbk.getSheet("Effort Total"); String lastProjectName = null; Date startDateValue = null;/*from ww w. j a v a 2 s . c om*/ Date endDateValue = null; double totalDaysValue = 0; for (int i = 4; i < 10000; i++) { HSSFRow row = templateSheet.getRow(i); if (row == null) { if (templateSheet.getRow(i + 1) == null) { break; } } else { HSSFCell projectNameCell = row.getCell((short) 1); HSSFCell startDateCell = row.getCell((short) 2); HSSFCell endDateCell = row.getCell((short) 3); HSSFCell totalDaysCell = row.getCell((short) 4); if (totalDaysCell == null) { break; } String newProjectName = projectNameCell.getStringCellValue(); Date _startDateValue = startDateCell.getDateCellValue(); Date _endDateValue = endDateCell.getDateCellValue(); double _totalDaysValue = totalDaysCell.getNumericCellValue(); if (lastProjectName == null) { lastProjectName = newProjectName; startDateValue = _startDateValue; endDateValue = _endDateValue; totalDaysValue = _totalDaysValue; } else { if (newProjectName.equals(lastProjectName)) { totalDaysValue += _totalDaysValue; templateSheet.getRow(i - 1).getCell((short) 4).setCellValue(totalDaysValue); if (startDateValue.compareTo(_startDateValue) > 0) { startDateValue = _startDateValue; templateSheet.getRow(i - 1).getCell((short) 2).setCellValue(startDateValue); } if (endDateValue.compareTo(_endDateValue) < 0) { endDateValue = _endDateValue; templateSheet.getRow(i - 1).getCell((short) 3).setCellValue(endDateValue); } templateSheet.removeRow(row); templateSheet.shiftRows(i + 1, 109, -1); i--; } else { lastProjectName = newProjectName; startDateValue = _startDateValue; endDateValue = _endDateValue; totalDaysValue = _totalDaysValue; } } } } }
From source file:cn.trymore.core.util.excel.PoiExcelParser.java
License:Open Source License
public Object getCellContent(Object cell) { if (cell != null) { HSSFCell cel = (HSSFCell) cell; HSSFFormulaEvaluator evaluator = new HSSFFormulaEvaluator(this.book); CellValue cellValue = evaluator.evaluate(cel); switch (cel.getCellType()) { case HSSFCell.CELL_TYPE_NUMERIC: if (HSSFDateUtil.isCellDateFormatted(cel)) { return UtilDate.parseTime(cel.getDateCellValue(), "yyyy-MM-dd HH:mm:ss"); } else { return Double.valueOf(cellValue.getNumberValue()); }//from w w w. j a va 2 s. c o m case HSSFCell.CELL_TYPE_STRING: return cellValue.getStringValue(); case HSSFCell.CELL_TYPE_BOOLEAN: return Boolean.valueOf(cellValue.getBooleanValue()); case HSSFCell.CELL_TYPE_ERROR: return Byte.valueOf(cellValue.getErrorValue()); case HSSFCell.CELL_TYPE_BLANK: break; case HSSFCell.CELL_TYPE_FORMULA: return cellValue.formatAsString(); default: return null; } } return null; }
From source file:com.bayareasoftware.chartengine.ds.util.ExcelDump.java
License:Apache License
private static String cell2string(HSSFCell c) { if (c == null) { return "<i>NULL CELL</i>"; }/*from w w w . ja va 2s .c o m*/ int type = c.getCellType(); String t = null, v = null; switch (type) { case HSSFCell.CELL_TYPE_BLANK: t = "BLANK"; v = ""; break; case HSSFCell.CELL_TYPE_STRING: t = "STRING"; v = c.getStringCellValue(); break; case HSSFCell.CELL_TYPE_NUMERIC: if (ExcelInference.isCellDateFormatted(c)) { t = "DATE"; v = c.getDateCellValue() + ""; } else { t = "NUMERIC"; v = c.getNumericCellValue() + ""; } break; case HSSFCell.CELL_TYPE_ERROR: t = "ERROR"; v = "(errByte=" + c.getErrorCellValue() + "/toString=" + c + ")"; break; case HSSFCell.CELL_TYPE_FORMULA: t = "FORMULA"; v = c.getCellFormula(); break; default: t = "(UNKNOWN TYPE: " + type + ")"; v = c.toString(); break; } short style = c.getCellStyle().getDataFormat(); return v + "<br/>(" + t + ")<br/>dataformat=0x" + Integer.toHexString(style); }
From source file:com.beyondb.io.ExcelReader.java
/** * ?HSSFCell?/*from w w w . j av a 2 s.c o m*/ * @param cell * @return */ private String getCellFormatValue(HSSFCell cell) { String cellvalue = ""; if (cell != null) { // ?CellType switch (cell.getCellType()) { // ?CellTypeNUMERIC case HSSFCell.CELL_TYPE_NUMERIC: case HSSFCell.CELL_TYPE_FORMULA: { // ?cell?Date if (HSSFDateUtil.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"); cellvalue = sdf.format(date); } // else { // ??Cell cellvalue = String.valueOf(cell.getNumericCellValue()); } break; } // ?CellTypeSTRIN case HSSFCell.CELL_TYPE_STRING: // ??Cell cellvalue = cell.getRichStringCellValue().getString(); break; // Cell default: cellvalue = " "; } } else { cellvalue = ""; } return cellvalue; }