1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;
import java.io.FileOutputStream;
public class PoiBackgroundColorDemo {
public static void main(String[] args) throws Exception {
//创建Excel工作薄
HSSFWorkbook excel = new HSSFWorkbook();
//创建第一个sheet
HSSFSheet sheet = excel.createSheet("POI Demo");
//创建第一行
HSSFRow row = sheet.createRow((short) 0);
//创建第一个单元格
HSSFCell cell = row.createCell((short) 0);
//设置单元格的值
cell.setCellValue("Ay");
//生成单元格样式
HSSFCellStyle style = excel.createCellStyle();
//设置背景颜色
style.setFillForegroundColor(HSSFColor.LIME.index);
//solid填充foreground前景色
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
cell.setCellStyle(style);
//通过流写到硬盘
FileOutputStream out = new FileOutputStream("D:/old_color.xls");
excel.write(out);
out.close();
//自定义单元格的样式
cell.setCellValue("Al");
//拿到palette颜色板
HSSFPalette palette = excel.getCustomPalette();
//把之前的颜色HSSFColor.LIME.index替换为RGB(51, 204, 204)宝石蓝这种颜色
palette.setColorAtIndex(HSSFColor.LIME.index, (byte) 0, (byte) 255, (byte) 127);
out = new FileOutputStream("D:/new_color.xls");
excel.write(out);
out.close();
}
}
Post
Cancel
POI自定义单元格背景颜色
This post is licensed under
CC BY 4.0
by the author.