Poi 操作excel 定義單元格顏色
來源:程序員人生 發布時間:2016-04-12 10:45:48 閱讀次數:3762次
使用java操作excel時可以指訂單元格的色彩,有兩種方法:
1.使用提供的索引:
//設置樣式-色彩
HSSFCellStyle style = workbook.createCellStyle();
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
style.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
HSSFColor.SKY_BLUE.index 這里的色彩是java提供的
2.自定義色彩
比如這里有1個16進制的字符串用來表示色彩,通過下面的方法來自定義色彩:
String color = "cbfdee"; //此處得到的color為16進制的字符串
//轉為RGB碼
int r = Integer.parseInt((color.substring(0,2)),16); //轉為16進制
int g = Integer.parseInt((color.substring(2,4)),16);
int b = Integer.parseInt((color.substring(4,6)),16);
//自定義cell色彩
HSSFPalette palette = workbook.getCustomPalette();
//這里的9是索引
palette.setColorAtIndex((short)9, (byte) r, (byte) g, (byte) b);
HSSFCellStyle style = workbook.createCellStyle();
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
style.setFillForegroundColor((short)9);
然后cell.setCellStyle(style);便可將樣式賦給指訂單元格
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈