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文件导出

相关推荐
金牌归来发现妻女流落街头15 小时前
【Spring Boot注解】
后端·springboot
千寻技术帮2 天前
10368_基于SpringBoot的共享租车管理系统
源码·springboot·安装·文档·共享单车·单车租赁
闻哥2 天前
Redis 避坑指南:从命令到主从的全链路踩坑实录
java·数据库·redis·缓存·面试·springboot
悟空码字2 天前
SpringBoot深度整合高德地图,构建高性能位置服务
java·springboot·高德地图·编程技术·后端开发
千寻技术帮3 天前
10392_基于SpringBoot的大学迎新系统
mysql·vue·源码·springboot·代码·新生报到
曹轲恒4 天前
配置文件的占位符
springboot
code袁5 天前
基于微信小程序的宿舍维修小程序的设计与实现
微信小程序·小程序·毕业设计·springboot·notepad++·宿舍维修小程序
二哈喇子!6 天前
基于Spring Boot框架的网络游戏虚拟交易平台的设计与实现
java·springboot·毕设项目
JavaGuide7 天前
IntelliJ IDEA 2026.1 EAP 发布!拥抱 Java 26,Spring Boot 4 深度支持!
java·后端·mysql·springboot·idea·大厂面试·javaguide