工具类

入参

dart 复制代码
@RequestBody List<Map<String, Object>> dataList
dart 复制代码
Sheet sheet = convertDataListToSheet(dataList);
dart 复制代码
 List<List<String>> data = wrapListMapToList(dataList);
dart 复制代码
在这里插入代码片
dart 复制代码
    private List<List<String>> wrapListMapToList(List<Map<String, Object>> dataList) {
        List<List<String>> data = new ArrayList<>();//  [["1","4"],[33,666]]
        for (Map<String, Object> row : dataList) {
            List<String> rowData = new ArrayList<>();
            for (Map.Entry<String, Object> entry : row.entrySet()) {
                rowData.add(entry.getValue().toString());
            }
            data.add(rowData);
        }
        return data;
    }
dart 复制代码
    //前8行是空白
    public static Sheet convertDataListToSheets(List<Map<String, Object>> dataList) {
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet();

        // Insert 8 blank rows before starting the data
        for (int i = 0; i < 8; i++) {
            sheet.createRow(i);
        }

        // Populate sheet with data starting from the ninth row
        int rowIndex = 8; // Start from the ninth row
        for (Map<String, Object> data : dataList) {
            Row row = sheet.createRow(rowIndex++);
            int cellIndex = 0;
            for (Map.Entry<String, Object> entry : data.entrySet()) {
                Cell cell = row.createCell(cellIndex++);
                setCellValue(cell, entry.getValue());
            }
        }

        return sheet;
    }

//前8行是空白 第九行第一列去掉不读

dart 复制代码
    public static Sheet convertDataListToSheetRemoveNineAndOne(List<Map<String, Object>> dataList) {
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet();

        // Insert 8 blank rows before starting the data
        for (int i = 0; i < 8; i++) {
            sheet.createRow(i);
        }

        // Populate sheet with data starting from the ninth row
        int rowIndex = 8; // Start from the ninth row
        for (Map<String, Object> data : dataList) {
            Row row = sheet.createRow(rowIndex++);
            int cellIndex = 0;
            for (Map.Entry<String, Object> entry : data.entrySet()) {
                Cell cell = row.createCell(cellIndex++);
                setCellValue(cell, entry.getValue());
            }
        }

        // Remove the first cell of the ninth row (index 8)
        if (sheet.getRow(8) != null && sheet.getRow(8).getCell(0) != null) {
            sheet.getRow(8).removeCell(sheet.getRow(8).getCell(0));
        }

        return sheet;
    }

    private static void setCellValue(Cell cell, Object value) {
        if (value instanceof String) {
            cell.setCellValue((String) value);
        } else if (value instanceof Number) {
            cell.setCellValue(((Number) value).doubleValue());
        } else if (value instanceof Boolean) {
            cell.setCellValue((Boolean) value);
        } else if (value instanceof java.util.Date) {
            cell.setCellValue((java.util.Date) value);
        } else {
            cell.setCellValue(value != null ? value.toString() : "");
        }
    }
dart 复制代码
    private List<List<String>> wrapListMapToList(List<Map<String, Object>> dataList) {
        List<List<String>> data = new ArrayList<>();//  [["1","4"],[33,666]]
        for (Map<String, Object> row : dataList) {
            List<String> rowData = new ArrayList<>();
            for (Map.Entry<String, Object> entry : row.entrySet()) {
                rowData.add(entry.getValue().toString());
            }
            data.add(rowData);
        }
        return data;
    }
相关推荐
一定要AK24 分钟前
Spring 入门核心笔记
java·笔记·spring
A__tao25 分钟前
Elasticsearch Mapping 一键生成 Java 实体类(支持嵌套 + 自动过滤注释)
java·python·elasticsearch
KevinCyao38 分钟前
java视频短信接口怎么调用?SpringBoot集成视频短信及回调处理Demo
java·spring boot·音视频
迷藏49444 分钟前
**发散创新:基于Rust实现的开源合规权限管理框架设计与实践**在现代软件架构中,**权限控制(RBAC)** 已成为保障
java·开发语言·python·rust·开源
wuxinyan1232 小时前
Java面试题47:一文深入了解Nginx
java·nginx·面试题
新知图书2 小时前
搭建Spring Boot开发环境
java·spring boot·后端
冰河团队2 小时前
一个拉胯的分库分表方案有多绝望?整个部门都在救火!
java·高并发·分布式数据库·分库分表·高性能
洛_尘2 小时前
Java EE进阶:Linux的基本使用
java·java-ee
宸津-代码粉碎机2 小时前
Spring Boot 4.0虚拟线程实战调优技巧,最大化发挥并发优势
java·人工智能·spring boot·后端·python
MaCa .BaKa2 小时前
47-心里健康咨询平台/心理咨询系统
java·spring boot·mysql·tomcat·maven·intellij-idea·个人开发