我正在查看一个我想要复制的 Excel 工作表,我遇到的唯一问题是颜色。我想要复制的颜色是Blue, Accent 5, Lighter 40%
and Light Green
来自Standard Colors
部分。我正在看docs在 XSSF Workbook 中使用自定义颜色,它指出执行方法如下:
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sheet = wb.createSheet();
XSSFRow row = sheet.createRow(0);
XSSFCell cell = row.createCell(0);
cell.setCellValue("custom XSSF colors");
XSSFCellStyle style1 = wb.createCellStyle();
style1.setFillForegroundColor(new XSSFColor(new java.awt.Color(128, 0, 128), new DefaultIndexedColorMap()));
style1.setFillPattern(FillPatternType.SOLID_FOREGROUND);
当我尝试使用时style1.setFillForegroundColor(new XSSFColor(new java.awt.Color(128, 0, 128), new DefaultIndexedColorMap()));
我收到一个错误,因为唯一的参数.setFillForegroundColor()
只接受一个参数,那就是short
而不是一个XSSFColor
.
有人有这样的运气吗?我已经找了几个小时了,找不到任何 8 年前或不起作用的东西。
使用电流apache poi 4.1.1
有公共无效setFillForegroundColor(XSSFColor颜色) in XSSFCellStyle
.
The XSSFColor
应该使用构造函数创建公共 XSSFColor(byte[] rgb, IndexedColorMap colorMap)因为所有其他构造函数都已被弃用或标记TEST ONLY
或不可用于创建自定义颜色。
The RGB
所需颜色的值可以从Excel
通过从调色板设置颜色,然后选择Fill Color
- More Colors
- Custom
。很遗憾apache poi
's 索引颜色与当前的颜色不再精确Excel
版本。他们是版本2007
。所以它们也可以使用,但稍后Excel
版本可能会显示不同的颜色。
使用电流的完整示例apache poi 4.1.1
:
import java.io.FileOutputStream;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFColor;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.DefaultIndexedColorMap;
public class CreateExcelXSSFCellFillColor {
public static void main(String[] args) throws Exception {
XSSFWorkbook workbook = new XSSFWorkbook();
java.util.List<XSSFCellStyle> cellStyles = new java.util.ArrayList<XSSFCellStyle>();
XSSFCellStyle cellStyle; byte[] rgb; XSSFColor color;
//Your custom color #800080
//create cell style on workbook level
cellStyle = workbook.createCellStyle();
//set pattern fill settings
cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
//create the RGB byte array
rgb = new byte[3];
rgb[0] = (byte) 128; // red
rgb[1] = (byte) 0; // green
rgb[2] = (byte) 128; // blue
//create XSSFColor
color = new XSSFColor(rgb, new DefaultIndexedColorMap());
//set fill color to cell style
cellStyle.setFillForegroundColor(color);
cellStyles.add(cellStyle);
//Light Green
cellStyle = workbook.createCellStyle();
cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
rgb = new byte[3];
rgb[0] = (byte) 146; // red
rgb[1] = (byte) 208; // green
rgb[2] = (byte) 80; // blue
color = new XSSFColor(rgb, new DefaultIndexedColorMap());
cellStyle.setFillForegroundColor(color);
cellStyles.add(cellStyle);
//Blue, Accent 5, Lighter 40%
cellStyle = workbook.createCellStyle();
cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
rgb = new byte[3];
rgb[0] = (byte) 155; // red
rgb[1] = (byte) 194; // green
rgb[2] = (byte) 230; // blue
color = new XSSFColor(rgb, new DefaultIndexedColorMap());
cellStyle.setFillForegroundColor(color);
cellStyles.add(cellStyle);
Sheet sheet = workbook.createSheet();
for (int r = 0; r < cellStyles.size(); r++) {
Row row = sheet.createRow(r);
row.setHeight((short)(20*20));
Cell cell = row.createCell(0);
cell.setCellValue("cell style " + (r+1));
cell.setCellStyle(cellStyles.get(r));
}
sheet.setColumnWidth(0, 20*256);
FileOutputStream out = new FileOutputStream("CreateExcelXSSFCellFillColor.xlsx");
workbook.write(out);
out.close();
workbook.close();
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)