【工具】EasyExcl 导出功能(包含多sheet页)实现步骤

文章目录

  • 实现效果
  • 步骤:
    • 1.导入依赖
    • 2.controller代码

实现效果

【工具】EasyExcl 导出功能(包含多sheet页)实现步骤_第1张图片

多sheet页:
【工具】EasyExcl 导出功能(包含多sheet页)实现步骤_第2张图片

步骤:

1.导入依赖

		<dependency>
            <groupId>com.alibabagroupId>
            <artifactId>easyexcelartifactId>
            <version>2.1.2version>
        dependency>

2.controller代码


    /**
     *  批量下载  http://localhost:9999/download
     */
    @GetMapping("/download")
    public void download(HttpServletResponse response) throws IOException {
    	// 查出的数据
        List<User> list = userMapper.queryUserList();
        
        String fileName = "学生表.xlsx";
        fileName = URLEncoder.encode(fileName, "UTF-8");
        response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
        response.setContentType("application/vnd.ms-excel");
        EasyExcel.write(response.getOutputStream(),User.class)
                .sheet("test1")
                .doWrite(list);
        System.out.println("response=====》"+response);
    }


    /**
     *  多sheet页   http://localhost:9999/downloadSheets
     */
    @GetMapping("/downloadSheets")
    public void downloadSheets(HttpServletResponse response) throws IOException {
    	// 查出的数据
        List<User> userList = userMapper.queryUserList();
        List<Teacher> orderList = userMapper.queryTeacherList();

        String fileName = "订单表.xlsx";
        fileName = URLEncoder.encode(fileName, "UTF-8");
        response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
        response.setContentType("application/vnd.ms-excel");

        ServletOutputStream out = response.getOutputStream();
        ExcelWriter writer = EasyExcel.write(out).build();

        WriteSheet userSheet = EasyExcel.writerSheet(0, "学生表").head(User.class).build();
        writer.write(userList, userSheet);

        WriteSheet orderSheet = EasyExcel.writerSheet(1, "教师表").head(Teacher.class).build();
        writer.write(orderList, orderSheet);

        writer.finish();
    }

你可能感兴趣的:(java,spring,boot,mybatis,intellij-idea,maven,spring)