mitao
2 天以前 e8e30e5474c1fd0c14390710066e40c17155ad37
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package com.ruoyi.system.utils;
 
import cn.idev.excel.FastExcel;
import cn.idev.excel.context.AnalysisContext;
import cn.idev.excel.event.AnalysisEventListener;
import com.ruoyi.common.exception.ServiceException;
import lombok.extern.slf4j.Slf4j;
 
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.List;
 
/**
 * @author mitao
 * @date 2025/10/22
 */
@Slf4j
public class FastExcelUtil {
    public static <T> void exportData(HttpServletResponse response, String fileName, String sheetName, Class<T> clazz, List<T> data) {
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        try {
            fileName = URLEncoder.encode(fileName, "UTF-8").replaceAll("\\+", "%20");
            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
            FastExcel.write(response.getOutputStream(), clazz)
                    .sheet(sheetName)
                    .doWrite(data);
        } catch (IOException e) {
            log.error("导出数据异常", e);
            throw new ServiceException("导出数据异常");
        }
    }
    /**
     * 读取文件数据,并返回一个包含指定类型数据的列表。
     *
     * @param inputStream 文件对象输入流
     * @param clazz       数据对象的类型
     * @param <T>         泛型类型,表示数据对象的类型
     * @return 包含解析后数据对象的列表
     */
    public static <T> List<T> readMultipartFile(InputStream inputStream, Class<T> clazz) {
        return FastExcel.read(inputStream, clazz, new AnalysisEventListener<T>() {
            @Override
            public void invoke(Object o, AnalysisContext analysisContext) {
                Integer rowIndex = analysisContext.readRowHolder().getRowIndex();
                rowIndex += 1; // 行号从0开始,需要加1
                try {
                    // 调用字段校验方法
                    ExcelImportValid.valid(o);
                } catch (Exception e) {
                    throw new ServiceException(String.format("第%d行:%s", rowIndex, e.getMessage()));
                }
            }
 
            @Override
            public void doAfterAllAnalysed(AnalysisContext analysisContext) {
 
            }
        }).sheet().doReadSync(); // 读取 Excel 文件中的第一个工作表
    }
}