springboot-实现csv文件导出功能

excle文件导出,会遇到一个65535行限制的问题,就是导出的数据行数超过65535行就会导出失败,这个是excle本生的限制,这种情况下通常将导出的格式改成csv这样就可以跨过这个限制,同时生成的csv文件用office打开浏览效果与打开excle没有区别可以完美替代。

1、依赖(比导出excle还少两个依赖)

复制代码
 <!--csv导出依赖 -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>4.1.1</version>
        </dependency>

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>4.1.1</version>
        </dependency>

2、代码

复制代码
@PostMapping("/csvExport")
    @ResponseBody
    public  void csvExport(HttpServletResponse response) {
        //表头
        List<String> headerList = Arrays.asList("编号", "姓名");

        // 创建工作簿
        Workbook workbook = new XSSFWorkbook();
        // 创建工作表
        Sheet sheet = workbook.createSheet("Sheet1");
        // 创建表头行
        Row headerRow = sheet.createRow(0);
        // 写入表头
        for (int i = 0; i < headerList.size(); i++) {
            Cell cell = headerRow.createCell(i);
            cell.setCellValue(headerList.get(i));
        }
        // 组织数据
        List<List<Object>> data = new ArrayList<>();

        List list1 = Arrays.asList("001","张三");
        List list2 = Arrays.asList("002","李四");
        List list3 = Arrays.asList("003","王五");
        List list4 = Arrays.asList("004","王五");
        data.add(list1);
        data.add(list2);
        data.add(list3);
        data.add(list4);

        // 写入数据
        for (int i = 0; i < data.size(); i++) {
            Row row = sheet.createRow(i + 1);
            List<Object> rowData = data.get(i);
            for (int j = 0; j < rowData.size(); j++) {
                Cell cell = row.createCell(j);
                cell.setCellValue(rowData.get(j).toString());
            }
        }
        //response为HttpServeltReponse对象
        response.setContentType(MediaType.TEXT_PLAIN_VALUE + ";charset=utf-8");
        response.setHeader("Content-Disposition", "attachment;filename=1.csv");
        try {
            workbook.write(response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭工作簿,释放内存
            try {
                workbook.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

3、postman调用

4、导出效果

源码获取方式(免费):

(1)登录-注册:http://resources.kittytiger.cn/

(2)签到获取积分

(3)搜索:springboot-csvExport csv文件导出

相关推荐
坐吃山猪3 小时前
SpringBoot2.7集成Knife4j
springboot·knife4j
AI开源软件2 天前
SpringBoot信创项目怎么验收?从数据库迁移到交付证据
postgresql·springboot·达梦·opengauss·信创·企业管理系统
咖啡八杯2 天前
Redis 工具封装:RedisCache 对 Operations 体系的二次封装
java·数据库·redis·缓存·框架·springboot·若依
西凉的悲伤5 天前
Spring Boot 条件装配注解详解
java·springboot·1024程序员节·条件装配
xieliyu.6 天前
【JavaEE进阶】Spring Boot 日志:从 System.out 到 @Slf4j,6 个级别 + 4 类配置一篇讲透
java·spring boot·spring·java-ee·springboot·logback·slf4j
AI开源软件6 天前
SpringBoot + Vue3 人力资源管理系统推荐:组织、档案与移动办公
vue3·springboot·ruoyi·oa系统·移动办公·人力资源管理系统·hrm
AI源码7 天前
社区医院儿童健康信息管理系统
springboot
AI源码7 天前
社区论坛系统
springboot
AI源码7 天前
图书馆借阅管理系统
vue3·springboot
孤狼GPT8 天前
ChatGPT、Codex排查实录:Kafka消费者日志明明显示处理成功,为什么同一条消息还是被重复消费?
kafka·springboot·codex·chatgptplus·chatgptpro