@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 wb * @param color * @param foreGround * @return */ public static XSSFCellStyle createBackgroundColorXSSFCellStyle(XSSFWorkbook wb,XSSFColor color,short foreGround){ String message="XSSFWorkbook must not be null!"; Objects.requireNonNull(wb, () -> message); XSSFCellStyle cellStyle=wb.createCellStyle(); cellStyle.setWrapText(true); cellStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER); cellStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER); cellStyle.setBorderBottom(BorderStyle.THIN); cellStyle.setBorderLeft(BorderStyle.THIN); cellStyle.setBorderTop(BorderStyle.THIN); cellStyle.setBorderRight(BorderStyle.THIN); cellStyle.setFillForegroundColor(color); cellStyle.setFillPattern(foreGround); return cellStyle; }
public static void insertDedicatedStyleCell(XSSFRow row,int col,XSSFCellStyle style,String value){ String message="XSSFRow or XSSFCellStyle must not be null!"; Objects.requireNonNull(row, () -> message); Objects.requireNonNull(style, () -> message); XSSFCell cell=createCellIfNotPresent(row,col); setCellProperties(createCellIfNotPresent(row, col), value, style); }
private void buildSheet(SXSSFWorkbook wb,VariableCategory vc,XSSFCellStyle style){ String name=vc.getName(); Sheet sheet=wb.createSheet(name); Row row=sheet.createRow(0); List<Variable> variables=vc.getVariables(); for(int i=0;i<variables.size();i++){ sheet.setColumnWidth(i,4000); Cell cell=row.createCell(i); Variable var=variables.get(i); cell.setCellValue(var.getLabel()); cell.setCellStyle(style); } }
/** * 设置表头的单元格样式 * * @return */ public XSSFCellStyle getHeadStyle() { // 创建单元格样式 XSSFCellStyle cellStyle = wb.createCellStyle(); // 设置单元格的背景颜色为淡蓝色 cellStyle.setFillForegroundColor(HSSFColor.PALE_BLUE.index); // 创建单元格内容显示不下时自动换行 //cellStyle.setWrapText(true); // 设置单元格字体样式 XSSFFont font = wb.createFont(); // 设置字体加粗 font.setFontName("宋体"); font.setFontHeight((short) 200); cellStyle.setFont(font); return cellStyle; }
/** * Fixed all properties suitable for cell-related style. * * @param workbook Excel Workbook * @param boardStyle all properties suitable on the style of a cell * @param font a font * @return the customized style */ protected static CellStyle getCellStyle(Workbook workbook, TableStyle boardStyle, Font font) { XSSFCellStyle cellStyle = (XSSFCellStyle) workbook.createCellStyle(); if (boardStyle.getFillColor() != null) { cellStyle.setFillForegroundColor(boardStyle.getFillColor()); cellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND); } cellStyle.setBorderLeft(boardStyle.getCellBorderLeft()); cellStyle.setBorderRight(boardStyle.getCellBorderRight()); cellStyle.setBorderTop(boardStyle.getCellBorderTop()); cellStyle.setBorderBottom(boardStyle.getCellBorderBottom()); cellStyle.setAlignment(boardStyle.getAlignment()); cellStyle.setBorderColor(BorderSide.LEFT, boardStyle.getBorderColor()); cellStyle.setBorderColor(BorderSide.RIGHT, boardStyle.getBorderColor()); cellStyle.setBorderColor(BorderSide.TOP, boardStyle.getBorderColor()); cellStyle.setBorderColor(BorderSide.BOTTOM, boardStyle.getBorderColor()); if (font != null) { cellStyle.setFont(font); } return cellStyle; }
/** * @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(XSSFSheet newSheet, XSSFSheet sheet, boolean copyStyle) { int maxColumnNum = 0; Map<Integer, XSSFCellStyle> styleMap = (copyStyle) ? new HashMap<Integer, XSSFCellStyle>() : null; for (int i = sheet.getFirstRowNum(); i <= sheet.getLastRowNum(); i++) { XSSFRow srcRow = sheet.getRow(i); XSSFRow 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) ; }
@Override public void setBorderStyle(Direction d, BorderStyle borderStyle) { org.apache.poi.ss.usermodel.BorderStyle poiBorder = getPoiBorder(borderStyle); final Color color = borderStyle.getColor(); XSSFColor poiColor = color == null ? null : ((PoiXssfWorkbook) workbook).getPoiColor(color); switch (d) { case NORTH: ((XSSFCellStyle) poiCellStyle).setTopBorderColor(poiColor); poiCellStyle.setBorderTop(poiBorder); break; case EAST: ((XSSFCellStyle) poiCellStyle).setRightBorderColor(poiColor); poiCellStyle.setBorderRight(poiBorder); break; case SOUTH: ((XSSFCellStyle) poiCellStyle).setBottomBorderColor(poiColor); poiCellStyle.setBorderBottom(poiBorder); break; case WEST: ((XSSFCellStyle) poiCellStyle).setLeftBorderColor(poiColor); poiCellStyle.setBorderLeft(poiBorder); break; default: throw new IllegalArgumentException(); } }
/** * @param color * @param font * @return CellStyle */ protected XSSFCellStyle getCellStyle(XSSFColor color, Font font) { XSSFCellStyle style = wb.createCellStyle(); style.setFillForegroundColor(color); style.setFillPattern(CellStyle.SOLID_FOREGROUND); style.setAlignment(CellStyle.ALIGN_CENTER); style.setVerticalAlignment(CellStyle.VERTICAL_CENTER); style.setWrapText(true); // new line style.setBorderBottom(CellStyle.BORDER_THIN); style.setBottomBorderColor(IndexedColors.BLACK.getIndex()); style.setBorderLeft(CellStyle.BORDER_THIN); style.setLeftBorderColor(IndexedColors.BLACK.getIndex()); style.setBorderRight(CellStyle.BORDER_THIN); style.setRightBorderColor(IndexedColors.BLACK.getIndex()); style.setBorderTop(CellStyle.BORDER_THIN); style.setTopBorderColor(IndexedColors.BLACK.getIndex()); if(font != null) style.setFont(font); return style; }
/** * 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); } }
private int addLangCell(Row header, TmxSegement segment) { int CellNum = header.getLastCellNum(); if (-1 == CellNum) { CellNum = 0; } Cell createCell = header.createCell(CellNum); CellStyle cellStyle = wb.createCellStyle(); XSSFFont headerFont = (XSSFFont) wb.createFont(); headerFont.setBold(true); cellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex()); cellStyle.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND); cellStyle.setFont(headerFont); createCell.setCellValue(segment.getLangCode()); createCell.setCellStyle(cellStyle); sh.setColumnWidth(CellNum, (100 * 7 + 5) / 7 * 256); return CellNum; }
@Override public void addBackgroundColourToStyle(Workbook workbook, CellStyle style, String colour) { if(colour == null) { return ; } if(IStyle.TRANSPARENT_VALUE.equals(colour)) { return ; } if(style instanceof XSSFCellStyle) { XSSFCellStyle cellStyle = (XSSFCellStyle)style; XSSFColor xColour = getXColour(colour); if(xColour != null) { cellStyle.setFillForegroundColor(xColour); cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); } } }
/** * 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); } }
/** * See http://thinktibits.blogspot.co.uk/2012/12/Java-POI-XLS-XLSX-Change-Cell-Font-Color-Example.html * Currently only for xlsx * @param wb * @param sheet */ private static void styleHeader(Workbook wb, Sheet sheet){ if(XSSFWorkbook.class.isInstance(wb) && XSSFSheet.class.isInstance(sheet)){ XSSFWorkbook my_workbook = (XSSFWorkbook)wb; XSSFCellStyle my_style = my_workbook.createCellStyle(); XSSFFont my_font=my_workbook.createFont(); my_font.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD); my_style.setFont(my_font); Row row = sheet.getRow(0); if(row!=null && row.getFirstCellNum()>=0){ for(int i = row.getFirstCellNum() ; i<= row.getLastCellNum();i++){ Cell cell = row.getCell(i); if(cell!=null){ cell.setCellStyle(my_style); } } } } }
/** * Creates a new line with values in the sheet. * * @param workbook * (XSSFWorkbook, required) workbook to create the row in * @param sheetRow * (XSSFSheetRow, required) sheet to create the data row in * @param sheetName * (String, required) name of the sheet * @param values * (String [], optional) if null or empty no work will be done, else the values written to the next line * @param bold * (boolean) true: the values will be set in bold font face, else normal * * @since 2.0.10 */ private void createSheetRow(XSSFWorkbook workbook, XSSFSheetRow sheetRow, String sheetName, List<String> values, boolean bold) { if (null != values && values.size() > 0) { // check if sheet exists and create if not if (null == sheetRow.sheet) { sheetRow.sheet = workbook.createSheet(sheetName); } // create cell style XSSFCellStyle cellStyle = workbook.createCellStyle(); cellStyle.setAlignment(HorizontalAlignment.CENTER); if (bold) { // Create bold font Font fontBold = workbook.createFont(); fontBold.setBoldweight(Font.BOLDWEIGHT_BOLD); cellStyle.setFont(fontBold); } // create row XSSFRow row = sheetRow.sheet.createRow(sheetRow.rowCount++); // set values for (int i = 0; i < values.size(); i++) { row.getCell(i).setCellValue(values.get(i)); row.getCell(i).setCellStyle(cellStyle); } } }
private XSSFCellStyle updateSubreportBandElementStyle(XSSFCellStyle cellStyle, BandElement bandElement, Object value, int gridRow, int gridColumn, int colSpan) { if (subreportCellStyle == null) { return cellStyle; } if (gridColumn == 0) { cellStyle.setBorderLeft(subreportCellStyle.getBorderLeft()); cellStyle.setLeftBorderColor(subreportCellStyle.getLeftBorderColor()); } else if (gridColumn+colSpan-1 == bean.getReportLayout().getColumnCount()-1) { cellStyle.setBorderRight(subreportCellStyle.getBorderRight()); cellStyle.setRightBorderColor(subreportCellStyle.getRightBorderColor()); } if (pageRow == 0) { cellStyle.setBorderTop(subreportCellStyle.getBorderTop()); cellStyle.setTopBorderColor(subreportCellStyle.getTopBorderColor()); } else if ( (pageRow+1) == getRowsCount()) { cellStyle.setBorderBottom(subreportCellStyle.getBorderBottom()); cellStyle.setBottomBorderColor(subreportCellStyle.getBottomBorderColor()); } return cellStyle; }
/** * @param cell * @param value * @param style */ public static void setCellProperties(XSSFCell cell, String value, XSSFCellStyle style) { Assert.notNull(cell, "cell must not be null!"); // fill year cell.setCellValue(value); cell.setCellStyle(style); }
/** * @param sheet * @param pos 位置【row,col】 * @param style * @param v */ public static void setCell(final XSSFWorkbook wb,final XSSFSheet sheet,int[] pos,CellStyle style,FontStyle font,String v){ XSSFRow row = sheet.getRow(pos[0]); XSSFCell cell = CellValueUtil.createCellIfNotPresent(row, pos[1]); XSSFCellStyle _style = Objects .requireNonNull(new XSSFCellStyleLib(wb).get(style), "specified style not defined!"); XSSFFont _font = Objects .requireNonNull(new XSSFFontStyleLib(wb).get(font), "specified font not defined!"); _style.setFont(_font); setCellProperties(cell, v, _style); }
public static XSSFCellStyle createXSSFCellStyle(XSSFWorkbook wb){ String message="XSSFWorkbook must not be null!"; Objects.requireNonNull(wb, () -> message); XSSFCellStyle cellStyle=wb.createCellStyle(); cellStyle.setWrapText(true); cellStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER); cellStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER); return cellStyle; }
public static XSSFCellStyle createCenterXSSFCellStyle(XSSFWorkbook wb){ String message="XSSFWorkbook must not be null!"; Objects.requireNonNull(wb, () -> message); XSSFCellStyle cellStyle=wb.createCellStyle(); cellStyle.setWrapText(true); cellStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER); cellStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER); cellStyle.setBorderBottom(BorderStyle.THIN); cellStyle.setBorderLeft(BorderStyle.THIN); cellStyle.setBorderTop(BorderStyle.THIN); cellStyle.setBorderRight(BorderStyle.THIN); return cellStyle; }
/** * @param cell * @param value * @param style */ public static void setCellProperties(XSSFCell cell,String value,XSSFCellStyle style){ Assert.notNull(cell, "cell must not be null!"); //fill year cell.setCellValue(value); cell.setCellStyle(style); }
/** * 返回副标题字体样式 * @param wb * @return */ public static XSSFCellStyle getCaptionCellStyle(XSSFWorkbook wb){ //default cellStyle XSSFCellStyle cellStyle=createXSSFCellStyle(wb); //default font XSSFFont cellfont=XSSFontUtil.createXSSFFont(wb); cellStyle.setFont(cellfont); return cellStyle; }
/** * 返回excel内容字体样式 * @param wb * @return */ public static XSSFCellStyle getcontentCellStyle(XSSFWorkbook wb){ String message="XSSFWorkbook must not be null!"; Objects.requireNonNull(wb, () -> message); XSSFCellStyle contentStyle=createCenterXSSFCellStyle(wb); //fill enlarged font XSSFFont enlargedFont=XSSFontUtil.createColorXSSFFont(wb, "新宋体", (short) 16, new XSSFColor(Color.WHITE), true); contentStyle.setFont(enlargedFont); return contentStyle; }
public XSSFCellStyle get(CellStyle k){ XSSFCellStyle v=CELL_STYLE_MAP.get(k); if(null==v){ throw new CellStyleNotFoundException("the cell style have not been defined!"); } return v; }
private static XSSFCellStyle createCellStyle(Workbook workbook) { XSSFCellStyle xstyle = (XSSFCellStyle) workbook.createCellStyle(); XSSFFont font = (XSSFFont) workbook.createFont(); font.setFontHeightInPoints((short) 11); xstyle.setFont(font); return xstyle; }
public static CellStyle createHeaderStyle(Workbook workbook) { XSSFCellStyle headerStyle = createCellStyle(workbook); XSSFColor header = new XSSFColor(new byte[]{(byte) 79, (byte) 129, (byte) 189}); headerStyle.setFillForegroundColor(header); headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); headerStyle.getFont().setColor(IndexedColors.WHITE.index); return headerStyle; }
@SuppressWarnings("unchecked") public void exportExcelTemplate(HttpServletRequest req, HttpServletResponse resp) throws Exception { List<VariableCategory> variableCategories=(List<VariableCategory>)httpSessionKnowledgeCache.get(req, VCS_KEY); if(variableCategories==null){ KnowledgeBase knowledgeBase=buildKnowledgeBase(req); variableCategories=knowledgeBase.getResourceLibrary().getVariableCategories(); } SXSSFWorkbook wb = new SXSSFWorkbook(); XSSFCellStyle style=(XSSFCellStyle)wb.createCellStyle(); Color c=new Color(147,208,15); XSSFColor xssfColor=new XSSFColor(c); style.setFillPattern(FillPatternType.SOLID_FOREGROUND); style.setFillForegroundColor(xssfColor); for(VariableCategory vc:variableCategories){ buildSheet(wb, vc,style); } resp.setContentType("application/x-xls"); resp.setHeader("Content-Disposition","attachment; filename=urule-batch-test-template.xlsx"); OutputStream outputStream=resp.getOutputStream(); wb.write(outputStream);; outputStream.flush(); outputStream.close(); }
/** * 处理数据类型 * * @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 static void applyStyleToRange(XSSFSheet sheet, XSSFCellStyle style, int rowStart, int colStart, int rowEnd, int colEnd) { for (int r = rowStart; r <= rowEnd; r++) { for (int c = colStart; c <= colEnd; c++) { XSSFRow row = sheet.getRow(r); if (row != null) { XSSFCell cell = row.getCell(c); if (cell != null) { cell.setCellStyle(style); } } } } }
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 XSSFCellStyle createXSSFCellStyle(Workbook wb, int[] bgColor, int[] fontColor, int fontSize) { SXSSFWorkbook workbook = (SXSSFWorkbook) wb; XSSFFont titleFont = (XSSFFont) workbook.createFont(); titleFont.setCharSet(HSSFFont.DEFAULT_CHARSET); titleFont.setFontName("宋体"); XSSFColor color9 = new XSSFColor(new java.awt.Color(fontColor[0], fontColor[1], fontColor[2])); XSSFColor color10 = new XSSFColor(new java.awt.Color(bgColor[0], bgColor[1], bgColor[2])); if (!(fontColor[0] == 0 && fontColor[1] == 0 && fontColor[2] == 0)) { titleFont.setColor(color9); } titleFont.setBold(true); titleFont.setFontHeightInPoints((short) fontSize); XSSFCellStyle titleStyle = (XSSFCellStyle) createBorderCellStyle(workbook, true); titleStyle.setFont(titleFont); titleStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); titleStyle.setFillForegroundColor(color10); titleStyle.setAlignment(HorizontalAlignment.CENTER); titleStyle.setVerticalAlignment(VerticalAlignment.CENTER); return titleStyle; }
public static XSSFCellStyle createTitleXSSFCellStyle(XSSFWorkbook wb){ String message="XSSFWorkbook must not be null!"; Objects.requireNonNull(wb, () -> message); XSSFCellStyle cellStyle=wb.createCellStyle(); cellStyle.setWrapText(true); cellStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER); cellStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER); cellStyle.setBorderBottom(BorderStyle.THIN); cellStyle.setBorderLeft(BorderStyle.THIN); cellStyle.setBorderTop(BorderStyle.THIN); cellStyle.setBorderRight(BorderStyle.THIN); cellStyle.setFillForegroundColor(new XSSFColor( new Color(75, 172, 198))); cellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND); return cellStyle; }