Excel 单元格 POI 的多种样式 [英] Multiple Styles to Excel Cell POI

查看:54
本文介绍了Excel 单元格 POI 的多种样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将颜色应用于单元格以及格式单元格值(例如日期、金额).但是当我应用两个单元格样式时,只有最后一个样式会应用于单元格.

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);

推荐答案

多个单元格样式不能应用于单个 Cell.应用的最后一个单元格样式将覆盖 Cell 上任何预先存在的单元格样式.设置多个 CellStyle 不会组合每个样式的设置属性.

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 CellStyles won't combined the set attributes of each style.

解决方案是创建另一个 CellStyle,它具有其他 CellStyle 的所需属性.您可以使用 cloneStyleFrom 方法从一个CellStyle的属性开始.

The solution is to create another CellStyle that has the desired attributes of both of the other CellStyles. 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);

此技术可以推广到克隆任何现有单元格样式并从第二个现有单元格样式复制单个属性.与往常一样,重用任何现有的 CellStyle,但如果需要不同的属性组合,则必须创建并使用新的 CellStyle.

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 CellStyles, but if a different combination of attributes is required, then you must create and use a new CellStyle.

这篇关于Excel 单元格 POI 的多种样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆