List of usage examples for org.apache.poi.ss.usermodel CellStyle setDataFormat
void setDataFormat(short fmt);
From source file:org.dashbuilder.dataset.service.DataSetExportServicesImpl.java
License:Apache License
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);//from ww w . j av a 2 s .co m 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; }
From source file:org.databene.formats.xls.BeanXLSWriter.java
License:Open Source License
private void writeHeaderRow(E bean, HSSFSheet sheet) { HSSFRow headerRow = sheet.createRow(0); for (int i = 0; i < beanProperties.size(); i++) { PropFormat prop = beanProperties.get(i); // write column header String componentName = prop.getName(); headerRow.createCell(i).setCellValue(new HSSFRichTextString(componentName)); // apply pattern if (prop.getPattern() != null) { HSSFDataFormat dataFormat = workbook.createDataFormat(); CellStyle columnStyle = workbook.createCellStyle(); columnStyle.setDataFormat(dataFormat.getFormat(prop.getPattern())); sheet.setDefaultColumnStyle(i, columnStyle); }/*from w w w . j a v a 2 s .c o m*/ } }
From source file:org.databene.platform.xls.XLSEntityExporter.java
License:Open Source License
private void writeHeaderRow(Entity entity, HSSFSheet sheet) { HSSFRow headerRow = sheet.createRow(0); int colnum = 0; for (Map.Entry<String, Object> component : getComponents(entity)) { String componentName = component.getKey(); headerRow.createCell(colnum).setCellValue(new HSSFRichTextString(componentName)); ComponentDescriptor cd = entity.descriptor().getComponent(componentName); PrimitiveType primitiveType; if (cd.getTypeDescriptor() instanceof SimpleTypeDescriptor) primitiveType = ((SimpleTypeDescriptor) cd.getTypeDescriptor()).getPrimitiveType(); else/*from w w w . j a va2 s . c om*/ throw new UnsupportedOperationException("Can only export simple type attributes, " + "failed to export " + entity.type() + '.' + cd.getName()); Class<?> javaType = (primitiveType != null ? primitiveType.getJavaType() : String.class); String formatString = null; if (BeanUtil.isIntegralNumberType(javaType)) formatString = getIntegralPattern(); else if (BeanUtil.isDecimalNumberType(javaType)) formatString = getDecimalPattern(); else if (Time.class.isAssignableFrom(javaType)) formatString = getTimePattern(); else if (Timestamp.class.isAssignableFrom(javaType)) formatString = getTimestampPattern(); else if (Date.class.isAssignableFrom(javaType)) formatString = getDatePattern(); if (formatString != null) { HSSFDataFormat dataFormat = workbook.createDataFormat(); CellStyle columnStyle = workbook.createCellStyle(); columnStyle.setDataFormat(dataFormat.getFormat(formatString)); sheet.setDefaultColumnStyle(colnum, columnStyle); } colnum++; } }
From source file:org.dbunit.dataset.excel.XlsDataSetWriter.java
License:Open Source License
protected static CellStyle findCellStyle(Workbook workbook, Short formatCode, Map<Short, CellStyle> map) { CellStyle cellStyle = map.get(formatCode); if (cellStyle == null) { cellStyle = workbook.createCellStyle(); cellStyle.setDataFormat(formatCode); map.put(formatCode, cellStyle);//from w w w . ja v a 2s . c om } return cellStyle; }
From source file:org.devgateway.eudevfin.sheetexp.poi.PoiObjectCreator.java
License:Open Source License
private CellStyle createBodyDateCellStyle() { final CellStyle style = this.workbook.createCellStyle(); final Font font = this.workbook.createFont(); style.setFont(font);// ww w.jav a 2 s. c o m final DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, this.locale); final String pattern = ((SimpleDateFormat) df).toPattern(); final HSSFDataFormat hssfDataFormat = this.workbook.createDataFormat(); style.setDataFormat(hssfDataFormat.getFormat(pattern)); return style; }
From source file:org.dhatim.fastexcel.Benchmarks.java
License:Apache License
private int poiPopulate(org.apache.poi.ss.usermodel.Workbook wb) throws Exception { Sheet ws = wb.createSheet("Sheet 1"); CellStyle dateStyle = wb.createCellStyle(); dateStyle.setDataFormat(wb.getCreationHelper().createDataFormat().getFormat("yyyy-mm-dd hh:mm:ss")); for (int r = 0; r < NB_ROWS; ++r) { Row row = ws.createRow(r);//from www . j a va2 s.c o m row.createCell(0).setCellValue(r); row.createCell(1).setCellValue(Integer.toString(r % 1000)); row.createCell(2).setCellValue(r / 87.0); Cell c = row.createCell(3); c.setCellStyle(dateStyle); c.setCellValue(new Date(1549915044)); } CountingOutputStream count = new CountingOutputStream(new NullOutputStream()); wb.write(count); return count.getCount(); }
From source file:org.drugepi.table.ExcelUtils.java
License:Mozilla Public License
public static void formatNumericCell(Cell cell, String s, String[] excelFormats) { CellStyle origStyle = cell.getCellStyle(); CellStyle newStyle = cell.getRow().getSheet().getWorkbook().createCellStyle(); DataFormat newFormat = cell.getRow().getSheet().getWorkbook().createDataFormat(); newStyle.cloneStyleFrom(origStyle);/* w w w .ja v a2 s .c o m*/ newStyle.setAlignment(CellStyle.ALIGN_LEFT); int numDecimals = -1; if (s != null) { int decimalIndex = s.indexOf("."); if (decimalIndex >= 0) numDecimals = s.length() - decimalIndex - 1; } if (numDecimals < 0) numDecimals = 0; if ((numDecimals >= 0) && (numDecimals <= 8)) newStyle.setDataFormat(newFormat.getFormat(excelFormats[numDecimals])); cell.setCellStyle(newStyle); }
From source file:org.eclipse.emfforms.internal.spreadsheet.core.converter.EMFFormsSpreadsheetMultiAttributeConverter_Test.java
License:Open Source License
@Before public void before() { converter = new EMFFormsSpreadsheetMultiAttributeConverter(); reportService = mock(ReportService.class); databinding = mock(EMFFormsDatabindingEMF.class); domainObject = mock(EObject.class); dmr = mock(VDomainModelReference.class); final Workbook wb = new HSSFWorkbook(); final CellStyle cellStyle = wb.createCellStyle(); cellStyle.setDataFormat((short) BuiltinFormats.getBuiltinFormat("text")); //$NON-NLS-1$ final Sheet sheet = wb.createSheet("test"); //$NON-NLS-1$ // Create a row and put some cells in it. Rows are 0 based. final Row row = sheet.createRow((short) 0); cell = row.createCell(0);/*w ww . j a v a 2 s . c o m*/ viewModelContext = mock(ViewModelContext.class); when(viewModelContext.getContextValue(EMFFormsCellStyleConstants.TEXT)).thenReturn(cellStyle); }
From source file:org.eclipse.emfforms.internal.spreadsheet.core.converter.EMFFormsSpreadsheetMultiReferenceConverter_Test.java
License:Open Source License
@Before public void before() { converter = new EMFFormsSpreadsheetMultiReferenceConverter(); reportService = mock(ReportService.class); databinding = mock(EMFFormsDatabindingEMF.class); domainObject = mock(EObject.class); dmr = mock(VDomainModelReference.class); final Workbook wb = new HSSFWorkbook(); final CellStyle cellStyle = wb.createCellStyle(); cellStyle.setDataFormat((short) BuiltinFormats.getBuiltinFormat("text")); //$NON-NLS-1$ final Sheet sheet = wb.createSheet("test"); //$NON-NLS-1$ // Create a row and put some cells in it. Rows are 0 based. final Row row = sheet.createRow((short) 0); cell = row.createCell(0);/*w w w. j a v a 2s. c om*/ viewModelContext = mock(ViewModelContext.class); when(viewModelContext.getContextValue(EMFFormsCellStyleConstants.TEXT)).thenReturn(cellStyle); }
From source file:org.eclipse.emfforms.internal.spreadsheet.core.converter.EMFFormsSpreadsheetSingleReferenceConverter_Test.java
License:Open Source License
@Before public void before() { converter = new EMFFormsSpreadsheetSingleReferenceConverter(); reportService = mock(ReportService.class); databinding = mock(EMFFormsDatabindingEMF.class); domainObject = mock(EObject.class); dmr = mock(VDomainModelReference.class); final Workbook wb = new HSSFWorkbook(); final CellStyle cellStyle = wb.createCellStyle(); cellStyle.setDataFormat((short) BuiltinFormats.getBuiltinFormat("text")); //$NON-NLS-1$ final Sheet sheet = wb.createSheet("test"); //$NON-NLS-1$ // Create a row and put some cells in it. Rows are 0 based. final Row row = sheet.createRow((short) 0); cell = row.createCell(0);//from ww w. j a v a 2s. c om viewModelContext = mock(ViewModelContext.class); when(viewModelContext.getContextValue(EMFFormsCellStyleConstants.TEXT)).thenReturn(cellStyle); }