@Override public void handleNumber(XSSFCellStyle style, String number) { rawValues.add(number); if (style != null) { short formatIndex = style.getDataFormat(); String formatString = style.getDataFormatString(); if (formatString == null) { formatString = BuiltinFormats.getBuiltinFormat(formatIndex); } if (formatString != null) { formattedValues.add(formatter.formatRawCellContents(Double.parseDouble(number), formatIndex, formatString)); if (formatString.contentEquals("D/M/YYYY")) { rawValues.add(formatter.formatRawCellContents(Double.parseDouble(number), formatIndex, formatString)); } } }else{ formattedValues.add(number); } }
/** * @param cellStyleId * the cell style ID stored within the XLSX worksheet cell tag. <code>null</code> is * allowed and will return <code>false</code> * @return <code>true</code> in case it is a date, <code>false</code> otherwise */ public boolean isDateFormatStyle(String cellStyleId) { if (cellStyleId == null) { return false; } /* * Cell styles references are stored within the cell tag as a 0 based index of the cell * formats. */ int numberFormatId = cellNumberFormatIds[Integer.parseInt(cellStyleId)]; Boolean isNumberFormat = isDateFormatCache.get(numberFormatId); if (isNumberFormat == null) { // Check builtin formats if custom date format cache does not contain a hit String builtinFormat = BuiltinFormats.getBuiltinFormat(numberFormatId); if (builtinFormat != null) { isNumberFormat = checkForDateFormat(numberFormatId, builtinFormat); isDateFormatCache.put(numberFormatId, isNumberFormat); } else { // It is neither a custom nor a a built-in format -> probably not a date format isNumberFormat = false; } } return isNumberFormat; }
protected static void setCellValue(Workbook wb, Cell cell, Object value) { if (cell == null) { return; } if (value instanceof Boolean) { cell.setCellValue((Boolean)value); } else if (value instanceof Number) { cell.setCellValue(((Number)value).doubleValue()); } else if (value instanceof Date) { CellStyle cellStyle = wb.createCellStyle(); DataFormat poiFormat = wb.createDataFormat(); // Format: 0x16, "m/d/yy h:mm" final short format = poiFormat.getFormat(BuiltinFormats.getBuiltinFormat(0x16)); cellStyle.setDataFormat(format); cell.setCellValue(((Date)value)); cell.setCellStyle(cellStyle); } else if (value instanceof Calendar) { cell.setCellValue(((Calendar)value)); } else if (value != null) { cell.setCellValue(value.toString()); } else { cell.setCellValue(""); cell.setCellType(Cell.CELL_TYPE_BLANK); } }
/** * Read the numeric format string out of the styles table for this cell. Stores the result in the Cell. * * @param startElement * @param cell */ void setFormatString(StartElement startElement, StreamingCell cell) { Attribute cellStyle = startElement.getAttributeByName(new QName("s")); String cellStyleString = (cellStyle != null) ? cellStyle.getValue() : null; XSSFCellStyle style = null; if (cellStyleString != null) { style = stylesTable.getStyleAt(Integer.parseInt(cellStyleString)); } else if (stylesTable.getNumCellStyles() > 0) { style = stylesTable.getStyleAt(0); } if (style != null) { cell.setNumericFormatIndex(style.getDataFormat()); String formatString = style.getDataFormatString(); if (formatString != null) { cell.setNumericFormat(formatString); } else { cell.setNumericFormat(BuiltinFormats.getBuiltinFormat(cell.getNumericFormatIndex())); } } else { cell.setNumericFormatIndex(null); cell.setNumericFormat(null); } }
/** * Read the numeric format string out of the styles table for this cell. Stores * the result in the Cell. * * @param startElement * @param cell */ void setFormatString(StartElement startElement, StreamingCell cell) { Attribute cellStyle = startElement.getAttributeByName(new QName("s")); String cellStyleString = (cellStyle != null) ? cellStyle.getValue() : null; XSSFCellStyle style = null; if(cellStyleString != null) { style = stylesTable.getStyleAt(Integer.parseInt(cellStyleString)); } else if(stylesTable.getNumCellStyles() > 0) { style = stylesTable.getStyleAt(0); } if(style != null) { cell.setNumericFormatIndex(style.getDataFormat()); String formatString = style.getDataFormatString(); if(formatString != null) { cell.setNumericFormat(formatString); } else { cell.setNumericFormat(BuiltinFormats.getBuiltinFormat(cell.getNumericFormatIndex())); } } else { cell.setNumericFormatIndex(null); cell.setNumericFormat(null); } }
/** * 处理数据类型 * * @param attributes attributes */ public void setNextDataType(Attributes attributes) { mNextDataType = CellValueType.STRING; mFormatIndex = -1; mFormatString = null; String cellType = attributes.getValue("t"); String cellStyleStr = attributes.getValue("s"); if ("b".equals(cellType)) { mNextDataType = CellValueType.BOOL; } else if ("e".equals(cellType)) { mNextDataType = CellValueType.ERROR; } else if ("inlineStr".equals(cellType)) { mNextDataType = CellValueType.INLINESTR; } else if ("s".equals(cellType)) { mNextDataType = CellValueType.STRING; } else if ("str".equals(cellType)) { mNextDataType = CellValueType.FORMULA; } //TODO: 日期类型的判断 if (cellStyleStr != null) { int styleIndex = Integer.parseInt(cellStyleStr); XSSFCellStyle style = mStylesTable.getStyleAt(styleIndex); mFormatIndex = style.getDataFormat(); mFormatString = style.getDataFormatString(); if (mFormatString == null) { mNextDataType = CellValueType.NULL; mFormatString = BuiltinFormats.getBuiltinFormat(mFormatIndex); } } }
public void startElement(String uri, String localName, String name, Attributes attributes) throws SAXException { if (name.equals("c")) { String r = attributes.getValue("r"); int firstDigit = -1; for (int c = 0; c < r.length(); ++c) { if (Character.isDigit(r.charAt(c))) { firstDigit = c; break; } } curColumn = nameToColumn(r.substring(0, firstDigit)); columns.put(curColumn, null); String cellType = attributes.getValue("t"); this.nextDataType = XssfDataType.NUMBER; this.formatIndex = -1; this.formatString = null; String cellStyleStr = attributes.getValue("s"); if ("b".equals(cellType)) nextDataType = XssfDataType.BOOL; else if ("e".equals(cellType)) nextDataType = XssfDataType.ERROR; else if ("inlineStr".equals(cellType)) nextDataType = XssfDataType.INLINESTR; else if ("s".equals(cellType)) nextDataType = XssfDataType.SSTINDEX; else if ("str".equals(cellType)) nextDataType = XssfDataType.FORMULA; else if (cellStyleStr != null) { int styleIndex = Integer.parseInt(cellStyleStr); XSSFCellStyle style = stylesTable.getStyleAt(styleIndex); this.formatIndex = style.getDataFormat(); this.formatString = style.getDataFormatString(); if (this.formatString == null) { this.formatString = BuiltinFormats.getBuiltinFormat(this.formatIndex); } } } lastContents = ""; }
private Map<String, CellStyle> createStyles(Workbook wb){ Map<String, CellStyle> styles = new HashMap<>(); CellStyle style; Font titleFont = wb.createFont(); titleFont.setFontHeightInPoints((short)12); titleFont.setBold(true); style = wb.createCellStyle(); style.setAlignment(HorizontalAlignment.CENTER); style.setVerticalAlignment(VerticalAlignment.CENTER); style.setFillForegroundColor( IndexedColors.GREY_25_PERCENT.getIndex()); style.setFillPattern(FillPatternType.SOLID_FOREGROUND); style.setFont(titleFont); style.setWrapText(false); style.setBorderBottom(BorderStyle.THIN); style.setBottomBorderColor(IndexedColors.GREY_80_PERCENT.getIndex()); styles.put("header", style); Font cellFont = wb.createFont(); cellFont.setFontHeightInPoints((short)10); cellFont.setBold(true); style = wb.createCellStyle(); style.setAlignment(HorizontalAlignment.RIGHT); style.setVerticalAlignment(VerticalAlignment.BOTTOM); style.setFont(cellFont); style.setWrapText(false); style.setDataFormat(wb.createDataFormat().getFormat( BuiltinFormats.getBuiltinFormat( 3 ))); styles.put("integer_number_cell", style); style = wb.createCellStyle(); style.setAlignment(HorizontalAlignment.RIGHT); style.setVerticalAlignment(VerticalAlignment.BOTTOM); style.setFont(cellFont); style.setWrapText(false); style.setDataFormat(wb.createDataFormat().getFormat(BuiltinFormats.getBuiltinFormat(4))); styles.put("decimal_number_cell", style); style = wb.createCellStyle(); style.setAlignment(HorizontalAlignment.LEFT); style.setVerticalAlignment(VerticalAlignment.BOTTOM); style.setFont(cellFont); style.setWrapText(false); style.setDataFormat( (short) BuiltinFormats.getBuiltinFormat("text") ); styles.put(TEXT_CELL, style); style = wb.createCellStyle(); style.setAlignment(HorizontalAlignment.CENTER); style.setVerticalAlignment(VerticalAlignment.BOTTOM); style.setFont(cellFont); style.setWrapText(false); style.setDataFormat(wb.createDataFormat().getFormat( DateFormatConverter.convert( Locale.getDefault(), dateFormatPattern ))); styles.put("date_cell", style); return styles; }
private void execute(final Sheet sheet) { DataFormat format = sheet.getWorkbook().createDataFormat(); for(int i=0; i <= 59; i++) { final Row row = sheet.getRow(3 + i); Cell descriptionCell = row.getCell(1); Cell testCaseCell = row.getCell(2); Cell testResultCell = row.getCell(3); Cell indexCell = row.getCell(4); Cell formatCell = row.getCell(5); descriptionCell.setCellValue(String.format("[10進数]=%d, [16進数]=%s", i, Integer.toHexString(i))); // テストケースのスタイルの設定 CellStyle style = sheet.getWorkbook().createCellStyle(); style.setDataFormat((short)i); style.setBorderBottom(CellStyle.BORDER_THIN); testCaseCell.setCellStyle(style); if(i == 0) { testCaseCell.setCellValue("テキスト"); } else if(range(i, 1, 8)) { // 数値 testCaseCell.setCellValue(-123.456); } else if(range(i, 9, 11)) { // パーセント、小数 testCaseCell.setCellValue(0.0123); } else if(range(i, 12, 13)) { // 分数 testCaseCell.setCellValue(12.345); } else if(range(i, 14, 36)) { // 日時 testCaseCell.setCellValue(Timestamp.valueOf("2000-02-29 10:19:23.123")); } else if(range(i, 37, 44)) { // 会計 testCaseCell.setCellValue(-123.456); } else if(range(i, 45, 48)) { // 時間、経過時間 testCaseCell.setCellValue(Timestamp.valueOf("1900-01-01 00:01:01.000")); } else if(range(i, 9, 11)) { // 指数 testCaseCell.setCellValue(123.456789); } else if(i == 49) { // テキスト testCaseCell.setCellValue("テキスト"); } else if(range(i, 50, 59)) { testCaseCell.setCellValue(Timestamp.valueOf("2000-02-29 10:19:23.123")); } // インデックス indexCell.setCellValue(i); // フォーマット formatCell.setCellValue(BuiltinFormats.getBuiltinFormat(i)); } }
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { if (qName.equals("c")) { String vCellType = attributes.getValue("t"); String cellS = attributes.getValue("s"); if ("b".equals(vCellType)) cellDataType = cDataType.BOOL; else if ("e".equals(vCellType)) cellDataType = cDataType.FORMULA; else if ("s".equals(vCellType)) cellDataType = cDataType.SSTINDEX; else if("str".equals(vCellType)) cellDataType = cDataType.STATIC; else if (cellS != null) { //number with formatting or date int styleIndex = Integer.parseInt(cellS); XSSFCellStyle style = st.getStyleAt(styleIndex); short formatIndex = style.getDataFormat(); String formatString = style.getDataFormatString(); if (formatString == null) formatString = BuiltinFormats.getBuiltinFormat(formatIndex); if (org.apache.poi.ss.usermodel.DateUtil.isADateFormat( formatIndex, formatString) ) { cellDataType = cDataType.DATETIME; }else{ cellDataType = cDataType.NUMBER; } } else cellDataType = cDataType.NUMBER; String r = attributes.getValue("r"); currentColumn = getColumnNumber( r ); //expand the number of columns if needed in existing rows if( currentColumn+1 > columnCount){ callback.columnExpansion(currentColumn+1); //clean up current row int newvals = (currentColumn+1) - columnCount; for( int ii=0; ii<newvals;ii++){ values.add(ExcelODAConstants.EMPTY_STRING); } columnCount = currentColumn+1; } } //empty cells are not in the xml so we have //create them in the row if (qName.equals("row")) { for( int i=0;i<columnCount; i++){ values.add(i, ExcelODAConstants.EMPTY_STRING); } } lastContents = ExcelODAConstants.EMPTY_STRING; }
@Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { if ("row".equals(qName)) { // element is a row // excel row numbers are 1-based int rowNumber = Integer.parseInt(attributes.getValue("r")); rowNumber = rowNumber - 1; if (_configuration.isSkipEmptyLines()) { _rowNumber++; } else { while (_rowNumber + 1 < rowNumber) { // empty lines are not skipped, so dispatch empty lines _rowNumber++; List<String> emptyValues = Collections.emptyList(); List<Style> emptyStyles = Collections.emptyList(); _callback.row(_rowNumber, emptyValues, emptyStyles); } _rowNumber = rowNumber; } } else if ("c".equals(qName)) { // element is a cell _inCell = true; final String r = attributes.getValue("r"); int firstDigit = -1; for (int c = 0; c < r.length(); ++c) { if (Character.isDigit(r.charAt(c))) { firstDigit = c; break; } } _columnNumber = nameToColumn(r.substring(0, firstDigit)); // Set up defaults. _dataType = XssfDataType.NUMBER; _formatIndex = -1; _formatString = null; final String cellType = attributes.getValue("t"); if ("b".equals(cellType)) { _dataType = XssfDataType.BOOL; } else if ("e".equals(cellType)) { _dataType = XssfDataType.ERROR; } else if ("inlineStr".equals(cellType)) { _dataType = XssfDataType.INLINESTR; } else if ("s".equals(cellType)) { _dataType = XssfDataType.SSTINDEX; } else if ("str".equals(cellType)) { _dataType = XssfDataType.FORMULA; } String cellStyleStr = attributes.getValue("s"); if (cellStyleStr != null) { // It's a number, but almost certainly one // with a special style or format int styleIndex = Integer.parseInt(cellStyleStr); XSSFCellStyle style = _stylesTable.getStyleAt(styleIndex); configureStyle(style); if (_dataType == XssfDataType.NUMBER) { this._formatIndex = style.getDataFormat(); this._formatString = style.getDataFormatString(); if (this._formatString == null) { this._formatString = BuiltinFormats.getBuiltinFormat(this._formatIndex); } } } } else if (_inCell && "f".equals(qName)) { // skip the actual formula line _inFormula = true; } }
public void startElement(String uri, String localName, String name, Attributes attributes) throws SAXException { if ("inlineStr".equals(name) || "v".equals(name)) { vIsOpen = true; // Clear contents cache value.setLength(0); } // c => cell else if ("c".equals(name)) { // Get the cell reference String r = attributes.getValue("r"); int firstDigit = -1; for (int c = 0; c < r.length(); ++c) { if (Character.isDigit(r.charAt(c))) { firstDigit = c; break; } } thisColumn = nameToColumn(r.substring(0, firstDigit)); // Set up defaults. this.nextDataType = xssfDataType.NUMBER; this.formatIndex = -1; this.formatString = null; String cellType = attributes.getValue("t"); String cellStyleStr = attributes.getValue("s"); if ("b".equals(cellType)) nextDataType = xssfDataType.BOOL; else if ("e".equals(cellType)) nextDataType = xssfDataType.ERROR; else if ("inlineStr".equals(cellType)) nextDataType = xssfDataType.INLINESTR; else if ("s".equals(cellType)) nextDataType = xssfDataType.SSTINDEX; else if ("str".equals(cellType)) nextDataType = xssfDataType.FORMULA; else if (cellStyleStr != null) { // It's a number, but almost certainly one // with a special style or format int styleIndex = Integer.parseInt(cellStyleStr); XSSFCellStyle style = stylesTable.getStyleAt(styleIndex); this.formatIndex = style.getDataFormat(); this.formatString = style.getDataFormatString(); if (this.formatString == null) this.formatString = BuiltinFormats.getBuiltinFormat(this.formatIndex); } } }