List of usage examples for org.apache.poi.ss.usermodel CellStyle setDataFormat
void setDataFormat(short fmt);
From source file:me.utils.excel.ExportExcelme.java
License:Open Source License
/** * ?/*from w w w . ja v a2 s .c o m*/ * @param row * @param column ? * @param val * @param align ??1?23?? * @return ? */ public Cell addCell(Row row, int column, Object val, int align) { Cell cell = row.createCell(column); CellStyle style = styles.get("data" + (align >= 1 && align <= 3 ? align : "")); try { if (val == null) { cell.setCellValue(""); } else if (val instanceof String) { cell.setCellValue((String) val); } else if (val instanceof Integer) { cell.setCellValue((Integer) val); } else if (val instanceof Long) { cell.setCellValue((Long) val); } else if (val instanceof Double) { cell.setCellValue((Double) val); } else if (val instanceof Float) { cell.setCellValue((Float) val); } else if (val instanceof Date) { DataFormat format = wb.createDataFormat(); style.setDataFormat(format.getFormat("yyyy-MM-dd")); cell.setCellValue((Date) val); } else { } } catch (Exception ex) { log.info("Set cell value [" + row.getRowNum() + "," + column + "] error: " + ex.toString()); cell.setCellValue(val.toString()); } cell.setCellStyle(style); return cell; }
From source file:model.Reports.java
private static Map<String, CellStyle> createStyles(Workbook wb) { Map<String, CellStyle> styles = new HashMap<>(); CellStyle style; Font titleFont = wb.createFont(); titleFont.setFontHeightInPoints((short) 18); titleFont.setBoldweight(Font.BOLDWEIGHT_BOLD); style = wb.createCellStyle();//w w w . jav a2s .c o m style.setAlignment(CellStyle.ALIGN_CENTER); style.setVerticalAlignment(CellStyle.VERTICAL_CENTER); style.setFont(titleFont); styles.put("title", style); Font monthFont = wb.createFont(); monthFont.setFontHeightInPoints((short) 11); monthFont.setColor(IndexedColors.WHITE.getIndex()); style = wb.createCellStyle(); style.setAlignment(CellStyle.ALIGN_CENTER); style.setVerticalAlignment(CellStyle.VERTICAL_CENTER); style.setFillForegroundColor(IndexedColors.GREY_50_PERCENT.getIndex()); style.setFillPattern(CellStyle.SOLID_FOREGROUND); style.setFont(monthFont); style.setWrapText(true); styles.put("header", style); style = wb.createCellStyle(); style.setAlignment(CellStyle.ALIGN_CENTER); style.setWrapText(true); style.setBorderRight(CellStyle.BORDER_THIN); style.setRightBorderColor(IndexedColors.BLACK.getIndex()); style.setBorderLeft(CellStyle.BORDER_THIN); style.setLeftBorderColor(IndexedColors.BLACK.getIndex()); style.setBorderTop(CellStyle.BORDER_THIN); style.setTopBorderColor(IndexedColors.BLACK.getIndex()); style.setBorderBottom(CellStyle.BORDER_THIN); style.setBottomBorderColor(IndexedColors.BLACK.getIndex()); styles.put("cell", style); style = wb.createCellStyle(); style.setAlignment(CellStyle.ALIGN_CENTER); style.setVerticalAlignment(CellStyle.VERTICAL_CENTER); style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex()); style.setFillPattern(CellStyle.SOLID_FOREGROUND); style.setDataFormat(wb.createDataFormat().getFormat("0.00")); styles.put("formula", style); style = wb.createCellStyle(); style.setAlignment(CellStyle.ALIGN_CENTER); style.setVerticalAlignment(CellStyle.VERTICAL_CENTER); style.setFillForegroundColor(IndexedColors.GREY_40_PERCENT.getIndex()); style.setFillPattern(CellStyle.SOLID_FOREGROUND); style.setDataFormat(wb.createDataFormat().getFormat("0.00")); styles.put("formula_2", style); return styles; }
From source file:mx.edu.um.mateo.activos.dao.impl.ActivoDaoHibernate.java
License:Open Source License
@SuppressWarnings("unchecked") @Override// w w w . j a v a 2s. co m public void arreglaFechas(OutputStream out) { log.debug("Arreglando fechas"); Date inicio = new Date(); XSSFWorkbook wb = new XSSFWorkbook(); CreationHelper createHelper = wb.getCreationHelper(); CellStyle cellStyle = wb.createCellStyle(); cellStyle.setDataFormat(createHelper.createDataFormat().getFormat("dd/mm/yyyy")); XSSFSheet fechas = wb.createSheet("FECHAS-ANTERIORES"); int fechasRow = 0; XSSFSheet fechas2 = wb.createSheet("FECHAS-POSTERIORES"); int fechas2Row = 0; Transaction tx = null; try { tx = currentSession().beginTransaction(); Query update = currentSession() .createQuery("update Activo set fechaCompra = :fechaCompra where id = :id"); Query query = currentSession().createQuery( "select new Activo(a.id, a.descripcion, a.fechaCompra, a.tipoActivo.cuenta.id.idCtaMayor, a.centroCosto.id.idCosto, a.codigo) from Activo a where a.fechaCompra < :fechaCompra order by a.tipoActivo.cuenta.id.idCtaMayor, a.centroCosto.id.idCosto, a.codigo"); SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); query.setDate("fechaCompra", sdf.parse("01/01/1970")); List<Activo> activos = query.list(); int cont = 0; for (Activo activo : activos) { Calendar cal1 = Calendar.getInstance(); cal1.setTime(activo.getFechaCompra()); if (cal1.get(Calendar.YEAR) < 10) { log.debug("Pasando al ao 2000 {} - {}", activo.getDescripcion(), activo.getFechaCompra()); cal1.add(Calendar.YEAR, 2000); update.setDate("fechaCompra", cal1.getTime()); update.setLong("id", activo.getId()); update.executeUpdate(); XSSFRow renglon = fechas.createRow(fechasRow++); renglon.createCell(0).setCellValue(activo.getTipoActivoCuenta()); renglon.createCell(1).setCellValue(activo.getCentroCostoCuenta()); renglon.createCell(2).setCellValue(activo.getCodigo()); renglon.createCell(3).setCellValue(activo.getDescripcion()); renglon.createCell(4).setCellValue(sdf.format(activo.getFechaCompra())); Cell cell = renglon.createCell(5); cell.setCellValue(cal1.getTime()); cell.setCellStyle(cellStyle); } else if (cal1.get(Calendar.YEAR) < 100) { log.debug("Pasando al ao 1900 {} - {}", activo.getDescripcion(), activo.getFechaCompra()); cal1.add(Calendar.YEAR, 1900); update.setDate("fechaCompra", cal1.getTime()); update.setLong("id", activo.getId()); update.executeUpdate(); XSSFRow renglon = fechas.createRow(fechasRow++); renglon.createCell(0).setCellValue(activo.getTipoActivoCuenta()); renglon.createCell(1).setCellValue(activo.getCentroCostoCuenta()); renglon.createCell(2).setCellValue(activo.getCodigo()); renglon.createCell(3).setCellValue(activo.getDescripcion()); renglon.createCell(4).setCellValue(sdf.format(activo.getFechaCompra())); Cell cell = renglon.createCell(5); cell.setCellValue(cal1.getTime()); cell.setCellStyle(cellStyle); } else if (cal1.get(Calendar.YEAR) >= 1900 && cal1.get(Calendar.YEAR) <= 1912) { log.debug("Pasando al ao 2000 {} - {}", activo.getDescripcion(), activo.getFechaCompra()); cal1.add(Calendar.YEAR, 100); update.setDate("fechaCompra", cal1.getTime()); update.setLong("id", activo.getId()); update.executeUpdate(); XSSFRow renglon = fechas.createRow(fechasRow++); renglon.createCell(0).setCellValue(activo.getTipoActivoCuenta()); renglon.createCell(1).setCellValue(activo.getCentroCostoCuenta()); renglon.createCell(2).setCellValue(activo.getCodigo()); renglon.createCell(3).setCellValue(activo.getDescripcion()); renglon.createCell(4).setCellValue(sdf.format(activo.getFechaCompra())); Cell cell = renglon.createCell(5); cell.setCellValue(cal1.getTime()); cell.setCellStyle(cellStyle); } cont++; } currentSession().flush(); query = currentSession().createQuery( "select new Activo(a.id, a.descripcion, a.fechaCompra, a.tipoActivo.cuenta.id.idCtaMayor, a.centroCosto.id.idCosto, a.codigo) from Activo a where a.fechaCompra > :fechaCompra order by a.tipoActivo.cuenta.id.idCtaMayor, a.centroCosto.id.idCosto, a.codigo"); query.setDate("fechaCompra", new Date()); activos = query.list(); for (Activo activo : activos) { Calendar cal1 = Calendar.getInstance(); cal1.setTime(activo.getFechaCompra()); if (cal1.get(Calendar.YEAR) < 2020) { log.debug("Quitandole 10 anios {} - {}", activo.getDescripcion(), activo.getFechaCompra()); cal1.add(Calendar.YEAR, -10); update.setDate("fechaCompra", cal1.getTime()); update.setLong("id", activo.getId()); update.executeUpdate(); XSSFRow renglon = fechas2.createRow(fechas2Row++); renglon.createCell(0).setCellValue(activo.getTipoActivoCuenta()); renglon.createCell(1).setCellValue(activo.getCentroCostoCuenta()); renglon.createCell(2).setCellValue(activo.getCodigo()); renglon.createCell(3).setCellValue(activo.getDescripcion()); renglon.createCell(4).setCellValue(sdf.format(activo.getFechaCompra())); Cell cell = renglon.createCell(5); cell.setCellValue(cal1.getTime()); cell.setCellStyle(cellStyle); } else if (cal1.get(Calendar.YEAR) >= 2020) { log.debug("Pasando al ao 1900 {} - {}", activo.getDescripcion(), activo.getFechaCompra()); cal1.add(Calendar.YEAR, -100); update.setDate("fechaCompra", cal1.getTime()); update.setLong("id", activo.getId()); update.executeUpdate(); XSSFRow renglon = fechas2.createRow(fechas2Row++); renglon.createCell(0).setCellValue(activo.getTipoActivoCuenta()); renglon.createCell(1).setCellValue(activo.getCentroCostoCuenta()); renglon.createCell(2).setCellValue(activo.getCodigo()); renglon.createCell(3).setCellValue(activo.getDescripcion()); renglon.createCell(4).setCellValue(sdf.format(activo.getFechaCompra())); Cell cell = renglon.createCell(5); cell.setCellValue(cal1.getTime()); cell.setCellStyle(cellStyle); } cont++; } currentSession().flush(); tx.commit(); log.debug("Termino actualizando {} de {} en {}", new Object[] { cont, activos.size(), ((new Date().getTime() - inicio.getTime()) / 1000) }); wb.write(out); } catch (ParseException | HibernateException | IOException e) { log.error("No se pudieron arreglar las fechas de los activos", e); tx.rollback(); throw new RuntimeException("No se pudieron arreglar las fechas de los actios", e); } }
From source file:nc.noumea.mairie.appock.services.impl.ExportExcelServiceImpl.java
License:Open Source License
private void construitEnteteNumeroMarcheEtSoldeCommande(Workbook wb, Map<Service, List<ArticleDemande>> map, AbstractEntity abstractEntity, Service service, Sheet sheet) { Row row = createRowGeneric(sheet, LIGNE_NUMERO_MARCHE_ET_SOLDE_COMMANDE, 500); Cell cell = row.createCell(0);/*from ww w. j av a2 s. com*/ if (abstractEntity instanceof SousMarche) { cell.setCellValue("March Nouma n: " + ((SousMarche) abstractEntity).getNumero()); cell.setCellStyle(createCellWithBorderAndColor(wb, BorderStyle.THIN, IndexedColors.TAN, false)); } // Cell Montant de la commande cell = row.createCell(6); int prixTotalService = 0; for (ArticleDemande articleDemande : map.get(service)) { prixTotalService += articleDemande.getTotalPrixCommande(); } cell.setCellValue(prixTotalService); CellStyle style = createCellWithBorderAndColor(wb, BorderStyle.THIN, null, false); style.setDataFormat(HSSFDataFormat.getBuiltinFormat("#,##0")); cell.setCellStyle(style); }
From source file:nc.noumea.mairie.appock.services.impl.ExportExcelServiceImpl.java
License:Open Source License
private void remplitLigneArticle(Workbook wb, List<ArticleDemande> listeArticleDemande, Sheet sheet) { int numRow = 10; Map<ArticleCatalogue, Integer> mapArticleQuantite = new HashMap<>(); for (ArticleDemande articleDemande : listeArticleDemande) { ArticleCatalogue articleCatalogue = articleDemande.getArticleCatalogue(); Integer quantite = mapArticleQuantite.get(articleCatalogue); if (quantite != null) { mapArticleQuantite.replace(articleCatalogue, quantite + articleDemande.getQuantiteCommande()); } else {//from w w w . j a v a2s .c o m mapArticleQuantite.put(articleCatalogue, articleDemande.getQuantiteCommande()); } } List<ArticleCatalogue> listeArticleCatalogue = new ArrayList(mapArticleQuantite.keySet()); Collections.sort(listeArticleCatalogue, new ArticleCatalogueComparator()); for (ArticleCatalogue articleCatalogue : listeArticleCatalogue) { Row row = createRowGeneric(sheet, numRow, 500); row.createCell(0).setCellValue(articleCatalogue.getReference()); row.createCell(1).setCellValue(articleCatalogue.getLibelle()); row.createCell(2).setCellValue(articleCatalogue.getPrix()); row.createCell(3).setCellValue(articleCatalogue.getLibelleColisage()); row.createCell(4).setCellValue(mapArticleQuantite.get(articleCatalogue)); String strFormula = "C" + (numRow + 1) + "*E" + (numRow + 1) + ""; Cell cell = row.createCell(5); cell.setCellType(CellType.FORMULA); cell.setCellFormula(strFormula); CellStyle style = createCellWithBorderAndColor(wb, BorderStyle.THIN, IndexedColors.LIGHT_YELLOW, false); CellStyle stylePrix = createCellWithBorderAndColor(wb, BorderStyle.THIN, IndexedColors.LIGHT_YELLOW, false); stylePrix.setDataFormat(HSSFDataFormat.getBuiltinFormat("#,##0")); row.getCell(0).setCellStyle(style); row.getCell(1).setCellStyle(style); row.getCell(2).setCellStyle(stylePrix); row.getCell(3).setCellStyle(style); row.getCell(4).setCellStyle(style); row.getCell(5).setCellStyle(stylePrix); numRow++; } }
From source file:net.ceos.project.poi.annotated.core.CellStyleHandler.java
License:Apache License
/** * Apply the data format to the cell style. * //w ww . j ava 2s.c o m * @param wb * the {@link Workbook} in use * @param cell * the {@link Cell} to use * @param cs * the cell style * @param formatMask the mask to apply */ protected static void applyDataFormat(final Workbook wb, final Cell cell, final CellStyle cs, final String formatMask) { DataFormat df = dataFormatFactory.newInstance(wb); cs.setDataFormat(df.getFormat(formatMask)); cell.setCellStyle(cs); }
From source file:net.ceos.project.poi.annotated.core.CellStyleHandler.java
License:Apache License
/** * Apply cell style according the one cell style base and format mask. * // w w w.ja v a 2 s . c o m * @param configCriteria * the {@link XConfigCriteria} * @param cell * the {@link Cell} * @param cellDecoratorType * the cell decorator by default * @param maskDecoratorType * the format mask by default * @throws ElementException * given when {@link CellStyle} is not defined */ protected static void applyCellStyle(final XConfigCriteria configCriteria, final Cell cell, final String cellDecoratorType, final String maskDecoratorType) throws ElementException { CellStyle cs = configCriteria.getCellStyle(cellDecoratorType); if (maskDecoratorType != null && StringUtils.isNotBlank(configCriteria.getMask(maskDecoratorType)) && cs != null) { /* generate a key based : formatMask + decorator */ String key = configCriteria.generateCellStyleKey(cellDecoratorType, maskDecoratorType); if (!configCriteria.getCellStyleManager().containsKey(key)) { /* clone a cell style */ CellStyle csNew = cloneCellStyle(configCriteria.getWorkbook(), cs); /* initialize/apply a data format */ DataFormat df = dataFormatFactory.newInstance(configCriteria.getWorkbook()); csNew.setDataFormat(df.getFormat(configCriteria.getMask(maskDecoratorType))); configCriteria.getCellStyleManager().put(key, csNew); cs = csNew; } else { cs = configCriteria.getCellStyleManager().get(key); } /* apply cell style to a cell */ cell.setCellStyle(cs); } else { if (cs == null) { /* CASE : if the cell has no cell style base */ cs = cellStyleFactory.newInstance(configCriteria.getWorkbook()); } if (maskDecoratorType != null && StringUtils.isNotBlank(configCriteria.getMask(maskDecoratorType))) { /* CASE : if the cell has a formatMask */ DataFormat df = dataFormatFactory.newInstance(configCriteria.getWorkbook()); cs.setDataFormat(df.getFormat(configCriteria.getMask(maskDecoratorType))); } cell.setCellStyle(cs); } }
From source file:net.sourceforge.jaulp.export.excel.poi.ExportExcelUtils.java
License:Apache License
/** * Creates a new CellStyle with the given date format. * * @param workbook/*from w ww. j a v a 2 s . c o m*/ * the workbook * @param dateFormat * the date format * @return the cell style */ public static CellStyle newDateCellStyle(Workbook workbook, String dateFormat) { CellStyle dateCellStyle = workbook.createCellStyle(); dateCellStyle.setDataFormat(workbook.getCreationHelper().createDataFormat().getFormat(dateFormat)); return dateCellStyle; }
From source file:net.sourceforge.squirrel_sql.fw.gui.action.exportData.DataExportExcelWriter.java
License:Open Source License
private void makeTemporalCell(Cell retVal, Date cellObj, String format) { CreationHelper creationHelper = workbook.getCreationHelper(); CellStyle cellStyle; if (formatCache == null) { cellStyle = workbook.createCellStyle(); cellStyle.setDataFormat(creationHelper.createDataFormat().getFormat(format)); formatCache = new HashMap<String, CellStyle>(); formatCache.put(format, cellStyle); } else {/*from w w w .j a v a 2 s . co m*/ cellStyle = formatCache.get(format); if (cellStyle == null) { cellStyle = workbook.createCellStyle(); cellStyle.setDataFormat(creationHelper.createDataFormat().getFormat(format)); formatCache.put(format, cellStyle); } } retVal.setCellStyle(cellStyle); if (null != cellObj) { Calendar calendar = Calendar.getInstance(); calendar.setTime((Date) cellObj); retVal.setCellValue(calendar); } }
From source file:nz.ac.auckland.abi.formatting.poi.ModelJSONToExcel.java
License:LGPL
/** * Create a library of cell styles//from w w w.j a v a2s. c om */ private static Map<String, CellStyle> createStyles(Workbook wb) { Map<String, CellStyle> styles = new HashMap<String, CellStyle>(); CellStyle style; Font titleFont = wb.createFont(); titleFont.setFontHeightInPoints((short) 18); titleFont.setBoldweight(Font.BOLDWEIGHT_NORMAL); style = wb.createCellStyle(); style.setAlignment(CellStyle.ALIGN_CENTER); style.setVerticalAlignment(CellStyle.VERTICAL_CENTER); style.setFont(titleFont); styles.put("title", style); Font monthFont = wb.createFont(); monthFont.setFontHeightInPoints((short) 11); monthFont.setColor(IndexedColors.WHITE.getIndex()); style = wb.createCellStyle(); style.setAlignment(CellStyle.ALIGN_CENTER); style.setVerticalAlignment(CellStyle.VERTICAL_CENTER); style.setFillForegroundColor(IndexedColors.GREY_50_PERCENT.getIndex()); style.setFillPattern(CellStyle.SOLID_FOREGROUND); style.setFont(monthFont); style.setWrapText(true); styles.put("header", style); style = wb.createCellStyle(); style.setAlignment(CellStyle.ALIGN_CENTER); style.setWrapText(true); style.setBorderRight(CellStyle.BORDER_THIN); style.setRightBorderColor(IndexedColors.BLACK.getIndex()); style.setBorderLeft(CellStyle.BORDER_THIN); style.setLeftBorderColor(IndexedColors.BLACK.getIndex()); style.setBorderTop(CellStyle.BORDER_THIN); style.setTopBorderColor(IndexedColors.BLACK.getIndex()); style.setBorderBottom(CellStyle.BORDER_THIN); style.setBottomBorderColor(IndexedColors.BLACK.getIndex()); styles.put("cell", style); style = wb.createCellStyle(); style.setAlignment(CellStyle.ALIGN_CENTER); style.setVerticalAlignment(CellStyle.VERTICAL_CENTER); style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex()); style.setFillPattern(CellStyle.SOLID_FOREGROUND); style.setDataFormat(wb.createDataFormat().getFormat("0.00")); styles.put("formula", style); style = wb.createCellStyle(); style.setAlignment(CellStyle.ALIGN_CENTER); style.setVerticalAlignment(CellStyle.VERTICAL_CENTER); style.setFillForegroundColor(IndexedColors.GREY_40_PERCENT.getIndex()); style.setFillPattern(CellStyle.SOLID_FOREGROUND); style.setDataFormat(wb.createDataFormat().getFormat("0.00")); styles.put("formula_2", style); style = wb.createCellStyle(); style.setAlignment(CellStyle.ALIGN_CENTER); style.setVerticalAlignment(CellStyle.VERTICAL_CENTER); style.setFillForegroundColor(IndexedColors.RED.getIndex()); style.setFillPattern(CellStyle.SOLID_FOREGROUND); style.setDataFormat(wb.createDataFormat().getFormat("0.00")); styles.put("MAX", style); style = wb.createCellStyle(); style.setAlignment(CellStyle.ALIGN_CENTER); style.setVerticalAlignment(CellStyle.VERTICAL_CENTER); style.setFillForegroundColor(IndexedColors.BLUE.getIndex()); style.setFillPattern(CellStyle.SOLID_FOREGROUND); style.setDataFormat(wb.createDataFormat().getFormat("0.00")); styles.put("MIN", style); style = wb.createCellStyle(); style.setAlignment(CellStyle.ALIGN_CENTER); style.setVerticalAlignment(CellStyle.VERTICAL_CENTER); style.setFillForegroundColor(IndexedColors.GREEN.getIndex()); style.setFillPattern(CellStyle.SOLID_FOREGROUND); style.setDataFormat(wb.createDataFormat().getFormat("0.00")); styles.put("AVERAGE", style); style = wb.createCellStyle(); style.setAlignment(CellStyle.ALIGN_CENTER); style.setVerticalAlignment(CellStyle.VERTICAL_CENTER); style.setFillForegroundColor(IndexedColors.LIGHT_ORANGE.getIndex()); style.setFillPattern(CellStyle.SOLID_FOREGROUND); style.setDataFormat(wb.createDataFormat().getFormat("0.00")); styles.put("STDEV", style); style = wb.createCellStyle(); style.setAlignment(CellStyle.ALIGN_CENTER); style.setVerticalAlignment(CellStyle.VERTICAL_CENTER); style.setFillForegroundColor(IndexedColors.LIGHT_ORANGE.getIndex()); style.setFillPattern(CellStyle.SOLID_FOREGROUND); style.setDataFormat(wb.createDataFormat().getFormat("0.00")); styles.put("AVGSERIES", style); return styles; }