網站首頁 編程語言 正文
想要 通過 自定義RGB 來設置 導出 excel 的表頭顏色
pom 依賴
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.2.7</version>
</dependency>
相關示例代碼
ExcelUtils 工具類中可以本地測試
/**
* 表頭 顏色 定義
*/
public class ComplexHeadStyles {
/**
* 表頭橫坐標 - 行
*/
private Integer x;
/**
* 表頭縱坐標 - 列
*/
private Integer y;
/**
* 內置顏色
*/
private Short indexColor;
private Integer red;
private Integer green;
private Integer blue;
public ComplexHeadStyles(Integer x, Integer y, Short indexColor) {
this.x = x;
this.y = y;
this.indexColor = indexColor;
}
public ComplexHeadStyles(Integer x, Integer y, Integer red, Integer green, Integer blue) {
this.x = x;
this.y = y;
this.red = red;
this.green = green;
this.blue = blue;
}
public Integer getX() {
return x;
}
public Integer getY() {
return y;
}
public Short getIndexColor() {
return indexColor;
}
public Integer getRed() {
return red;
}
public Integer getGreen() {
return green;
}
public Integer getBlue() {
return blue;
}
}
/**
* 自定義表頭樣式攔截器
*
* @since 2022-07-21 14:26
*/
public class CustomHeadWriteHandler extends AbstractCellStyleStrategy {
/**
* 復雜表頭自定義樣式隊列,先進先出,方便存儲
*/
private ArrayBlockingQueue<ComplexHeadStyles> headStylesQueue;
public CustomHeadWriteHandler(ArrayBlockingQueue<ComplexHeadStyles> headStylesQueue) {
this.headStylesQueue = headStylesQueue;
}
@Override
public void afterCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
if (isHead) {
if (headStylesQueue != null && !headStylesQueue.isEmpty()) {
ComplexHeadStyles complexHeadStyle = headStylesQueue.peek();
Sheet sheet = writeSheetHolder.getSheet();
Workbook workbook = sheet.getWorkbook();
CellStyle cellStyle = initCellStyleCustom(workbook);
// 取出隊列中的自定義表頭信息,與當前坐標比較,判斷是否相符
if (cell.getColumnIndex() == complexHeadStyle.getY() && relativeRowIndex.equals(complexHeadStyle.getX())) {
if (ObjectUtil.isNotEmpty(complexHeadStyle.getIndexColor())) {
//有指定顏色顏色 索引
cellStyle.setFillForegroundColor(complexHeadStyle.getIndexColor());
cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
cell.setCellStyle(cellStyle);
} else {
//有指定顏色 RGB
if (ObjectUtil.isNotEmpty(complexHeadStyle.getRed()) && ObjectUtil.isNotEmpty(complexHeadStyle.getGreen()) && ObjectUtil.isNotEmpty(complexHeadStyle.getBlue())) {
XSSFCellStyle xssfCellStyle = (XSSFCellStyle)workbook.createCellStyle();
xssfCellStyle.cloneStyleFrom(cellStyle);
xssfCellStyle.setFillForegroundColor(new XSSFColor(new java.awt.Color(complexHeadStyle.getRed(), complexHeadStyle.getGreen(), complexHeadStyle.getBlue())));
xssfCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
cell.setCellStyle(xssfCellStyle);
}
}
// 樣式出隊
headStylesQueue.poll();
}
}
}
}
@Override
protected void initCellStyle(Workbook workbook) {
}
@Override
protected void setHeadCellStyle(Cell cell, Head head, Integer relativeRowIndex) {
}
@Override
protected void setContentCellStyle(Cell cell, Head head, Integer relativeRowIndex) {
}
private CellStyle initCellStyleCustom(Workbook workbook) {
CellStyle style = workbook.createCellStyle();
style.setBorderBottom(BorderStyle.THIN); // 下邊框
style.setBorderLeft(BorderStyle.THIN); // 左邊框
style.setBorderTop(BorderStyle.THIN); // 上邊框
style.setBorderRight(BorderStyle.THIN); // 右邊框
Font font = workbook.createFont();
font.setFontName("微軟雅黑"); // 字體樣式
font.setBold(false); // 是否加粗
font.setFontHeightInPoints((short)13); // 字體大小
style.setFont(font);
return style;
}
}
/**
* excel 工具類
*/
public class ExcelUtils {
/**
* excel 導出樣式 默認
*
* @return
*/
public static WriteHandler setExcelStyle() {
// 頭的策略
WriteCellStyle headWriteCellStyle = new WriteCellStyle();
// 背景設置為橙色
headWriteCellStyle.setFillForegroundColor(IndexedColors.ORANGE.getIndex());
WriteFont headWriteFont = new WriteFont();
headWriteFont.setColor(IndexedColors.WHITE.getIndex());
headWriteFont.setFontName("微軟雅黑");
headWriteFont.setFontHeightInPoints((short)13);
headWriteCellStyle.setWriteFont(headWriteFont);
// 內容的策略
WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
WriteFont contentWriteFont = new WriteFont();
contentWriteFont.setFontName("微軟雅黑");
contentWriteFont.setFontHeightInPoints((short)11);
contentWriteFont.setColor(IndexedColors.BLACK.getIndex());
// 字體大小
contentWriteCellStyle.setWriteFont(contentWriteFont);
// 這個策略是 頭是頭的樣式 內容是內容的樣式 其他的策略可以自己實現
return new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
}
/**
* 導出文件時為Writer生成OutputStream
*
* @param fileName 文件名
* @param response response
* @return 流
*/
public static OutputStream getOutputStream(String fileName, HttpServletResponse response) throws Exception {
try {
fileName = URLEncoder.encode(fileName, "utf-8");
response.setCharacterEncoding("utf8");
response.setContentType("multipart/form-data");
response.setHeader("Content-Disposition", "attachment;filename*=utf-8'zh_cn'" + fileName + ".xlsx");
return response.getOutputStream();
} catch (IOException e) {
throw new Exception("導出excel表格失敗!", e);
}
}
/**
* excel 導出樣式 測試
*
* @return
*/
public static WriteHandler setTestTemplateSheet0() {
// 設置表頭樣式隊列【先進先出】
ArrayBlockingQueue<ComplexHeadStyles> complexHeadStylesArrayBlockingQueue = new ArrayBlockingQueue<>(3);
/**
* 顏色
*/
complexHeadStylesArrayBlockingQueue.add(new ComplexHeadStyles(0, 0, 102, 255, 255));
complexHeadStylesArrayBlockingQueue.add(new ComplexHeadStyles(0, 1, IndexedColors.ORANGE.getIndex()));
complexHeadStylesArrayBlockingQueue.add(new ComplexHeadStyles(0, 2, IndexedColors.GREY_25_PERCENT.getIndex()));
CustomHeadWriteHandler headStyleWriteHandler = new CustomHeadWriteHandler(complexHeadStylesArrayBlockingQueue);
return headStyleWriteHandler;
}
public static List<List<String>> getTestHeader0(List<List<Object>> list0) {
List<List<String>> list = Lists.newArrayList();
List<String> head0 = Lists.newArrayList("第一列");
List<String> head1 = Lists.newArrayList("第二列");
List<String> head2 = Lists.newArrayList("第三列");
list.add(head0);
list.add(head1);
list.add(head2);
List<Object> row1 = Lists.newArrayList("111", "222", "333");
list0.add(row1);
return list;
}
public static ByteArrayOutputStream getExcelTestOut(HttpServletResponse response) {
ExcelWriter excelWriter = null;
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
if (ObjectUtil.isNotEmpty(response)) {
excelWriter = EasyExcel.write(getOutputStream("test", response)).registerWriteHandler(setExcelStyle()).build();
} else {
excelWriter = EasyExcel.write(out).registerWriteHandler(setExcelStyle()).build();
}
List<List<Object>> list0 = Lists.newArrayList();
List<List<String>> header0 = getTestHeader0(list0);
//獲取sheet對象
WriteSheet sheet0 = EasyExcel.writerSheet(0, "test").head(header0).registerWriteHandler(ExcelUtils.setTestTemplateSheet0()).build();
excelWriter.write(list0, sheet0);
} catch (Exception e) {
e.printStackTrace();
} finally {
excelWriter.finish();
}
return out;
}
public static void main(String[] args) throws IOException {
ByteArrayOutputStream excelTestOut = getExcelTestOut(null);
//建立文件
File file = new File("D:\\test.xlsx");
if (file.exists()) {
System.out.println("文件已存在");
return;
}
file.createNewFile();
FileOutputStream fileOutputStream = new FileOutputStream(file.getPath());
fileOutputStream.write(excelTestOut.toByteArray());
fileOutputStream.close();
}
}
原文鏈接:https://blog.csdn.net/xydxiong/article/details/125918485
相關推薦
- 2022-12-13 Redis?Hash序列化存儲的問題及解決方案_Redis
- 2022-09-17 使用cache加快編譯速度的命令詳解_相關技巧
- 2022-12-03 C++通信新特性協程詳細介紹_C 語言
- 2022-06-13 matplotlib繪制餅圖的基本配置(萬能模板案例)_python
- 2022-07-10 Linux安裝及管理程序
- 2022-05-18 C++?qt實現打開關閉狀態按鈕的代碼_C 語言
- 2022-11-26 利用Python讀取Excel表內容的詳細過程_python
- 2021-12-02 Spring?Boot?分層打包?Docker?鏡像實踐及分析(推薦)_docker
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支