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;
    }
相关推荐
ITUnicorn2 小时前
【Vue2+SpringBoot在线商城】13-本项目运用到的技术
java·spring boot·redis·后端
仰泳之鹅2 小时前
【杂谈】C语言中的链接属性、声明周期以及static关键字
java·c语言·前端
weixin_531651812 小时前
Java 正则表达式
java·正则表达式
空空kkk2 小时前
Spring Boot项目的搭建
java·spring boot·后端
2501_940315262 小时前
【无标题】(leetcode933)最近的请求次数
java·前端·javascript
Sheep Shaun2 小时前
深入理解红黑树:从概念到完整C++实现详解
java·开发语言·数据结构·c++·b树·算法
每天学习一丢丢2 小时前
Spring Boot 调用泛微 E9 Token 认证 + 创建流程完整教程
java·spring boot·后端
苦逼的老王2 小时前
《java-使用kkview+libreoffice 实现在线预览ppt、xls、doc、pdf..》
java·pdf·powerpoint
没有bug.的程序员2 小时前
Spring Boot 启动原理:从 @SpringBootApplication 到自动配置深度解析
java·spring boot·后端·python·springboot·application