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);
    }
}
相关推荐
云飞云共享云桌面几秒前
SolidWorks三维设计不用单独买电脑,1台服务器10个设计用
运维·服务器·数据库·3d·电脑
Rabbit_QL几秒前
从服务器拷文件到本地:scp 与 rsync 实战
服务器
acaad4 分钟前
访问信创系统的服务器报错Received fatal alert: handshake_failure
运维·服务器
大树885 分钟前
【无标题】
大数据·运维·服务器·人工智能
南境十里·墨染春水6 分钟前
linux学习进展 基础命令 vi基础命令
linux·运维·服务器·笔记·学习
chools17 分钟前
Java后端拥抱AI开发之个人学习路线 - - Spring AI【第四期】(Tool + MCP)
java·人工智能·学习·spring
亦暖筑序19 分钟前
多轮对话的记忆心脏:ChatMemory 滑动窗口原理
java·人工智能
AAAAA924020 分钟前
物联网BOM成本管理:精准化、智能化与可持续化
java·物联网·struts
江畔何人初20 分钟前
GTID的作用
linux·运维·服务器·mysql·云原生·kubernetes
今天又在写代码20 分钟前
数据智能分析平台部署服务器
android·服务器·adb