导航

< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5
统计
 

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

复制代码
 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   一棵二叉树  阅读(788)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述
 
点击右上角即可分享
微信分享提示