当前位置: 首页 > article >正文

Easyexcel操作文件

常见问题汇总

1.往文件里写数据时,如果没有对应的实体类时,需要创建一个List<String,Map<Integer,String>>来保存一行的数据

private Map<Integer,Object> convertToMap(ResultSet rs){
    LinkedHashMap<Integer, Object> row = new LinkedHashMap<Integer, Object>;
    for (int i = 0; i < columns.size(); i++) {
        row.put(i,columns.get(i));
    }
    return row;
}

注意:如果与表头无法对应,数据插入不进去(则row中的key值从0开始)

2.文件无法打开

当时排查好久,发现流没有正常关闭,最后不要忘记finish();

3.poi于EasyExcel结合使用时注意事项

@Test
    void test38() throws IOException {

        // 模板文件路径(已用 POI 设置好样式)
        String templatePath = "D:\\idea代码\\demo1\\src\\main\\resources\\output1.xlsx";
        // 输出文件路径
        String outputPath = "D:\\idea代码\\demo1\\src\\main\\resources\\output2.xlsx";
        // 准备数据
        List<Map<Integer, Object>> data = new ArrayList<>();
        Map<Integer, Object> rowData = new HashMap<>();
        rowData.put(0, "张三");
        rowData.put(1, 25);
        rowData.put(2, "开发");
        rowData.put(3, "技术部");
        data.add(rowData);

        // 配置 EasyExcel
        ExcelWriter excelWriter = EasyExcel.write(outputPath)
                .withTemplate(templatePath) // 指定模板文件
                .build();

        // 指定从第2行开始写入数据(假设第0行是标题,第1行是表头)
        WriteSheet writeSheet = EasyExcel.writerSheet()
                .relativeHeadRowIndex(1) // 数据从第2行开始写入
                .build();

        // 写入数据
        excelWriter.write(data, writeSheet);
        excelWriter.finish();
        System.out.println("数据写入完成,样式已保留!");
    }

4.文件压缩注意事项

 private static boolean packFileIntoZip(String filePath, String zipFilePath) {
        try (FileInputStream fis = new FileInputStream(filePath);
             FileOutputStream fos = new FileOutputStream(zipFilePath);
             ZipOutputStream zipOut = new ZipOutputStream(fos)) {
            // 获取文件名(去掉路径)
            String fileName = new File(filePath).getName();
            System.out.println("正在压缩文件: " + fileName);
            // 创建 ZipEntry
            ZipEntry zipEntry = new ZipEntry(fileName);
            zipOut.putNextEntry(zipEntry);
            // 将文件内容读取为字节数组
            byte[] fileBytes = readAllBytes(fis);
            // 检查文件大小
            System.out.println("文件大小: " + fileBytes.length + " 字节");
            // 将字节数组写入 ZIP 文件
            zipOut.write(fileBytes);
            // 关闭当前条目
            zipOut.closeEntry();
            return true;
        } catch (FileNotFoundException e) {
            System.err.println("找不到文件: " + e.getMessage());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }


http://www.kler.cn/a/588489.html

相关文章:

  • ubuntu-linux-系统用户界面无法显示-弹出报警框!
  • Go语言的数据库分片
  • tkinter快键画布
  • 技术视界|构建理想仿真平台,加速机器人智能化落地
  • Polhemus Patriot 电磁式位置追踪系统空间定位系统
  • SpringBoot 和vue前后端配合开发网页拼图10关游戏源码技术分享
  • Linux操作系统6- 线程3(线程的取消,分离与独立存储)
  • 反射(第三篇)、代理模式、静态代理和动态代理、InvocationHandler实际应用
  • 第十六届蓝桥杯康复训练--2
  • 新手村:数据预处理-缺失值补充策略
  • 【RabbitMQ】事务机制、限流、负载均衡
  • 玩转python:通俗易懂掌握高级数据结构-collections模块之Counter
  • 零基础掌握分布式ID生成:从理论到实战的完整指南 [特殊字符]
  • 563采药
  • NocoBase 本周更新汇总:双因素身份认证(2FA)
  • 蓝桥杯学习-08序列二分
  • 【动手学深度学习】#2线性神经网络
  • 火焰图分析Java程序瓶颈
  • 第15章:ConvNeXt图像分类实战:遥感场景分类【包含本地网页部署、迁移学习】
  • git subtree在本地合并子仓库到主仓库