easyExcel:https://github.com/alibaba/easyexcel
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>4.0.3</version>
</dependency>
基本用法
List<List<String>> excelHeaders = buildExcelHeaders(headers);
// 3. 创建ExcelWriter
try (ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream())
.registerWriteHandler(ExcelUtil.createStyleStrategy())
.build()) {
// 4. 写入数据
List<List<Object>> sheetDataList = buildSheetData(sheetData, headers);
// 创建Sheet ,多个sheet 这两句放for循环(创建Sheet+写入sheet数据)
WriteSheet writeSheet = EasyExcel.writerSheet()
.head(excelHeaders)
.build();
// 写入数据,多个sheet 这句放for循环
excelWriter.write(sheetDataList, writeSheet);
// 5. 关闭并返回字节数组
excelWriter.finish();
}
常用工具类
package com.ceair.muframe.common.utils;
import com.alibaba.excel.write.handler.WriteHandler;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.*;
import javax.servlet.http.HttpServletResponse;
@Slf4j
public class ExcelUtil {
/**
* 设置响应头
*/
public static void setExcelResponse(String fileName, HttpServletResponse response) {
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
}
/**
* 创建表头样式(第一行、第二行、第一列):背景,字体
*/
public static CellStyle createHeaderStyle(Workbook workbook) {
CellStyle style = workbook.createCellStyle();
// 设置白色字体
Font font = createBaseFont(workbook, null, true);
font.setColor(IndexedColors.BLACK.getIndex());
style.setFont(font);
// 设置边框
setAllBorders(style, IndexedColors.BLACK.getIndex());
// 居中对齐
style.setAlignment(HorizontalAlignment.CENTER);
style.setVerticalAlignment(VerticalAlignment.CENTER);
return style;
}
/**
* 设置所有边框
*/
public static void setAllBorders(CellStyle style, short colorIndex) {
BorderStyle dottedStyle = BorderStyle.THIN;
style.setBorderTop(dottedStyle);
style.setBorderBottom(dottedStyle);
style.setBorderLeft(dottedStyle);
style.setBorderRight(dottedStyle);
// 设置边框颜色(可选,默认为黑色)
style.setTopBorderColor(colorIndex);
style.setBottomBorderColor(colorIndex);
style.setLeftBorderColor(colorIndex);
style.setRightBorderColor(colorIndex);
}
/**
* 创建数据格样式
*/
public static CellStyle createValueStyle(Workbook workbook, String fontName) {
CellStyle style = workbook.createCellStyle();
// 设置字体
Font font = createBaseFont(workbook, fontName, false);
style.setFont(font);
// 设置边框
setAllBorders(style, IndexedColors.BLACK.getIndex());
// 居中对齐
style.setAlignment(HorizontalAlignment.CENTER);
style.setVerticalAlignment(VerticalAlignment.CENTER);
return style;
}
/**
* 创建基础字体:微软雅黑,10号
*/
public static Font createBaseFont(Workbook workbook, String fontName, boolean isBold) {
if (fontName == null) fontName = "微软雅黑";
Font font = workbook.createFont();
font.setFontName(fontName);
font.setFontHeightInPoints((short) 10);
font.setBold(isBold);
return font;
}
/**
* 创建批注
*/
public static void buildComment(Workbook workbook, Sheet sheet, Row row, Cell cell, String text) {
// ===== 创建批注 =====
// 1. 获取绘图对象
Drawing<?> drawing = sheet.createDrawingPatriarch();
// 2. 创建锚点(确定批注位置和大小)
ClientAnchor anchor = workbook.getCreationHelper().createClientAnchor();
anchor.setCol1(cell.getColumnIndex()); // 起始列
anchor.setCol2(cell.getColumnIndex() + 2); // 结束列(批注宽度)
anchor.setRow1(row.getRowNum()); // 起始行
anchor.setRow2(row.getRowNum() + 4); // 结束行(批注高度)
// 3. 创建批注
Comment comment = drawing.createCellComment(anchor);
// 4. 设置批注文本
RichTextString commentText = workbook.getCreationHelper()
.createRichTextString(text);
comment.setString(commentText);
// 6. 将批注关联到单元格
cell.setCellComment(comment);
}
/**
* 创建单元格样式策略
*/
public static WriteHandler createStyleStrategy() {
// 表头样式
WriteCellStyle headStyle = new WriteCellStyle();
headStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
headStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);
headStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
headStyle.setVerticalAlignment(VerticalAlignment.CENTER);
WriteFont headFont = new WriteFont();
headFont.setBold(true);
headFont.setFontHeightInPoints((short) 11);
headStyle.setWriteFont(headFont);
// 内容样式
WriteCellStyle contentStyle = new WriteCellStyle();
contentStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
contentStyle.setVerticalAlignment(VerticalAlignment.CENTER);
// 设置边框
headStyle.setBorderLeft(BorderStyle.THIN);
headStyle.setBorderRight(BorderStyle.THIN);
headStyle.setBorderTop(BorderStyle.THIN);
headStyle.setBorderBottom(BorderStyle.THIN);
contentStyle.setBorderLeft(BorderStyle.THIN);
contentStyle.setBorderRight(BorderStyle.THIN);
contentStyle.setBorderTop(BorderStyle.THIN);
contentStyle.setBorderBottom(BorderStyle.THIN);
return new HorizontalCellStyleStrategy(headStyle, contentStyle);
}
}