使用Apache POI着色单元格

时间:2012-03-28 12:00:10

标签: java apache-poi

我想使用POI为特定单元格着色Shade it 通常要对单元格进行着色,可以选择单元格>右键单击>格式单元格 - >填充(制表符) - >填充效果,选择颜色并选择着色样式(对我来说,它将是黄金和阴影样式为HORIZONTAL 2nd Option) 我面临的问题是我无法找到一个有助于着色Cell的功能。这甚至可能使用POI甚至jxl?这是我的代码片段:

FileOutputStream fileOut = new FileOutputStream("C:/poi-test.xls");
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet worksheet = workbook.createSheet("POI Worksheet");
HSSFRow row1 = worksheet.createRow((short) 0);
HSSFCell cellA1 = row1.createCell((short) 0);
cellA1.setCellValue("Hello");
HSSFCellStyle cellStyle = workbook.createCellStyle();
cellStyle.setFillForegroundColor(HSSFColor.GOLD.index); 
cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);\\ this part is probably redundant
cellA1.setCellStyle(cellStyle);
workbook.write(fileOut);
fileOut.close();  

1 个答案:

答案 0 :(得分:2)

您可以尝试以下填充图案进行着色。

CellStyle.THIN_HORZ_BANDS,CellStyle.THICK_HORZ_BANDS,

其他可能的填充模式可以从以下链接中找到:

http://poi.apache.org/apidocs/org/apache/poi/hssf/usermodel/HSSFCellStyle.html#setFillPattern(short)

http://www.roseindia.net/jsp/poi/filelColorInExcel.shtml

相关问题