java通用Excel解析工具类

为了创建一个通用的Excel解析工具类,我们需要考虑以下几点:

  1. 泛型支持,以便能够处理不同类型的Java对象。
  2. 映射机制,以将Excel列映射到Java对象的字段。
  3. 错误处理和日志记录。

以下是一个简化的通用Excel解析工具类的示例

java 复制代码
<dependency>  
    <groupId>org.apache.poi</groupId>  
    <artifactId>poi</artifactId>  
    <version>5.2.3</version> <!-- 使用时请检查最新版本 -->  
</dependency>  
<dependency>  
    <groupId>org.apache.poi</groupId>  
    <artifactId>poi-ooxml</artifactId>  
    <version>5.2.3</version> <!-- 使用时请检查最新版本 -->  
</dependency>
java 复制代码
@Data
public class MyEntity {

    /**
     * name
     */
    @ExcelColumn(index = 0, name = "name")
    private long name;
    /**
     *age
     */
    @ExcelColumn(index = 1, name = "age")
    private long age;
}
java 复制代码
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface ExcelColumn {
    int index() default -1; // 列的索引,从0开始

    String name() default ""; // 列名,可以与Excel中的标题对应
}
java 复制代码
import com.alibaba.fastjson.JSON;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.*;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.*;

public class ExcelParserUtil {

    /**
     * excel解析工具类
     *
     * @param inputStream
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> List<T> parseExcel(InputStream inputStream, Class<T> clazz) throws Exception {
        List<T> result = new ArrayList<>();
        List<String> failReasonList = new ArrayList<>();
        int execRow = 1;
        try (Workbook workbook = new XSSFWorkbook(inputStream)) {
            Sheet sheet = workbook.getSheetAt(0); // 假设我们只处理第一个工作表
            Row headerRow = sheet.getRow(0); // 假设第一行是标题行

            // 读取实体类的字段注解,构建字段名和列索引的映射
            Map<String, Integer> fieldIndexMap = new HashMap<>();
            Field[] fields = clazz.getDeclaredFields();
            for (Field field : fields) {
                ExcelColumn excelColumn = field.getAnnotation(ExcelColumn.class);
                if (excelColumn != null && excelColumn.index() >= 0) {
                    field.setAccessible(true);
                    fieldIndexMap.put(field.getName(), excelColumn.index());
                }
            }

            // 跳过标题行,从第二行开始解析数据
            for (int i = 1; i <= sheet.getLastRowNum(); i++) {
                Row row = sheet.getRow(i);
                T instance = clazz.getDeclaredConstructor().newInstance();
                for (Map.Entry<String, Integer> entry : fieldIndexMap.entrySet()) {
                    try {
                        String fieldName = entry.getKey();
                        int columnIndex = entry.getValue();
                        Cell cell = row.getCell(columnIndex);
                        if (cell != null) {
                            Field field = clazz.getDeclaredField(fieldName);
                            setFieldValue(instance, field, cell);
                        }
                    } catch (Exception e) {
                        failReasonList.add("第" + execRow + "行解析错误:" + e.getMessage());
                    }
                }
                execRow++;
                result.add(instance);
            }
        } catch (Exception e) {
            throw new Exception("parseExcel->数据解析错误:", e);
        }
        if (CollectionUtils.isNotEmpty(failReasonList)) {
            //System.out.println(JSON.toJSON(failReasonList));
            throw new Exception(JSON.toJSONString(failReasonList.subList(0, failReasonList.size() > 20 ? 20 : failReasonList.size())));
        }
        return result;
    }

    private static void setFieldValue(Object instance, Field field, Cell cell) throws IllegalAccessException, InvocationTargetException {
        Class<?> fieldType = field.getType();
        field.setAccessible(true);
        // 总是将Cell设置为STRING类型,以确保我们可以获取字符串值
        cell.setCellType(CellType.STRING);
        String cellValue = cell.getStringCellValue();
        if (String.class.equals(fieldType)) {
            field.set(instance, cell.getStringCellValue());
        } else if (Integer.class.equals(fieldType) || int.class.equals(fieldType)) {
            field.setInt(instance, Integer.parseInt(cellValue));
        } else if (Long.class.equals(fieldType) || long.class.equals(fieldType)) {
            field.setLong(instance, Long.parseLong(cellValue));
        } else if (Double.class.equals(fieldType) || double.class.equals(fieldType)) {
            field.setDouble(instance, Double.parseDouble(cellValue));
        } else if (Boolean.class.equals(fieldType) || boolean.class.equals(fieldType)) {
            field.setBoolean(instance, Boolean.parseBoolean(cellValue));
        } else {
            throw new RuntimeException("Unsupported field type: " + fieldType.getName());
        }
    }


    public static void main(String[] args) throws FileNotFoundException {
        // 假设你有一个方法可以从某个地方获取输入流,这里我们用一个示例输入流代替
        ExcelParserUtil parser = new ExcelParserUtil();
        File file = new File("/Users/xxx/Downloads/xxx.xlsx"); // 替换为你的文件路径
        InputStream inputStream = new FileInputStream(file);
        try {
            if (inputStream != null) {
                List<MyEntity> entityList = parser.parseExcel(inputStream, MyEntity.class);
                // 输出解析结果
                for (MyEntity entity : entityList) {
                    System.out.println(entity.toString());
                }
            } else {
                System.out.println("无法找到或加载Excel文件。");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
相关推荐
小蒜学长2 分钟前
“喵汪联盟”宠物领养系统的设计与实现(代码+数据库+LW)
java·spring boot·后端·宠物
程序员黑豆4 分钟前
Java字符串常量池完全指南:原理、intern()方法与性能优化最佳实践
java·前端·ai编程
AI人工智能+电脑小能手7 分钟前
大白话说Java设计模式-14-适配器模式(业务实战篇)
java·设计模式·适配器模式·系统兼容·多渠道对接
xbgRS3 小时前
springboot的自动装配
java·spring boot
嘻哈∠※10 小时前
0061基于 SpringBoot 的投稿与稿件处理系统设计与实现
java·spring boot·后端
白狐_79811 小时前
408数据结构第8章:排序②——性质对比秒杀、场景选择与外部排序
java·数据结构·算法
zander25811 小时前
LeetCode 84:柱状图中的最大矩形——单调栈如何确定左右边界
java·数据结构·算法
码匠许师傅12 小时前
【C++ 面试真题】聊聊 C++ 的拷贝构造与拷贝赋值
java·c++·面试
极创信息13 小时前
系统安全隐患全方位排查方案:标准化渗透测试全流程
java·opencv·struts·数据挖掘·eclipse·语音识别·hibernate
汉字萌萌哒14 小时前
2019CCF-CSP入门级C++试题解析
java·开发语言·c++