alibaba / easyexcel

快速、简洁、解决大文件内存溢出的java处理Excel工具

Home Page:https://easyexcel.opensource.alibaba.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

生成的自定义颜色与设置的不一致

linqunjing opened this issue · comments

建议先去看文档

快速开始常见问题

触发场景描述

我设置的RGB颜色为252, 228, 214,生成后的RGB颜色为255,255,204

触发Bug的代码

@test
public void test13() {
String fileName = getPath() + "handlerStyleWrite" + System.currentTimeMillis() + ".xlsx";
HorizontalCellStyleStrategy horizontalCellStyleStrategy = ExcelUtil.getStyle();
ExcelWriter excelWriter = EasyExcel.write(fileName).registerWriteHandler(horizontalCellStyleStrategy).build();

	WriteSheet writeSheet = EasyExcel.writerSheet("sheet").head(Data.class).build();

	//填充数据
	List<Data> exVos1 = exportGauge();
	excelWriter.write(exVos1, writeSheet);

	excelWriter.finish();
}

public static HorizontalCellStyleStrategy getStyle(){
	// 头的策略
	WriteCellStyle headWriteCellStyle = new WriteCellStyle();
	// 自定义背景色
	HSSFWorkbook hwb = new HSSFWorkbook();
	HSSFPalette hpalette = hwb.getCustomPalette();
	HSSFColor hhssfColor = hpalette.findSimilarColor(252, 228, 214);
	headWriteCellStyle.setFillForegroundColor(hhssfColor.getIndex());
	//设置字体
	WriteFont headWriteFont = new WriteFont();
	// 字体大小
	headWriteFont.setFontHeightInPoints((short) 11);
	// 字体
	headWriteFont.setFontName("宋体");
	// 字体加粗
	headWriteFont.setBold(true);
	headWriteCellStyle.setWriteFont(headWriteFont);

	//设置边框
	headWriteCellStyle.setBorderBottom(BorderStyle.THIN);
	headWriteCellStyle.setBorderLeft(BorderStyle.THIN);
	headWriteCellStyle.setBorderRight(BorderStyle.THIN);
	headWriteCellStyle.setBorderTop(BorderStyle.THIN);

	// 内容的策略
	WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
	// 这里需要指定 FillPatternType 为FillPatternType.SOLID_FOREGROUND 不然无法显示背景颜色.头默认了 FillPatternType所以可以不指定
	contentWriteCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);
	// 自定义背景色
	HSSFWorkbook wb = new HSSFWorkbook();
	HSSFPalette palette = wb.getCustomPalette();
	HSSFColor hssfColor = palette.findSimilarColor(255, 255, 255);
	contentWriteCellStyle.setFillForegroundColor(hssfColor.getIndex());
	//设置边框
	contentWriteCellStyle.setBorderBottom(BorderStyle.THIN);
	contentWriteCellStyle.setBorderLeft(BorderStyle.THIN);
	contentWriteCellStyle.setBorderRight(BorderStyle.THIN);
	contentWriteCellStyle.setBorderTop(BorderStyle.THIN);

	WriteFont contentWriteFont = new WriteFont();
	// 字体大小
	contentWriteFont.setFontHeightInPoints((short) 11);
	// 字体
	contentWriteFont.setFontName("宋体");
	contentWriteCellStyle.setWriteFont(contentWriteFont);
	//居中
	contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
	contentWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
	// 这个策略是 头是头的样式 内容是内容的样式 其他的策略可以自己实现
	return new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
}

提示的异常或者没有达到的效果

大家尽量把问题一次性描述清楚,然后贴上全部异常,这样方便把问题一次性解决掉。
至少大家要符合一个原则就是,能让其他人复现出这个问题,如果无法复现,肯定无法解决。

使用的版本是2.2.5,升级到3.1.3还是有相同的问题

已经参照网上资料,扩展了一下,实现了自定义颜色
https://www.cnblogs.com/Tomlin/articles/18132393