easyExcel和poi分别处理不同标准的excel

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);
    }
}
相关推荐
JMchen1232 小时前
跨平台相机方案深度对比:CameraX vs. Flutter Camera vs. React Native
java·经验分享·数码相机·flutter·react native·kotlin·dart
阿常呓语2 小时前
ls 命令详解
linux·运维·服务器·ls
hgz07102 小时前
堆内存分区
java·开发语言·jvm
索荣荣2 小时前
SpringBoot Starter终极指南:从入门到精通
java·开发语言·springboot
独断万古他化2 小时前
【Spring 事务】事务隔离级别与事务传播机制:从理论到业务落地实操
java·后端·spring·事务隔离·事务传播
苏涵.2 小时前
三种工厂设计模式
java
倔强的石头1062 小时前
【Linux指南】基础IO系列(一)Linux 文件本质揭秘 —— 从 “磁盘文件” 到 “一切皆文件”
linux·运维·服务器
Code小翊2 小时前
re标准库模块一天学完
运维·服务器·网络
Warren982 小时前
Allure 常用装饰器:实战用法 + 最佳实践(接口自动化)
运维·服务器·git·python·单元测试·自动化·pytest