java Excel 单元格 POI 的多种样式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32759871/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Multiple Styles to Excel Cell POI
提问by rupesh_padhye
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);
回答by rgettman
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.
多个单元格样式不能应用于单个Cell
. 应用的最后一个单元格样式将覆盖Cell
. 设置多个CellStyle
s 不会组合每个样式的设置属性。
The solution is to create another CellStyle
that has the desired attributes of both of the other CellStyle
s. You can use the cloneStyleFrom
methodto start with the attributes of one CellStyle
.
解决方案是创建另一个CellStyle
具有其他两个CellStyle
s所需属性的另一个。您可以使用该cloneStyleFrom
方法从 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
.
该技术可以推广到克隆任何现有单元格样式并从第二个现有单元格样式复制单个属性。与往常一样,重用任何现有的CellStyle
s,但如果需要不同的属性组合,则必须创建并使用新的CellStyle
.
回答by New Bee
You can create a map of styles and then you can use different styles throughout the java program.
您可以创建样式映射,然后您可以在整个 Java 程序中使用不同的样式。
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"));