问题
I want to apply colour to cell as well as Format Cell value(e.g. Date,Amount).But when I am applying two Cell Style only the last style is gets applied on cell.
//before this colourCellStyle and dateCellStyle are the formatting style
cell9 = row.createCell(9);
cell9.setCellValue(getLoadDate());
cell9.setCellStyle(colourCellStyle);
cell9.setCellStyle(dateCellStyle);
回答1:
Multiple cell styles cannot be applied to a single Cell
. The last cell style applied will overwrite any pre-existing cell style on the Cell
. Setting multiple CellStyle
s won't combined the set attributes of each style.
The solution is to create another CellStyle
that has the desired attributes of both of the other CellStyle
s. You can use the cloneStyleFrom method to start with the attributes of one CellStyle
.
CellStyle combined = workbook.createCellStyle();
combined.cloneStyleFrom(colourCellStyle);
combined.setDataFormat(dateCellStyle.getDataFormat());
// You can copy other attributes to "combined" here if desired.
cell9.setCellStyle(combined);
This technique can be generalized to clone any existing cell style and copy individual attributes from a second existing cell style. As always, reuse any existing CellStyle
s, but if a different combination of attributes is required, then you must create and use a new CellStyle
.
回答2:
You can create a map of styles and then you can use different styles throughout the java program.
For example
Map<String, CellStyle> cellStyles = new HashMap<String, CellStyle>();
DataFormat dataFormat = workbook.createDataFormat();
XSSFCellStyle cellStyle;
XSSFFont font;
cellStyle = workbook.createCellStyle();
cellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
cellStyle.setAlignment(cellStyle.ALIGN_CENTER_SELECTION);
font = workbook.createFont();
font.setFontHeightInPoints((short)16);
font.setFontName("Calibri");
cellStyle.setFont(font);
cellStyles.put("header_cell_style", cellStyle);
cellStyle = workbook.createCellStyle();
cellStyle.setAlignment(cellStyle.ALIGN_CENTER_SELECTION);
font = workbook.createFont();
font.setFontHeightInPoints((short)12);
font.setFontName("Calibri");
cellStyle.setFont(font);
cellStyles.put("normal_cell_style", cellStyle);
cellStyle = workbook.createCellStyle();
cellStyle.setAlignment(cellStyle.ALIGN_CENTER_SELECTION);
cellStyle.setDataFormat(dataFormat.getFormat("dd-mmm-yyyy"));
font = workbook.createFont();
font.setFontHeightInPoints((short)12);
font.setFontName("Calibri");
cellStyle.setFont(font);
cellStyles.put("date_cell_style", cellStyle);
cellStyle = workbook.createCellStyle();
cellStyle.setAlignment(cellStyle.ALIGN_CENTER_SELECTION);
cellStyle.setDataFormat(dataFormat.getFormat("dd-mmm-yyyy"));
font = workbook.createFont();
font.setFontHeightInPoints((short)16);
font.setFontName("Calibri");
cellStyle.setFont(font);
cellStyles.put("header_date_cell_style", cellStyle);
return cellStyles;
and then use this map like
Map<String, CellStyle> multipleCellStyles = createMultipleExcelCellStyles(workbook);
headerCellD1.setCellStyle(multipleCellStyles.get("header_cell_style"));
cellB.setCellStyle(multipleCellStyles.get("normal_cell_style"));
cellC.setCellStyle(multipleCellStyles.get("date_cell_style"));
来源:https://stackoverflow.com/questions/32759871/multiple-styles-to-excel-cell-poi