Easyexcel导出自定义单元格背景色问题记录

文章讲述了在使用Java的XSSF库处理Excel时,如何在监听器中正确设置单元格样式,特别是自定义背景色的问题。原始代码中使用HSSFWorkbook和HSSFColor遇到了问题,解决方案是改用XSSFCellStyle并直接设置RGB值。
摘要由CSDN通过智能技术生成

版本为2.2.6,jdk1.8;

写一个监听器CellStyleStrategy 实现 CellWriteHandler 重写afterCellDispose方法

问题:使用REG颜色设置不成功

private static final String DEFAULT_BACKGROUND_COLOR = "#ddebf7";
	
	 
	@Override
	public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder,
			List<CellData> cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
		    
			CellStyle toStyle = writeSheetHolder.getSheet().getWorkbook().createCellStyle();
			toStyle.setBorderLeft(BorderStyle.THIN);//设置边框
			if (rowIndex % 2 == 0 ) {
				// 自定义背景色
				int r = Integer.parseInt((DEFAULT_BACKGROUND_COLOR.substring(1,3)),16);
				int g = Integer.parseInt((DEFAULT_BACKGROUND_COLOR.substring(3,5)),16);
				int b = Integer.parseInt((DEFAULT_BACKGROUND_COLOR.substring(5,7)),16);
				HSSFWorkbook wb = new HSSFWorkbook();
				HSSFPalette palette = wb.getCustomPalette();
				HSSFColor hssfColor = palette.findSimilarColor(r,g,b);
				toStyle.setFillForegroundColor(hssfColor.getIndex());	
                //byte[] rgb = new byte[]{(byte) 221, (byte) 235, (byte) 247};
                //XSSFColor color = new XSSFColor(rgb, null);
                //toStyle.setFillForegroundColor(color.getIndex());			
			} else {
				toStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());
			}
            toStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
			toStyle.setFillBackgroundColor(IndexedColors.AUTOMATIC.index);
			cell.setCellStyle(toStyle);
		}
	}

以上代码会导致颜色设置不正常

解决方法:修改创建cellStyle的方式

以下代码

@Override
	public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder,
			List<CellData> cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
		int rowIndex = cell.getRowIndex();
	 
//			CellStyle toStyle = writeSheetHolder.getSheet().getWorkbook().createCellStyle();//这里修改创建方式
			XSSFCellStyle toStyle = (XSSFCellStyle) cell.getRow().getSheet().getWorkbook().createCellStyle();
			toStyle.setBorderLeft(BorderStyle.THIN);
			if (rowIndex % 2 == 0 ) {
				// 自定义背景色
				XSSFColor color = new XSSFColor(new byte[]{(byte) 221, (byte) 235, (byte) 247}, new DefaultIndexedColorMap());
				toStyle.setFillForegroundColor(color);
			} else {
				toStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());
			}
			toStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
			toStyle.setFillBackgroundColor(IndexedColors.AUTOMATIC.index);
			cell.setCellStyle(toStyle);
		 
	}

EasyExcel是一个基于Java的Excel读写库,可以简化Java开发者对Excel文件的读写操作。EasyExcel支持大数据量的读写,同时还支持Excel文件的合并单元格等操作。 下面是EasyExcel导出合并单元格的步骤: 1. 定义需要导出的数据实体类,使用@ExcelProperty注解标记需要导出的属性。 2. 使用EasyExcel的WriteSheet类创建需要导出的Sheet。 3. 使用EasyExcel的WriteTable类创建需要导出的Table,设置Table的列属性和合并单元格的规则。 4. 使用EasyExcel的ExcelWriter类创建导出Excel文件的实例。 5. 调用ExcelWriter的write方法将数据写入Excel文件中。 下面是一段示例代码: ``` // 定义需要导出的数据实体类 public class DemoData { @ExcelProperty(value = "姓名", index = 0) private String name; @ExcelProperty(value = "年龄", index = 1) private Integer age; // ... 省略getter和setter方法 } // 创建需要导出的Sheet WriteSheet sheet = EasyExcel.writerSheet("Sheet1").build(); // 创建需要导出的Table WriteTable table = EasyExcel.writerTable(0).needHead(true) .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()) .head(DemoData.class).build(); // 设置合并单元格规则 table.columnWidthMap().put(0, 20); table.columnWidthMap().put(1, 20); table.merge(0, 0, 1, 0); // 创建导出Excel文件的实例 String fileName = "demo.xlsx"; ExcelWriter excelWriter = EasyExcel.write(fileName, DemoData.class).build(); // 将数据写入Excel文件中 List<DemoData> data = getData(); excelWriter.write(data, sheet, table); // 关闭ExcelWriter excelWriter.finish(); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值