单元格可以设置居左、居中、居右、上下居中、设置边框、设置边框颜色、设置单元格背景颜色等, excel设置单元格有一个HSSFCellStyle类可以设置样式,单元格颜色比较麻烦,excel颜色对应一个下标值,我们可以使用自定义颜色,但下标值从11开始,前1-10被poi已经使用,通过palette.setColorAtIndex方法将颜色与下标值对应,下面cellStyle.setFillForegroundColor(bgIndex)设置背景颜色时set
下标值并不是颜色Color,一个下标值如11不能被重复设置颜色,否则excel单元格显示的都是黑色,如下
背景颜色使用下标值bgIndex=11,边框颜色使用下标值bdIndex=12
short colorIndex = 10;
HSSFPalette palette = wb.getCustomPalette();//自定义颜色
Color rgb = Color.GREEN;
short bgIndex = colorIndex ++; //背景颜色下标值
palette.setColorAtIndex(bgIndex, (byte) rgb.getRed(), (byte) rgb.getGreen(), (byte) rgb.getBlue());
short bdIndex = colorIndex ++; //边框颜色下标值
rgb = Color.BLACK;
palette.setColorAtIndex(bdIndex, (byte) rgb.getRed(), (byte) rgb.getGreen(), (byte) rgb.getBlue());
for(int i = 0; i
|