EasyExcel 实现多个Sheet页导出

1 Maven依赖


        
            com.alibaba
            easyexcel
            2.1.6
        

2 实现代码

    /**
     * 导出多个sheet页数据
     *
     * @param response
     */
    @GetMapping("/exportMultiSheet")
    public void exportMultiSheet(HttpServletResponse response) {
        try {
            //第一个Sheet页数据
            List> dataList = new ArrayList<>();
            List headList = new ArrayList<>(Arrays.asList(new Object[]{"表头1", "表头2", "表头3"}));
            dataList.add(headList);

            //第二个Sheet页数据
            List> dataList2 = new ArrayList<>();
            List headList2 = new ArrayList<>(Arrays.asList(new Object[]{"表头1", "表头2", "表头3", "表头4"}));
            dataList2.add(headList2);
            String fileName = new String("文件名称.xls".getBytes(), "UTF-8");
            response.addHeader("Content-Disposition", "filename=" + fileName);
            //设置类型,扩展名为.xls
            response.setContentType("application/vnd.ms-excel");

            //将数据写入sheet页中
            ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).build();
            WriteSheet writeSheet1 = EasyExcel.writerSheet(0, "sheet1").build();
            WriteSheet writeSheet2 = EasyExcel.writerSheet(1, "sheet2").build();
            excelWriter.write(dataList, writeSheet1);
            excelWriter.write(dataList2, writeSheet2);
            excelWriter.finish();
            response.flushBuffer();
        } catch (Exception e) {
            e.printStackTrace();
        }
    } 
  

3 调试结果

EasyExcel 实现多个Sheet页导出_第1张图片

EasyExcel 实现多个Sheet页导出_第2张图片EasyExcel 实现多个Sheet页导出_第3张图片

旭东怪的个人空间_哔哩哔哩_Bilibili旭东怪,人生低谷不可怕,可怕的是坚持不到人生转折点的那一天;旭东怪的主页、动态、视频、专栏、频道、收藏、订阅等。哔哩哔哩Bilibili,你感兴趣的视频都在B站。https://space.bilibili.com/484264966?spm_id_from=333.337.search-card.all.click 

你可能感兴趣的:(文档处理工具,java,前端,服务器)