工具类

入参

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;
    }
相关推荐
苹果醋337 分钟前
React源码02 - 基础知识 React API 一览
java·运维·spring boot·mysql·nginx
Hello.Reader1 小时前
深入解析 Apache APISIX
java·apache
菠萝蚊鸭1 小时前
Dhatim FastExcel 读写 Excel 文件
java·excel·fastexcel
旭东怪2 小时前
EasyPoi 使用$fe:模板语法生成Word动态行
java·前端·word
007php0072 小时前
Go语言zero项目部署后启动失败问题分析与解决
java·服务器·网络·python·golang·php·ai编程
∝请叫*我简单先生2 小时前
java如何使用poi-tl在word模板里渲染多张图片
java·后端·poi-tl
ssr——ssss2 小时前
SSM-期末项目 - 基于SSM的宠物信息管理系统
java·ssm
一棵星2 小时前
Java模拟Mqtt客户端连接Mqtt Broker
java·开发语言
鲤籽鲲2 小时前
C# Random 随机数 全面解析
android·java·c#
zquwei3 小时前
SpringCloudGateway+Nacos注册与转发Netty+WebSocket
java·网络·分布式·后端·websocket·网络协议·spring