List of usage examples for org.apache.poi.ss.usermodel CreationHelper createRichTextString
RichTextString createRichTextString(String text);
From source file:it.eng.spagobi.engines.qbe.crosstable.exporter.CrosstabXLSExporter.java
License:Mozilla Public License
/** * Builds the columns' headers recursively with this order: * |------------------------------------------| * | 1 | 9 | * |------------------------------------------| * | 2 | 5 | 10 | * |-----------|-----------------|------------| * | 3 | 4 | 6 | 7 | 8 | 11 | 12 | * |------------------------------------------| * /*from w w w. j a v a 2 s . c o m*/ * @param sheet The sheet of the XLS file * @param siblings The siblings nodes of the headers structure * @param rowNum The row number where the siblings must be inserted * @param columnNum The column number where the first sibling must be inserted * @param createHelper The file creation helper * @param dimensionCellStyle The cell style for cells containing dimensions (i.e. attributes' names) * @param memberCellStyle The cell style for cells containing members (i.e. attributes' values) * @throws JSONException */ protected void buildColumnsHeader(Sheet sheet, CrossTab cs, List<Node> siblings, int rowNum, int columnNum, CreationHelper createHelper, Locale locale, CellStyle memberCellStyle, CellStyle dimensionCellStyle) throws JSONException { int columnCounter = columnNum; for (int i = 0; i < siblings.size(); i++) { Node aNode = (Node) siblings.get(i); List<Node> childs = aNode.getChilds(); Row row = sheet.getRow(rowNum); Cell cell = row.createCell(columnCounter); String text = (String) aNode.getDescription(); if (!cs.isMeasureOnRow() && (childs == null || childs.size() <= 0)) { //apply the measure scale factor text = MeasureScaleFactorOption.getScaledName(text, cs.getMeasureScaleFactor(text), locale); } cell.setCellValue(createHelper.createRichTextString(text)); cell.setCellType(this.getCellTypeString()); int descendants = aNode.getLeafsNumber(); if (descendants > 1) { sheet.addMergedRegion(new CellRangeAddress(rowNum, //first row (0-based) rowNum, //last row (0-based) columnCounter, //first column (0-based) columnCounter + descendants - 1 //last column (0-based) )); } /* * Now we have to set the style properly according to the nature of * the node: if it contains the name of a dimension or a member. * Since the structure foresees that a list of members follows a * dimension, we calculate the position of the node with respect to * the leaves; in case it is odd, the cell contains a dimension; in * case it is even, the cell contains a dimension. */ int distanceToLeaves = aNode.getDistanceFromLeaves(); if (!cs.isMeasureOnRow()) { distanceToLeaves--; } boolean isDimensionNameCell = distanceToLeaves > 0 && (distanceToLeaves % 2) == 1; if (isDimensionNameCell) { cell.setCellStyle(dimensionCellStyle); } else { cell.setCellStyle(memberCellStyle); } if (childs != null && childs.size() > 0) { buildColumnsHeader(sheet, cs, childs, rowNum + 1, columnCounter, createHelper, locale, memberCellStyle, dimensionCellStyle); } int increment = descendants > 1 ? descendants : 1; columnCounter = columnCounter + increment; } }
From source file:it.eng.spagobi.engines.qbe.exporter.QbeXLSExporter.java
License:Mozilla Public License
/** * //w w w . jav a2s .co m * @param sheet ... * @param workbook ... * @param createHelper ... * @param beginRowHeaderData header's vertical offset. Expressed in number of rows * @param beginColumnHeaderData header's horizontal offset. Expressed in number of columns * @return ... */ private CellStyle[] fillSheetHeader(Sheet sheet, Workbook workbook, CreationHelper createHelper, int beginRowHeaderData, int beginColumnHeaderData) { CellStyle[] cellTypes; logger.trace("IN"); try { IMetaData dataStoreMetaData = dataStore.getMetaData(); int colnumCount = dataStoreMetaData.getFieldCount(); Row headerRow = sheet.getRow(beginRowHeaderData); CellStyle headerCellStyle = buildHeaderCellStyle(sheet); cellTypes = new CellStyle[colnumCount]; for (int j = 0; j < colnumCount; j++) { Cell cell = headerRow.createCell(j + beginColumnHeaderData); cell.setCellType(getCellTypeString()); String fieldName = dataStoreMetaData.getFieldAlias(j); IFieldMetaData fieldMetaData = dataStoreMetaData.getFieldMeta(j); String format = (String) fieldMetaData.getProperty("format"); String alias = (String) fieldMetaData.getAlias(); String scaleFactorHeader = (String) fieldMetaData.getProperty( WorkSheetSerializationUtils.WORKSHEETS_ADDITIONAL_DATA_FIELDS_OPTIONS_SCALE_FACTOR); String header; if (extractedFields != null && j < extractedFields.size() && extractedFields.get(j) != null) { Field field = (Field) extractedFields.get(j); fieldName = field.getAlias(); if (field.getPattern() != null) { format = field.getPattern(); } } CellStyle aCellStyle = this.buildCellStyle(sheet); if (format != null) { short formatInt = this.getBuiltinFormat(format); aCellStyle.setDataFormat(formatInt); cellTypes[j] = aCellStyle; } if (alias != null && !alias.equals("")) { header = alias; } else { header = fieldName; } header = MeasureScaleFactorOption.getScaledName(header, scaleFactorHeader, locale); cell.setCellValue(createHelper.createRichTextString(header)); cell.setCellStyle(headerCellStyle); } } catch (Throwable t) { throw new SpagoBIRuntimeException("An unexpected error occured while filling sheet header", t); } finally { logger.trace("OUT"); } return cellTypes; }
From source file:it.eng.spagobi.engines.qbe.exporter.QbeXLSExporter.java
License:Mozilla Public License
public void fillSheetData(Sheet sheet, Workbook wb, CreationHelper createHelper, CellStyle[] cellTypes, int beginRowData, int beginColumnData) { CellStyle dCellStyle = this.buildCellStyle(sheet); Iterator it = dataStore.iterator(); int rownum = beginRowData; short formatIndexInt = this.getBuiltinFormat("#,##0"); CellStyle cellStyleInt = this.buildCellStyle(sheet); // cellStyleInt is the default cell style for integers cellStyleInt.cloneStyleFrom(dCellStyle); cellStyleInt.setDataFormat(formatIndexInt); CellStyle cellStyleDate = this.buildCellStyle(sheet); // cellStyleDate is the default cell style for dates cellStyleDate.cloneStyleFrom(dCellStyle); cellStyleDate.setDataFormat(createHelper.createDataFormat().getFormat("m/d/yy")); IMetaData d = dataStore.getMetaData(); while (it.hasNext()) { Row rowVal = sheet.getRow(rownum); IRecord record = (IRecord) it.next(); List fields = record.getFields(); int length = fields.size(); for (int fieldIndex = 0; fieldIndex < length; fieldIndex++) { IField f = (IField) fields.get(fieldIndex); if (f != null && f.getValue() != null) { Class c = d.getFieldType(fieldIndex); logger.debug("Column [" + (fieldIndex) + "] class is equal to [" + c.getName() + "]"); if (rowVal == null) { rowVal = sheet.createRow(rownum); }//w ww .j a v a2 s . co m Cell cell = rowVal.createCell(fieldIndex + beginColumnData); cell.setCellStyle(dCellStyle); if (Integer.class.isAssignableFrom(c) || Short.class.isAssignableFrom(c)) { logger.debug("Column [" + (fieldIndex + 1) + "] type is equal to [" + "INTEGER" + "]"); IFieldMetaData fieldMetaData = d.getFieldMeta(fieldIndex); String scaleFactor = (String) fieldMetaData.getProperty( WorkSheetSerializationUtils.WORKSHEETS_ADDITIONAL_DATA_FIELDS_OPTIONS_SCALE_FACTOR); Number val = (Number) f.getValue(); Double doubleValue = MeasureScaleFactorOption.applyScaleFactor(val.doubleValue(), scaleFactor); cell.setCellValue(doubleValue); cell.setCellType(this.getCellTypeNumeric()); cell.setCellStyle((cellTypes[fieldIndex] != null) ? cellTypes[fieldIndex] : cellStyleInt); } else if (Number.class.isAssignableFrom(c)) { logger.debug("Column [" + (fieldIndex + 1) + "] type is equal to [" + "NUMBER" + "]"); IFieldMetaData fieldMetaData = d.getFieldMeta(fieldIndex); String decimalPrecision = (String) fieldMetaData .getProperty(IFieldMetaData.DECIMALPRECISION); CellStyle cs; if (decimalPrecision != null) { cs = getDecimalNumberFormat(new Integer(decimalPrecision), sheet, createHelper, dCellStyle); } else { cs = getDecimalNumberFormat(DEFAULT_DECIMAL_PRECISION, sheet, createHelper, dCellStyle); } Number val = (Number) f.getValue(); Double value = val.doubleValue(); String scaleFactor = (String) fieldMetaData.getProperty( WorkSheetSerializationUtils.WORKSHEETS_ADDITIONAL_DATA_FIELDS_OPTIONS_SCALE_FACTOR); cell.setCellValue(MeasureScaleFactorOption.applyScaleFactor(value, scaleFactor)); cell.setCellType(this.getCellTypeNumeric()); cell.setCellStyle((cellTypes[fieldIndex] != null) ? cellTypes[fieldIndex] : cs); } else if (String.class.isAssignableFrom(c)) { logger.debug("Column [" + (fieldIndex + 1) + "] type is equal to [" + "STRING" + "]"); String val = (String) f.getValue(); cell.setCellValue(createHelper.createRichTextString(val)); cell.setCellType(this.getCellTypeString()); } else if (Boolean.class.isAssignableFrom(c)) { logger.debug("Column [" + (fieldIndex + 1) + "] type is equal to [" + "BOOLEAN" + "]"); Boolean val = (Boolean) f.getValue(); cell.setCellValue(val.booleanValue()); cell.setCellType(this.getCellTypeBoolean()); } else if (Date.class.isAssignableFrom(c)) { logger.debug("Column [" + (fieldIndex + 1) + "] type is equal to [" + "DATE" + "]"); Date val = (Date) f.getValue(); cell.setCellValue(val); cell.setCellStyle(cellStyleDate); } else { logger.warn("Column [" + (fieldIndex + 1) + "] type is equal to [" + "???" + "]"); String val = f.getValue().toString(); cell.setCellValue(createHelper.createRichTextString(val)); cell.setCellType(this.getCellTypeString()); } } } rownum++; } }
From source file:it.eng.spagobi.engines.worksheet.exporter.WorkSheetXLSExporter.java
License:Mozilla Public License
public int setHeader(Sheet sheet, JSONObject header, CreationHelper createHelper, Workbook wb, Drawing patriarch, int sheetRow) throws JSONException, IOException { String title = header.getString(TITLE); String imgName = header.optString(IMG); String imagePosition = header.getString(POSITION); CellStyle cellStyle = this.buildHeaderTitleCellStyle(sheet); if (title != null && !title.equals("")) { Row row = sheet.createRow(sheetRow); sheetRow++;/*w ww .j ava 2 s . c o m*/ Cell cell = row.createCell(6); cell.setCellValue(createHelper.createRichTextString(title)); cell.setCellType(this.getCellTypeString()); cell.setCellStyle(cellStyle); } if (imgName != null && !imgName.equals("") && !imgName.equals("null")) { File img = getImage(imgName); String imgNameUpperCase = imgName.toUpperCase(); int impgType = getImageType(imgNameUpperCase); int c = 2; int colend = 3; if (imagePosition != null && !imagePosition.equals("")) { if (imagePosition.equals(LEFT)) { c = 0; colend = 1; } else if (imagePosition.equals(RIGHT)) { c = 4; colend = 5; } } if (impgType != 0) { for (int i = 0; i < 4; i++) { sheet.createRow(sheetRow + i); } setImageIntoWorkSheet(wb, patriarch, img, c, colend, sheetRow, 4, impgType); sheetRow = sheetRow + 4; } } return sheetRow; }
From source file:it.eng.spagobi.engines.worksheet.exporter.WorkSheetXLSExporter.java
License:Mozilla Public License
public int setFooter(Sheet sheet, JSONObject footer, CreationHelper createHelper, Workbook wb, Drawing patriarch, int sheetRow) throws JSONException, IOException { String title = footer.getString(TITLE); String imgName = footer.optString(IMG); String imagePosition = footer.getString(POSITION); CellStyle cellStyle = buildHeaderTitleCellStyle(sheet); if (title != null && !title.equals("")) { Row row = sheet.createRow(sheetRow); sheetRow++;/* w w w. j a v a 2 s .co m*/ Cell cell = row.createCell(6); cell.setCellValue(createHelper.createRichTextString(title)); cell.setCellType(this.getCellTypeString()); cell.setCellStyle(cellStyle); } if (imgName != null && !imgName.equals("") && !imgName.equals("null")) { File img = getImage(imgName); String imgNameUpperCase = imgName.toUpperCase(); int impgType = getImageType(imgNameUpperCase); int c = 2; int colend = 3; if (imagePosition != null && !imagePosition.equals("")) { if (imagePosition.equals(LEFT)) { c = 0; colend = 1; } else if (imagePosition.equals(RIGHT)) { c = 4; colend = 5; } } if (impgType != 0) { setImageIntoWorkSheet(wb, patriarch, img, c, colend, sheetRow, 4, impgType); sheetRow = sheetRow + 4; } } return sheetRow; }
From source file:it.eng.spagobi.engines.worksheet.services.export.ExportWorksheetAction.java
License:Mozilla Public License
public void exportMetadataToXLS(Workbook wb, WorkSheetXLSExporter exporter, CreationHelper createHelper, JSONArray metadataPropertiesJSON, JSONArray parametersJSON) throws Exception { int FIRST_ROW = 0; int FIRST_COLUMN = 0; int rowCount = 0; JSONArray technicalMetadataProperty; JSONArray shortBusinessMetadataProperty; JSONArray longBusinessMetadataProperty; org.apache.poi.ss.usermodel.Sheet sheet = wb .createSheet(EngineMessageBundle.getMessage("worksheet.export.metadata.title", this.getLocale())); sheet.setColumnWidth(FIRST_COLUMN, 256 * 25); sheet.setColumnWidth(FIRST_COLUMN + 1, 256 * 90); CellStyle headerCellStyle = exporter.buildMetadataTitleCellStyle(sheet); CellStyle metaNameCellStyle = exporter.buildMetadataNameCellStyle(sheet); CellStyle metaValueCellStyle = exporter.buildMetadataValueCellStyle(sheet); Row row;/* w ww. j a va 2 s. co m*/ Cell nameCell; Cell valueCell; Cell headerCell; String text; technicalMetadataProperty = new JSONArray(); shortBusinessMetadataProperty = new JSONArray(); longBusinessMetadataProperty = new JSONArray(); if (metadataPropertiesJSON != null) { for (int i = 0; i < metadataPropertiesJSON.length(); i++) { JSONObject metadataProperty = metadataPropertiesJSON.getJSONObject(i); String metadataPropertyType = metadataProperty.getString("meta_type"); if ("SHORT_TEXT".equalsIgnoreCase(metadataPropertyType)) { shortBusinessMetadataProperty.put(metadataProperty); continue; } else if ("LONG_TEXT".equalsIgnoreCase(metadataPropertyType)) { longBusinessMetadataProperty.put(metadataProperty); continue; } else { technicalMetadataProperty.put(metadataProperty); } } } if (technicalMetadataProperty.length() > 0) { row = sheet.createRow((FIRST_ROW) + rowCount); headerCell = row.createCell(FIRST_COLUMN + 1); headerCell = row.createCell(FIRST_COLUMN + 1); text = EngineMessageBundle.getMessage("worksheet.export.metadata.technicalMetadata", this.getLocale()); headerCell.setCellValue(createHelper.createRichTextString(text)); headerCell.setCellType(exporter.getCellTypeString()); headerCell.setCellStyle(headerCellStyle); rowCount++; for (int i = 0; i < technicalMetadataProperty.length(); i++) { JSONObject metadataProperty = technicalMetadataProperty.getJSONObject(i); String metadataPropertyName = metadataProperty.getString("meta_name"); String metadataPropertyValue = metadataProperty.getString("meta_content"); row = sheet.createRow((FIRST_ROW) + rowCount); nameCell = row.createCell(FIRST_COLUMN); nameCell.setCellValue(createHelper.createRichTextString(metadataPropertyName)); nameCell.setCellType(exporter.getCellTypeString()); nameCell.setCellStyle(metaNameCellStyle); valueCell = row.createCell(FIRST_COLUMN + 1); valueCell.setCellValue(createHelper.createRichTextString(metadataPropertyValue)); valueCell.setCellType(exporter.getCellTypeString()); valueCell.setCellStyle(metaValueCellStyle); rowCount++; } rowCount = rowCount + 2; } if (shortBusinessMetadataProperty.length() + longBusinessMetadataProperty.length() > 0) { row = sheet.createRow((FIRST_ROW) + rowCount); headerCell = row.createCell(FIRST_COLUMN + 1); headerCell = row.createCell(FIRST_COLUMN + 1); text = EngineMessageBundle.getMessage("worksheet.export.metadata.businessMetadata", this.getLocale()); headerCell.setCellValue(createHelper.createRichTextString(text)); headerCell.setCellType(exporter.getCellTypeString()); headerCell.setCellStyle(headerCellStyle); rowCount++; for (int i = 0; i < shortBusinessMetadataProperty.length(); i++, rowCount++) { JSONObject metadataProperty = shortBusinessMetadataProperty.getJSONObject(i); String metadataPropertyName = metadataProperty.getString("meta_name"); String metadataPropertyValue = metadataProperty.getString("meta_content"); row = sheet.createRow((FIRST_ROW) + rowCount); nameCell = row.createCell(FIRST_COLUMN); nameCell.setCellValue(createHelper.createRichTextString(metadataPropertyName)); nameCell.setCellType(exporter.getCellTypeString()); nameCell.setCellStyle(metaNameCellStyle); valueCell = row.createCell(FIRST_COLUMN + 1); valueCell.setCellValue(createHelper.createRichTextString(metadataPropertyValue)); valueCell.setCellType(exporter.getCellTypeString()); valueCell.setCellStyle(metaValueCellStyle); } for (int i = 0; i < longBusinessMetadataProperty.length(); i++, rowCount++) { JSONObject metadataProperty = longBusinessMetadataProperty.getJSONObject(i); String metadataPropertyName = metadataProperty.getString("meta_name"); String metadataPropertyValue = metadataProperty.getString("meta_content"); row = sheet.createRow((FIRST_ROW) + rowCount); nameCell = row.createCell(FIRST_COLUMN); nameCell.setCellValue(createHelper.createRichTextString(metadataPropertyName)); nameCell.setCellType(exporter.getCellTypeString()); nameCell.setCellStyle(metaNameCellStyle); valueCell = row.createCell(FIRST_COLUMN + 1); valueCell.setCellValue(createHelper.createRichTextString(metadataPropertyValue)); valueCell.setCellType(exporter.getCellTypeString()); valueCell.setCellStyle(metaValueCellStyle); } rowCount = rowCount + 2; } if (parametersJSON.length() > 0) { row = sheet.createRow((FIRST_ROW) + rowCount); headerCell = row.createCell(FIRST_COLUMN + 1); headerCell = row.createCell(FIRST_COLUMN + 1); text = EngineMessageBundle.getMessage("worksheet.export.metadata.analyticalDrivers", this.getLocale()); headerCell.setCellValue(createHelper.createRichTextString(text)); headerCell.setCellType(exporter.getCellTypeString()); headerCell.setCellStyle(headerCellStyle); rowCount++; Drawing drawing = sheet.createDrawingPatriarch(); for (int i = 0; i < parametersJSON.length(); i++) { JSONObject parameterJSON = parametersJSON.getJSONObject(i); String name = parameterJSON.getString("name"); String value = parameterJSON.getString("value"); String description = parameterJSON.optString("description"); row = sheet.createRow((FIRST_ROW) + rowCount); nameCell = row.createCell(FIRST_COLUMN); nameCell.setCellValue(createHelper.createRichTextString(name)); nameCell.setCellType(exporter.getCellTypeString()); nameCell.setCellStyle(metaNameCellStyle); valueCell = row.createCell(FIRST_COLUMN + 1); if (StringUtilities.isNotEmpty(description)) { valueCell.setCellValue(createHelper.createRichTextString(description)); ClientAnchor anchor = createHelper.createClientAnchor(); anchor.setCol1(valueCell.getColumnIndex()); anchor.setCol2(valueCell.getColumnIndex() + 1); anchor.setRow1(row.getRowNum()); anchor.setRow2(row.getRowNum() + 3); Comment comment = drawing.createCellComment(anchor); RichTextString str = createHelper.createRichTextString(value); comment.setString(str); comment.setAuthor("SpagoBI"); valueCell.setCellComment(comment); } else { valueCell.setCellValue(createHelper.createRichTextString(value)); } valueCell.setCellType(exporter.getCellTypeString()); valueCell.setCellStyle(metaValueCellStyle); rowCount++; } } }
From source file:it.eng.spagobi.engines.worksheet.services.export.ExportWorksheetAction.java
License:Mozilla Public License
private int fillFiltersInfo(Map<String, List<String>> filters, Workbook wb, org.apache.poi.ss.usermodel.Sheet sheet, WorkSheetXLSExporter exporter, WhereField splittingWhereField, CreationHelper createHelper, int beginRowHeaderData, int beginColumnHeaderData) { int sheetRow = beginRowHeaderData; if (filters != null && !filters.isEmpty()) { CellStyle titleCellStyle = exporter.buildFiltersTitleCellStyle(sheet); CellStyle contentCellStyle = exporter.buildFiltersValuesCellStyle(sheet); // table title Row row = sheet.createRow(sheetRow); // row for filters title Cell cell = row.createCell(beginColumnHeaderData); cell.setCellType(exporter.getCellTypeString()); String text = EngineMessageBundle.getMessage("worksheet.export.filters.info.title", this.getLocale()); cell.setCellValue(createHelper.createRichTextString(text)); cell.setCellStyle(titleCellStyle); sheetRow++;//from www. j a v a 2 s .c o m // table content Iterator<String> it = filters.keySet().iterator(); while (it.hasNext()) { // Filter name and values String aKey = it.next(); List<String> values = filters.get(aKey); String[] array = values.toArray(new String[] {}); String allFilterValues = StringUtils.join(array, FiltersInfoJSONDecorator.VALUES_SEPARATOR); Row aRow = sheet.createRow(sheetRow); Cell aCell = aRow.createCell(beginColumnHeaderData); aCell.setCellType(exporter.getCellTypeString()); aCell.setCellValue(aKey + " : " + allFilterValues); aCell.setCellStyle(contentCellStyle); sheetRow++; } } return sheetRow; }
From source file:it.inspired.exporter.ExcelExporter.java
License:Open Source License
private void setCell(Cell cell, Object obj) { if (obj == null) { cell.setCellValue(""); } else if (obj instanceof Date) { cell.setCellValue((Date) obj); cell.setCellStyle(dateStyle);//w w w.j a v a2 s . c o m } else if (obj instanceof Boolean) { cell.setCellValue((Boolean) obj); } else if (obj instanceof Integer || obj instanceof Long) { cell.setCellValue(Double.parseDouble(obj.toString())); cell.setCellStyle(integerStyle); } else if (obj instanceof Double) { cell.setCellValue(Double.parseDouble(obj.toString())); cell.setCellStyle(doubleStyle); } else if (obj instanceof BigDecimal) { cell.setCellValue(Double.parseDouble(obj.toString())); cell.setCellStyle(bigDecimalStyle); } else { CreationHelper helper = workbook.getCreationHelper(); cell.setCellValue(helper.createRichTextString(obj.toString())); } }
From source file:it.redev.parco.ext.ExportableModelEntityQuery.java
License:Open Source License
private void setCell(Cell cell, Object obj) { if (obj == null) { cell.setCellValue(""); } else if (obj instanceof Date) { cell.setCellValue((Date) obj); CellStyle style = workbook.createCellStyle(); CreationHelper helper = workbook.getCreationHelper(); style.setDataFormat(helper.createDataFormat().getFormat(dateFormat)); cell.setCellStyle(style);//from w ww .jav a 2 s.c o m } else if (obj instanceof Boolean) { cell.setCellValue((Boolean) obj); } else if (obj instanceof Integer) { cell.setCellValue(Double.parseDouble(obj.toString())); DataFormat format = workbook.createDataFormat(); CellStyle style = workbook.createCellStyle(); style.setDataFormat(format.getFormat("0")); cell.setCellStyle(style); } else if (obj instanceof Double) { cell.setCellValue(Double.parseDouble(obj.toString())); DataFormat format = workbook.createDataFormat(); CellStyle style = workbook.createCellStyle(); style.setDataFormat(format.getFormat("0.00")); cell.setCellStyle(style); } else if (obj instanceof BigDecimal) { cell.setCellValue(Double.parseDouble(obj.toString())); DataFormat format = workbook.createDataFormat(); CellStyle style = workbook.createCellStyle(); style.setDataFormat(format.getFormat("#,##0.0000")); cell.setCellStyle(style); } else { CreationHelper helper = workbook.getCreationHelper(); cell.setCellValue(helper.createRichTextString(obj.toString())); } }
From source file:javafxapplication7.Main_controller.java
private void exportExcel() throws FileNotFoundException, IOException { setBounds(0, 0, 500, 500);//from w w w . java 2s . co m JFileChooser dialog = new JFileChooser(); dialog.setFileSelectionMode(DIRECTORIES_ONLY); dialog.showOpenDialog(this); File file = dialog.getSelectedFile(); setVisible(true); System.out.println(file); if (file != null) { setVisible(false); Workbook wb = new HSSFWorkbook(); //Workbook wb = new XSSFWorkbook(); CreationHelper createHelper = wb.getCreationHelper(); Sheet sheet = wb.createSheet("new sheet"); // Create a row and put some cells in it. Rows are 0 based. for (int i = 0; i < usersData.size(); i++) { Row row = sheet.createRow((short) i); // Or do it on one line. row.createCell(0).setCellValue(createHelper.createRichTextString(String.valueOf("P-41"))); row.createCell(1).setCellValue(true); } // Write the output to a file FileOutputStream fileOut = new FileOutputStream(file + "/Export.xls"); wb.write(fileOut); fileOut.close(); } }