java读取excel文件返回JSON

java读取excel文件返回JSON

bash 复制代码
/**
     * 读取文件,返回JSON格式
     *
     * @param titleRowNum   表头行 1--表示第一行是表头,2--表示第一行和第二行都是表头
     * @param excelFile
     * @param typeReference
     * @param <T>
     * @return
     */
    public static <T> List<T> readExcelJSON(int titleRowNum, File excelFile, TypeReference<T> typeReference) {
        cn.hutool.poi.excel.ExcelReader excelReader = ExcelUtil.getReader(excelFile);
        int sheetCount = excelReader.getSheetCount();

        List<T> resultList = new ArrayList<>();

        for (int sheetNum = 0; sheetNum < sheetCount; sheetNum++) {
            excelReader.setSheet(sheetNum);
            // 构建表头结构
            List<List<Object>> titleRowsList = excelReader.read(0, titleRowNum - 1);
            List<List<String>> titleColumnList = new ArrayList<>(titleRowsList.get(0).size());
            // 将表头第一行初始化到表头list
            titleRowsList.get(0).forEach(titleRowColumnValue -> {
                List<String> titleColumnValueList = new ArrayList<>();
                titleColumnValueList.add(titleRowColumnValue.toString());
                titleColumnList.add(titleColumnValueList);
            });
            // 拼接表头其他行
            for (int titleRowIndex = 1; titleRowIndex < titleRowsList.size(); titleRowIndex++) {
                List<Object> titleRowList = titleRowsList.get(titleRowIndex);
                for (int rowIndex = 0; rowIndex < titleRowList.size(); rowIndex++) {
                    if (!titleRowList.get(rowIndex).toString()
                            .equals(titleColumnList.get(rowIndex).get(titleColumnList.get(rowIndex).size() - 1))) {
                        titleColumnList.get(rowIndex).add(titleRowList.get(rowIndex).toString());
                    }
                }
            }
            // 初始化json解析模板
            JSONObject jsonDataTemplate = new JSONObject();
            titleColumnList.forEach(columnList -> {
                if (columnList.size() > 1) {
                    for (int columnIndex = 0; columnIndex < columnList.size() - 1; columnIndex++) {
                        jsonDataTemplate.put(columnList.get(columnIndex), new JSONObject());
                    }
                }
            });

            // 读取数据
            List<List<Object>> valueRowsList = excelReader.read(titleRowNum);
            valueRowsList.forEach(valueRowList -> {
                JSONObject rowValueJson = JSONObject.parseObject(jsonDataTemplate.toJSONString());
                for (int valueRowIndex = 0; valueRowIndex < valueRowList.size(); valueRowIndex++) {
                    List<String> columnTitleList = titleColumnList.get(valueRowIndex);
                    if (columnTitleList.size() > 1) {
                        JSONObject tmeJson = rowValueJson;
                        for (int columnTitleIndex = 0; columnTitleIndex < columnTitleList.size(); columnTitleIndex++) {
                            if (columnTitleIndex != columnTitleList.size() - 1) {
                                tmeJson = rowValueJson.getJSONObject(columnTitleList.get(columnTitleIndex));
                            } else {
                                tmeJson.put(columnTitleList.get(columnTitleIndex), valueRowList.get(valueRowIndex));
                            }
                        }
                    } else {
                        rowValueJson.put(columnTitleList.get(0), valueRowList.get(valueRowIndex));
                    }
                }
                resultList.add(JSON.parseObject(rowValueJson.toJSONString(), typeReference));
            });

        }
        return resultList;
    }
相关推荐
星落梦生4 小时前
SpringAOP基础笔记(7)
java·开发语言
金銀銅鐵5 小时前
带您看懂字节码指令(四):一条指令里有什么
java·jvm
最强小杰5 小时前
某 OpenAI 兼容网关上的 `kimi-k3.1-pro`:Cline、Zed 接入与 SSE 排错示例
java·服务器·网络·ai
Flynn Liang6 小时前
流量治理不是限流那么简单:I/O 密集型 Java 服务的 6 层防线
java·微服务·高并发·限流·熔断·系统稳定性·resilience4j
学长毕设6 小时前
基于SpringBoot智慧养老服务平台设计与实现-附源码
java·spring boot·后端
曹牧6 小时前
Java:“刷新雪崩”
java·开发语言
秋天的怀念_David6 小时前
算法一千零一夜 · 第三十三夜 · 跳表
java·程序人生·算法
Forget the Dream8 小时前
Spring AI 2.0 系列(二):第一个 ChatClient——从注入到一次完整调用
java·人工智能·spring boot·ai·springai
wuyk5559 小时前
Python办公自动化-第02讲-Excel文件认知与四大库选型
开发语言·python·excel
黑十9 小时前
FDE 怎么快速搭一个可演示页面?最小前端方案
java·大数据·前端·数据库·人工智能