导航

 

写个工具将所有颜色代码的实际效果生成出来

 1 static void colorTest() throws IOException {
 2         Workbook wb = new HSSFWorkbook();
 3         Sheet sheet = wb.createSheet("colorful");
 4         int rowNum = 0;
 5         for (IndexedColors color : IndexedColors.values()) {
 6             Row row = sheet.createRow(rowNum++);
 7             Cell cell = row.createCell(0);
 8             cell.setCellValue(color.toString());
 9             Cell cellColor = row.createCell(1);
10             CellStyle style = wb.createCellStyle();
11             style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
12             style.setFillForegroundColor(color.getIndex());
13             cellColor.setCellStyle(style);
14         }
15         sheet.setColumnWidth(0,25*256);
16         sheet.setColumnWidth(1,50*256);
17         FileOutputStream fout = new FileOutputStream(new File("d:\\colors.xls"));
18         wb.write(fout);
19         wb.close();
20         fout.close();
21     }

生成结果:

 

 

实际使用:

cellStyle.setFillForegroundColor(IndexedColors.LIGHT_CORNFLOWER_BLUE.getIndex()); //单元格背景色
cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);//前景填充模式

 

其他常用Excel样式设置:

cellStyle.setAlignment(HorizontalAlignment.CENTER);//水平居中
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中

cellStyle.setBorderBottom(BorderStyle.THIN); //下边框
cellStyle.setBorderLeft(BorderStyle.THIN);//左边框
cellStyle.setBorderTop(BorderStyle.THIN);//上边框
cellStyle.setBorderRight(BorderStyle.THIN);//右边框

HSSFFont font = workbook.createFont();
font.setFontHeightInPoints((short)9);//字体大小
font.setFontName("微软雅黑");//字体
font.setBold(true); //加粗
font.setColor(Font.COLOR_NORMAL); //字体颜色
cellStyle.setFont(font);

cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("0.00%")); //设置数字以百分比显示

 附easypoi学习文档:
http://doc.wupaas.com/docs/easypoi/easypoi-1c10ldlb9epsk

posted on 2022-04-26 18:21  一棵二叉树  阅读(746)  评论(0编辑  收藏  举报