/** * 获取单元格式样式 * * @param wb * @param color * @return */ private static HSSFCellStyle getCellStyle(HSSFWorkbook wb, int color) { if (STYLE_MAP.get(color) != null) { return STYLE_MAP.get(color); } HSSFCellStyle style = wb.createCellStyle(); if (color != -1) { style.setFillForegroundColor(Short.valueOf(color + "")); style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); style.setAlignment(CellStyle.ALIGN_CENTER); } style.setBorderBottom(CellStyle.BORDER_THIN); style.setBorderLeft(CellStyle.BORDER_THIN); style.setBorderRight(CellStyle.BORDER_THIN); style.setBorderTop(CellStyle.BORDER_THIN); style.setBottomBorderColor(HSSFColor.BLACK.index); style.setLeftBorderColor(HSSFColor.BLACK.index); style.setRightBorderColor(HSSFColor.BLACK.index); style.setTopBorderColor(HSSFColor.BLACK.index); STYLE_MAP.put(color, style); return style; }
private HSSFCellStyle createHSSFCellStyle(Workbook wb, int[] bgColor, int[] fontColor, int fontSize) { HSSFWorkbook workbook = (HSSFWorkbook) wb; HSSFPalette palette = workbook.getCustomPalette(); palette.setColorAtIndex((short) 9, (byte) fontColor[0], (byte) fontColor[1], (byte) fontColor[2]); palette.setColorAtIndex((short) 10, (byte) bgColor[0], (byte) bgColor[1], (byte) bgColor[2]); HSSFFont titleFont = workbook.createFont(); titleFont.setCharSet(HSSFFont.DEFAULT_CHARSET); titleFont.setFontName("宋体"); titleFont.setColor((short) 9); titleFont.setBold(true); titleFont.setFontHeightInPoints((short) fontSize); HSSFCellStyle titleStyle = (HSSFCellStyle) createBorderCellStyle(workbook, true); titleStyle.setFont(titleFont); titleStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); titleStyle.setFillForegroundColor((short) 10); titleStyle.setAlignment(HorizontalAlignment.CENTER); titleStyle.setVerticalAlignment(VerticalAlignment.CENTER); return titleStyle; }
/** * 单元格小平对齐 */ private String convertAlignToHtml(short alignment) { String align = "left"; switch (alignment) { case HSSFCellStyle.ALIGN_LEFT: { align = "left"; } break; case HSSFCellStyle.ALIGN_CENTER: { align = "center"; } break; case HSSFCellStyle.ALIGN_RIGHT: { align = "right"; } break; } return align; }
/** * 单元格垂直对齐 */ private String convertVerticalAlignToHtml(short verticalAlignment) { String align = "middle"; switch (verticalAlignment) { case HSSFCellStyle.VERTICAL_BOTTOM: { align = "bottom"; } break; case HSSFCellStyle.VERTICAL_CENTER: { align = "center"; } break; case HSSFCellStyle.VERTICAL_TOP: { align = "top"; } break; } return align; }
/** * 设置边框 * * @param border_status <br> * BORDER_BOTTOM 底边框 <br> * BORDER_LEFT 左边框 <br> * BORDER_RIGHT 右边框 <br> * BORDER_TOP 顶边框 <br> * @return */ public ExcelStyle border(Short... border_status) { List<Short> shorts = new ArrayList<>(); for (Short st : border_status) { shorts.add(st); } if (shorts.size() < 1) return this; if (shorts.contains(BORDER_BOTTOM)) { this.cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); } if (shorts.contains(BORDER_LEFT)) { this.cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN); } if (shorts.contains(BORDER_RIGHT)) { this.cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN); } if (shorts.contains(BORDER_TOP)) { this.cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN); } return this; }
/** */ protected void createColumnHeaders() { final HSSFRow headersRow = this.sheet.createRow(0); final HSSFFont font = this.workbook.createFont(); font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); final HSSFCellStyle style = this.workbook.createCellStyle(); style.setFont(font); style.setAlignment(HSSFCellStyle.ALIGN_CENTER); int counter = 1; for (int i = 0; i < this.model.getColumnCount(); i++) { final HSSFCell cell = headersRow.createCell(counter++); // cell.setEncoding(HSSFCell.ENCODING_UTF_16); cell.setCellValue(this.model.getColumnName(i)); cell.setCellStyle(style); } }
/** * * @param rowIndex * int */ protected void createRow(final int rowIndex) { final HSSFRow row = this.sheet.createRow(rowIndex + 1); // since the // rows in // excel starts from 1 // not 0 final HSSFCellStyle style = this.workbook.createCellStyle(); style.setAlignment(HSSFCellStyle.ALIGN_CENTER); int counter = 1; for (int i = 0; i < this.model.getColumnCount(); i++) { final HSSFCell cell = row.createCell(counter++); // cell.setEncoding(HSSFCell.ENCODING_UTF_16); final Object value = this.model.getValueAt(rowIndex, i); setValue(cell, value); cell.setCellStyle(style); } }
/** * Write the value to the cell. Override this method if you have complex data types that may need to be exported. * @param value the value of the cell * @param cell the cell to write it to */ protected void writeCell(Object value, HSSFCell cell, HSSFWorkbook wb) { if (value instanceof Number) { Number num = (Number) value; cell.setCellValue(num.doubleValue()); } else if (value instanceof Date) { HSSFCellStyle cellStyle = wb.createCellStyle(); cellStyle.setDataFormat( wb.getCreationHelper().createDataFormat().getFormat("dd/MM/yyyy HH:mm")); cell.setCellStyle(cellStyle); cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC); cell.setCellValue((Date) value); } else if (value instanceof Calendar) { cell.setCellValue((Calendar) value); } else { cell.setCellValue(new HSSFRichTextString(escapeColumnValue(value))); } }
/** * @param newSheet the sheet to create from the copy. * @param sheet the sheet to copy. * @param copyStyle true copy the style. */ public static void copySheets(HSSFSheet newSheet, HSSFSheet sheet, boolean copyStyle) { int maxColumnNum = 0; Map<Integer, HSSFCellStyle> styleMap = (copyStyle) ? new HashMap<Integer, HSSFCellStyle>() : null; for (int i = sheet.getFirstRowNum(); i <= sheet.getLastRowNum(); i++) { HSSFRow srcRow = sheet.getRow(i); HSSFRow destRow = newSheet.createRow(i); if (srcRow != null) { Util.copyRow(sheet, newSheet, srcRow, destRow, styleMap); if (srcRow.getLastCellNum() > maxColumnNum) { maxColumnNum = srcRow.getLastCellNum(); } } } for (int i = 0; i <= maxColumnNum; i++) { newSheet.setColumnWidth(i, sheet.getColumnWidth(i)); } //Util.copyPictures(newSheet,sheet) ; }
private boolean createHeader(WorkbookGeneratorContext context, HSSFSheet sheet, HSSFCellStyle style) { if (context.headerNames == null || context.headerNames.isEmpty()) { return false; } int headerRowIndex = 0; HSSFRow rowHeader = sheet.createRow(headerRowIndex); for (int i = 0; i < context.headerNames.size(); i++) { HSSFCell cell = rowHeader.createCell(i); sheet.autoSizeColumn((short) i); cell.setCellStyle(style); cell.setCellValue(new HSSFRichTextString(context.headerNames.get(i))); } return true; }
protected HSSFCellStyle getLoadedCellStyle( FillPatternType mode, short backcolor, HorizontalAlignment horizontalAlignment, VerticalAlignment verticalAlignment, short rotation, HSSFFont font, BoxStyle box, boolean isWrapText, boolean isCellLocked, boolean isCellHidden, boolean isShrinkToFit ) { StyleInfo style = new StyleInfo(mode, backcolor, horizontalAlignment, verticalAlignment, rotation, font, box, isWrapText, isCellLocked, isCellHidden, isShrinkToFit); return getLoadedCellStyle(style); }
protected void addBlankElement(HSSFCellStyle cellStyle, boolean repeatValue, String currentColumnName) throws JRException { if (columnNames.size() > 0) { if (columnNames.contains(currentColumnName) && !currentRow.containsKey(currentColumnName) && isColumnReadOnTime(currentRow, currentColumnName)) { // the column is for export but was not read yet and comes in the expected order addBlankCell(cellStyle, currentRow, currentColumnName); } else if ( (columnNames.contains(currentColumnName) && !currentRow.containsKey(currentColumnName) && !isColumnReadOnTime(currentRow, currentColumnName)) // the column is for export, was not read yet, but it is read after it should be || (columnNames.contains(currentColumnName) && currentRow.containsKey(currentColumnName)) ) { // the column is for export and was already read if(rowIndex == 1 && getCurrentItemConfiguration().isWriteHeader()) { writeReportHeader(); } writeCurrentRow(currentRow, repeatedValues); currentRow.clear(); addBlankCell(cellStyle, currentRow, currentColumnName); } // set auto fill columns if(repeatValue) { if (repeatValue && currentColumnName != null && currentColumnName.length() > 0 && cellStyle != null) { addBlankCell(cellStyle, repeatedValues, currentColumnName); } } else { repeatedValues.remove(currentColumnName); } } }
public CellSettings( CellType cellType, HSSFCellStyle cellStyle, Object cellValue, String formula, Hyperlink link ) { this.cellType = cellType; this.cellStyle = cellStyle; this.cellValue = cellValue; this.formula = formula; this.link = link; }
public void importValues( CellType cellType, HSSFCellStyle cellStyle, Object cellValue, String formula, Hyperlink link ) { this.cellType = cellType; this.cellStyle = cellStyle; this.cellValue = cellValue; this.formula = formula; this.link = link; }
public static boolean createHeader(HSSFWorkbook workbook, HSSFSheet sheet,String[] header) throws Exception{ boolean flag = false; try { HSSFCellStyle cellStyle = workbook.createCellStyle(); cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); HSSFRow head_row = sheet.createRow(0); // 创建行 // 操作head for (short h = 0; h < header.length; h++) { createcsv(head_row, h, header[h], cellStyle); } flag = true; } catch (Exception e) { logger.error(sheet.getSheetName() + " : 抬头标题创建失败"); throw e; } return flag; }
public static boolean createTop(HSSFWorkbook workbook, HSSFSheet sheet,String[] header, HSSFCellStyle cellStyle) throws Exception{ boolean flag = false; try { HSSFRow head_row = sheet.createRow(1); // 创建行 // 操作head for (short h = 0; h < header.length; h++) { createcsv(head_row, h, header[h], cellStyle); } flag = true; } catch (Exception e) { logger.error(sheet.getSheetName() + " : 抬头标题创建失败"); throw e; } return flag; }
/** * 表头条件 * @param sheet * @param t * @param cellCount * @return */ void writeCondtions(HSSFSheet sheet){ T t = getConditions(); if (t!=null) { HSSFRow row = sheet.createRow(getRowNumber()); row.setHeight((short) 500); CellRangeAddress cra = new CellRangeAddress(getRowNumber(), getRowNumber(), 0, getColumnJson().size()); sheet.addMergedRegion(cra); HSSFCell cell = row.createCell(0); HSSFCellStyle style = cell.getCellStyle(); style.setAlignment(HorizontalAlignment.CENTER); style.setVerticalAlignment(VerticalAlignment.CENTER); style.setWrapText(true); cell.setCellStyle(style); setCellValue(cell, formatCondition(t)); addRowNumber(); } }
public static List<HSSFCellStyle> copyCellStyle(final HSSFWorkbook workbook, final HSSFRow row) { final List<HSSFCellStyle> cellStyleList = new ArrayList<HSSFCellStyle>(); for (int colNum = row.getFirstCellNum(); colNum <= row.getLastCellNum(); colNum++) { final HSSFCell cell = row.getCell(colNum); if (cell != null) { final HSSFCellStyle style = cell.getCellStyle(); final HSSFCellStyle newCellStyle = copyCellStyle(workbook, style); cellStyleList.add(newCellStyle); } else { cellStyleList.add(null); } } return cellStyleList; }
private HSSFCellStyle createMatrixCellStyle(final HSSFWorkbook workbook, final HSSFCellStyle matrixHeaderTemplateCellStyle, final boolean top, final boolean right, final boolean bottom, final boolean left) { final HSSFCellStyle cellStyle = POIUtils.copyCellStyle(workbook, matrixHeaderTemplateCellStyle); if (top) { cellStyle.setBorderTop(CellStyle.BORDER_THIN); } if (right) { cellStyle.setBorderRight(CellStyle.BORDER_THIN); } if (bottom) { cellStyle.setBorderBottom(CellStyle.BORDER_THIN); } if (left) { cellStyle.setBorderLeft(CellStyle.BORDER_THIN); } return cellStyle; }
/** * * @Title: getCellStyle * @Description: TODO(设置表头样式) * @param wb * @return */ private CellStyle getHeadCellStyle(Workbook wb) { CellStyle style = wb.createCellStyle(); Font font = wb.createFont(); font.setFontName("宋体"); font.setFontHeightInPoints((short) 12);// 设置字体大小 font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 加粗 style.setFillForegroundColor(HSSFColor.LIME.index);// 设置背景色 style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); style.setAlignment(HSSFCellStyle.SOLID_FOREGROUND);// 让单元格居中 style.setAlignment(HSSFCellStyle.ALIGN_CENTER); style.setBorderTop(CellStyle.BORDER_THIN); style.setBorderLeft(CellStyle.BORDER_THIN); style.setBorderRight(CellStyle.BORDER_THIN); style.setBorderBottom(CellStyle.BORDER_THIN); // style.setWrapText(true);//设置自动换行 style.setFont(font); return style; }
public static HSSFCellStyle createCellStyleForColumnHeading(HSSFWorkbook workBook) { HSSFCellStyle cellStyle = workBook.createCellStyle(); HSSFFont fontObj = workBook.createFont(); cellStyle.setBorderBottom(BorderStyle.THIN); cellStyle.setBorderTop(BorderStyle.THIN); cellStyle.setBorderLeft(BorderStyle.THIN); cellStyle.setBorderRight(BorderStyle.THIN); cellStyle.setWrapText(true); cellStyle.setAlignment(HorizontalAlignment.CENTER); cellStyle.setFillBackgroundColor(Short.valueOf("22").shortValue()); cellStyle.setFillPattern(FillPatternType.BIG_SPOTS); cellStyle.setFillForegroundColor(Short.valueOf("22").shortValue()); cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); fontObj.setFontName("Calibri"); fontObj.setFontHeightInPoints(Short.valueOf("12").shortValue()); fontObj.setBold(true); fontObj.setColor(Short.valueOf("8").shortValue()); cellStyle.setFont(fontObj); return cellStyle; }
public static List<HSSFCellStyle> copyCellStyle(HSSFWorkbook workbook, HSSFRow row) { List<HSSFCellStyle> cellStyleList = new ArrayList<HSSFCellStyle>(); for (int colNum = row.getFirstCellNum(); colNum <= row.getLastCellNum(); colNum++) { HSSFCell cell = row.getCell(colNum); if (cell != null) { HSSFCellStyle style = cell.getCellStyle(); HSSFCellStyle newCellStyle = copyCellStyle(workbook, style); cellStyleList.add(newCellStyle); } else { cellStyleList.add(null); } } return cellStyleList; }
private HSSFCellStyle createMatrixCellStyle(HSSFWorkbook workbook, HSSFCellStyle matrixHeaderTemplateCellStyle, boolean top, boolean right, boolean bottom, boolean left) { HSSFCellStyle cellStyle = POIUtils.copyCellStyle(workbook, matrixHeaderTemplateCellStyle); if (top) { cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN); } if (right) { cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN); } if (bottom) { cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); } if (left) { cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN); } return cellStyle; }
private HSSFCellStyle getHeaderStyle(final int col) { String key = "header-" + col; HSSFCellStyle cs_header = m_styles.get(key); if (cs_header == null) { HSSFFont font_header = getFont(true); cs_header = m_workbook.createCellStyle(); cs_header.setFont(font_header); cs_header.setBorderLeft((short)2); cs_header.setBorderTop((short)2); cs_header.setBorderRight((short)2); cs_header.setBorderBottom((short)2); cs_header.setDataFormat(HSSFDataFormat.getBuiltinFormat("text")); cs_header.setWrapText(true); m_styles.put(key, cs_header); } return cs_header; }
private void addDocumentIntoTheRow(Document document, HSSFRow row, LinkedHashMap<String, Integer> columPoints,LinkedHashMap<String,String>map, HSSFCellStyle style) throws Exception { // TODO Auto-generated method stub HSSFCell cell; //int count=0; for(String key : map.keySet()){ String colum=map.get(key); String mathodName="get"+key; Object args[]=new Object[0]; Object result=null; String value; if((result=ReflectionUtil.invokeMethod(document, mathodName, args))!=null){ value=result.toString(); cell=row.createCell(columPoints.get(colum)); cell.setCellStyle(style); cell.setCellValue(value); }else{ cell=row.createCell(columPoints.get(colum)); cell.setCellStyle(style); cell.setCellValue(""); } } }
private LinkedHashMap<String,Integer> createSheetHead(HSSFSheet sheet, HSSFCellStyle style,LinkedHashMap<String,String> map) { HSSFRow row = sheet.createRow(2); row.setHeight((short)1000); HSSFCell cell; int count=0; LinkedHashMap<String,Integer> columPoints=new LinkedHashMap<String,Integer>(); for(String value:map.values()){ cell=row.createCell(count); cell.setCellValue(value); cell.setCellStyle(style); columPoints.put(value, count); count++; } return columPoints; }
private HSSFCellStyle createSecondTitleStyle(HSSFWorkbook wb){ HSSFCellStyle style = wb.createCellStyle(); style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); style.setAlignment(HSSFCellStyle.ALIGN_LEFT); style.setBorderBottom(HSSFCellStyle.BORDER_NONE); style.setBorderLeft(HSSFCellStyle.BORDER_NONE); style.setBorderRight(HSSFCellStyle.BORDER_NONE); style.setBorderTop(HSSFCellStyle.BORDER_NONE); style.setWrapText(true); HSSFFont font = wb.createFont(); //font.setFontHeightInPoints((short)20); font.setFontName("����"); style.setFont(font); return style; }
/** * @Description: 生成excel表格 单元格内容的样式 * @param workbook * @return * * @History * 1. 2014-12-19 linwb 创建方法 */ private HSSFCellStyle generateContStyle(HSSFWorkbook workbook) { HSSFCellStyle cellStyle = workbook.createCellStyle(); cellStyle.setFillForegroundColor(contCellBackgroundColor); cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); cellStyle.setBorderBottom(contBorderBottom); cellStyle.setBorderLeft(contBorderLeft); cellStyle.setBorderRight(contBorderRight); cellStyle.setBorderTop(contBorderTop); cellStyle.setAlignment(contCellTextAlign); cellStyle.setVerticalAlignment(contCellVehicleAlign); // 生成字体 HSSFFont font = workbook.createFont(); font.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL); // 把字体应用到当前的样式 cellStyle.setFont(font); return cellStyle; }
/** * Estilo de cabecalho. * @param wb * @return retorna o estilo da celula. * @author Ekler Paulino de Mattos. */ private HSSFCellStyle estiloCabecalho(HSSFWorkbook wb){ HSSFCellStyle cellStyle = wb.createCellStyle(); cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THICK); cellStyle.setBorderRight(HSSFCellStyle.BORDER_THICK); cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THICK); cellStyle.setBorderTop(HSSFCellStyle.BORDER_THICK); cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND ); // cellStyle.setFillForegroundColor(HSSFColor.GREY_40_PERCENT.index); cellStyle.setFillForegroundColor(HSSFColor.LIGHT_YELLOW.index); cellStyle.setAlignment(HSSFCellStyle.ALIGN_LEFT); HSSFFont font = cellStyle.getFont(wb); font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); font.setColor(HSSFFont.COLOR_NORMAL); font.setFontName(HSSFFont.FONT_ARIAL); cellStyle.setFont(font); return cellStyle; }
/** * Estilo dos campos dos dados. * @param wb * @return retorna o estilo da celula. * @author Ekler Paulino de Mattos. */ private HSSFCellStyle estiloDadosConsultado(HSSFWorkbook wb){ HSSFCellStyle cellStyle = wb.createCellStyle(); cellStyle.setBorderBottom(HSSFCellStyle.BORDER_MEDIUM); cellStyle.setBorderRight(HSSFCellStyle.BORDER_MEDIUM); cellStyle.setBorderLeft(HSSFCellStyle.BORDER_MEDIUM); cellStyle.setBorderTop(HSSFCellStyle.BORDER_MEDIUM); // plano de fundo - para que funcione deve se // definido antes um padrao de preenchimento. cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND ); //cellStyle.setFillForegroundColor(HSSFColor.AQUA.index); cellStyle.setFillForegroundColor(HSSFColor.WHITE.index); cellStyle.setAlignment(HSSFCellStyle.ALIGN_LEFT); HSSFFont font = cellStyle.getFont(wb); font.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL); font.setColor(HSSFFont.COLOR_NORMAL); font.setFontName(HSSFFont.FONT_ARIAL); cellStyle.setFont(font); return cellStyle; }
/** * Estilo dos campos dos dados. * @param wb * @return retorna o estilo da celula. * @author Ekler Paulino de Mattos. */ private HSSFCellStyle estiloDadosRetorno(HSSFWorkbook wb){ HSSFCellStyle cellStyle = wb.createCellStyle(); cellStyle.setBorderBottom(HSSFCellStyle.BORDER_MEDIUM); cellStyle.setBorderRight(HSSFCellStyle.BORDER_MEDIUM); cellStyle.setBorderLeft(HSSFCellStyle.BORDER_MEDIUM); cellStyle.setBorderTop(HSSFCellStyle.BORDER_MEDIUM); // plano de fundo - para que funcione deve se // definido antes um padrao de preenchimento. cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND ); //cellStyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index); cellStyle.setFillForegroundColor(HSSFColor.PALE_BLUE.index); cellStyle.setAlignment(HSSFCellStyle.ALIGN_LEFT); HSSFFont font = cellStyle.getFont(wb); font.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL); font.setColor(HSSFFont.COLOR_NORMAL); font.setFontName(HSSFFont.FONT_ARIAL); cellStyle.setFont(font); return cellStyle; }
/** * Estilo de cabecalho. * @param wb * @return retorna o estilo da celula. * @author Ekler Paulino de Mattos. */ private HSSFCellStyle estiloCabecalho(HSSFWorkbook wb){ if(this.estiloCabecalho==null){ this.estiloCabecalho = wb.createCellStyle(); this.estiloCabecalho.setBorderBottom(HSSFCellStyle.BORDER_THIN); this.estiloCabecalho.setBorderRight(HSSFCellStyle.BORDER_THIN); this.estiloCabecalho.setBorderLeft(HSSFCellStyle.BORDER_THIN); this.estiloCabecalho.setBorderTop(HSSFCellStyle.BORDER_THIN); this.estiloCabecalho.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND ); // cellStyle.setFillForegroundColor(HSSFColor.GREY_40_PERCENT.index); this.estiloCabecalho.setFillForegroundColor(HSSFColor.LIGHT_YELLOW.index); this.estiloCabecalho.setAlignment(HSSFCellStyle.ALIGN_LEFT); HSSFFont font = this.estiloCabecalho.getFont(wb); font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); font.setColor(HSSFFont.COLOR_NORMAL); font.setFontName(HSSFFont.FONT_ARIAL); this.estiloCabecalho.setFont(font); } return this.estiloCabecalho; }
protected void setNumericCell(HSSFCell cell, BigDecimal value, HSSFWorkbook workbook) { if(logger.isDebugEnabled()) logger.debug("setNumericCell(cell={}, value={}, workbook={}) - start", new Object[] {cell, value, workbook} ); cell.setCellValue( ((BigDecimal)value).doubleValue() ); HSSFDataFormat df = workbook.createDataFormat(); int scale = ((BigDecimal)value).scale(); short format; if(scale <= 0){ format = df.getFormat("####"); } else { String zeros = createZeros(((BigDecimal)value).scale()); format = df.getFormat("####." + zeros); } if(logger.isDebugEnabled()) logger.debug("Using format '{}' for value '{}'.", String.valueOf(format), value); HSSFCellStyle cellStyleNumber = workbook.createCellStyle(); cellStyleNumber.setDataFormat(format); cell.setCellStyle(cellStyleNumber); }
protected void addRow(HSSFWorkbook workbook, HSSFSheet sheet, Object value, int row, int column) { HSSFRow hssfRow = sheet.getRow(row); hssfRow = (hssfRow == null) ? sheet.createRow(row) : hssfRow; HSSFCell cell = hssfRow.getCell(column); cell = (cell == null) ? hssfRow.createCell(column) : cell; String cellValue = (value == null) ? "" : value.toString(); DecimalFormat decimalFormatter = new DecimalFormat("###,###,###,##0.00"); try { double doubleValue = decimalFormatter.parse(cellValue).doubleValue(); DataFormat dataFormat = workbook.createDataFormat(); HSSFCellStyle cellStyle = workbook.createCellStyle(); cellStyle.setDataFormat(dataFormat.getFormat("###,###,###,##0.00")); cell.setCellValue(doubleValue); } catch (ParseException e) { cell.setCellValue(cellValue); } formatCell(workbook, cell); }
/** * Adds header to the sheet * @param worksheet the worksheet * @param sheetName name of sheet * @param title title of the sheet (can be null or empty) * @param attributes header attributes */ public static void addHeader(Worksheet worksheet, String sheetName, String title, List<String> attributes) { HSSFSheet sheet = worksheet.getSheets().get(sheetName); HSSFCellStyle cellStyle = worksheet.getCellStyle(); HSSFRow row = null; int rowNum = 0; if (title != null && ! title.isEmpty()) { row = sheet.createRow(rowNum++); row.createCell(0).setCellValue(title); char y = (char) ((int) 'A' + attributes.size() - 1); String cell = "A1:" + y + "1"; sheet.addMergedRegion(CellRangeAddress.valueOf(cell)); } row = sheet.createRow(rowNum); for (int i=0; i<attributes.size(); i++) { addCell(row,cellStyle, i, attributes.get(i)); } }