写个工具将所有颜色代码的实际效果生成出来
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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述