EasyExcel 单元格背景颜色、字体颜色使用2种设置颜色方法(IndexedColors中定义的颜色,自定义RGB颜色)实现 |
您所在的位置:网站首页 › java如何设置标签字体颜色 › EasyExcel 单元格背景颜色、字体颜色使用2种设置颜色方法(IndexedColors中定义的颜色,自定义RGB颜色)实现 |
1 Maven配置
cn.hutool
hutool-all
5.5.1
com.alibaba
easyexcel
2.2.8
org.projectlombok
lombok
true
2 调试代码
/**
* 导出(单元格背景颜色、字体颜色使用2种设置颜色方法(IndexedColors中定义的颜色,自定义RGB颜色)实现)
*
* @param response
*/
@GetMapping("/exportStyleColor")
public void exportStyleColor(HttpServletResponse response) {
try {
//生成表格数据
List dataList = new ArrayList();
dataList.add(new ArrayList(Arrays.asList(new Object[]{"表头11", "表头2", "表头3", "表头4"})));
dataList.add(new ArrayList(Arrays.asList(new Object[]{"表头1", "表头2", "表头3", "表头4"})));
dataList.add(new ArrayList(Arrays.asList(new Object[]{"表头31", "表头2", "表头3", "表头4"})));
//导出文件
String fileName = new String("文件名称.xlsx".getBytes(), "UTF-8");
List cellStyleList = new ArrayList();
//设置字体颜色
//使用IndexedColors中定义的颜色
cellStyleList.add(CellStyleModel.createFontColorCellStyleModel("模板", 0, 0, IndexedColors.BLUE));
//使用自定义RGB颜色
cellStyleList.add(CellStyleModel.createFontColorCellStyleModel("模板", 0, 1, 119, 119, 119));
XSSFColor fontColor = CellStyleModel.getRGBColor(225, 0, 0);
cellStyleList.add(CellStyleModel.createFontColorCellStyleModel("模板", 0, 2, fontColor));
//设置单元格背景颜色
//使用IndexedColors中定义的颜色
cellStyleList.add(CellStyleModel.createBackgroundColorCellStyleModel("模板", 1, 0, IndexedColors.PINK));
//使用自定义RGB颜色
cellStyleList.add(CellStyleModel.createBackgroundColorCellStyleModel("模板", 1, 1, 119, 119, 119));
XSSFColor backgroundColor = CellStyleModel.getRGBColor(225, 0, 0);
cellStyleList.add(CellStyleModel.createBackgroundColorCellStyleModel("模板", 1, 2, backgroundColor));
response.addHeader("Content-Disposition", "filename=" + fileName);
//设置类型,扩展名为.xls
response.setContentType("application/vnd.ms-excel");
ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).registerWriteHandler(new CustomCellStyleHandler(cellStyleList)).build();
WriteSheet writeSheet = EasyExcel.writerSheet("模板").build();
excelWriter.write(dataList, writeSheet);
//千万别忘记finish 会帮忙关闭流
excelWriter.finish();
} catch (Exception e) {
e.printStackTrace();
}
}
3 调试结果
(1)有关字体样式更详细的设置请查看以下博客。 EasyExcel 设置字体样式(字体、字体大小、字体颜色、字体加粗、字体斜体、字体下划线、字体上标下标、字体删除线) (2)有关CellStyleModel和CustomCellStyleHandler的源码请查看以下博客。 EasyExcel 批量设置单元格样式(字体样式、背景颜色) 旭东怪的个人空间_哔哩哔哩_Bilibili旭东怪,人生低谷不可怕,可怕的是坚持不到人生转折点的那一天;旭东怪的主页、动态、视频、专栏、频道、收藏、订阅等。哔哩哔哩Bilibili,你感兴趣的视频都在B站。 |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |