使用jxl将自定义颜色作为背景应用于excel表行

时间:2014-10-15 09:31:32

标签: excel colors background-color jxl

我已经学会了如何使用以下代码使用jxl库将颜色应用于excel表行的背景

WritableFont cellFonts = new WritableFont(WritableFont.ARIAL, 11, WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLACK);
WritableCellFormat cellFormats = new WritableCellFormat(cellFonts);
cellFormats.setBackground(Colour.AQUA);

但我不知道如何将自定义颜色应用为背景

当我尝试这个Colour myColour = Colour(221,221,221);时,我得到了异常

WritableFont cellFonts = new WritableFont(WritableFont.ARIAL, 11, WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLACK);
WritableCellFormat cellFormats = new WritableCellFormat(cellFonts);
Colour myColour = Colour(221,221,221);
cellFormats.setBackground(myColour);

任何人都可以告诉我一些解决方案吗

1 个答案:

答案 0 :(得分:0)

首先你需要创建一个方法:

private final WritableCellFormat getCellFormat(Colour colour) throws WriteException {

WritableFont cellFonts = new WritableFont(WritableFont.ARIAL, 11, WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLACK);
WritableCellFormat cellFormats = new WritableCellFormat(cellFonts);


myColour= new Colour(10000, "1", 221, 221, 221) {
            };

cellFormats.setBackground(myColour);

return cellFormats;
}

你需要: 改变这个:

Colour myColour = Colour(221,221,221);
cellFormats.setBackground(myColour);

到此:

myColour= new Colour(10000, "1", 221, 221, 221) {
            };

并在单元格中设置:

Label label1 = new Label(column, row, "text of cell or nothing", getCellFormat(myColour));

我希望有所帮助。