Today, I saw an article about the difference between POI and EasyExcel (EasyExcel is a simple and save Excel based on Java), because I only used POI, so I want to try how to use EasyExcel, so I have this article

EasyExcelOfficial document address:www.yuque.com/easyexcel/d…

1. Introduce dependencies

<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId>  </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <! --MybatisPlus--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.2. 0</version> </dependency> <! --EasyExcel--> <dependency> <groupId>com.alibaba</groupId> <artifactId>easyexcel</artifactId> <version>2.24.</version>
        </dependency>
        
    </dependencies>
Copy the code

2. Configure mysql database information and use code generator to generate DAO files

2.1 inapplication.ymlTo configure the database information

spring:
  datasource:
    url: jdbc:mysql://ip:port/dbName? useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull&serverTimezon e=Asia/Shanghai&allowMultiQueries=true&rewriteBatchedStatements=trueApplication: name: excel-demo server: port:8081Mybatis -plus: mapper-locations: classpath:/mapper/*.xml  
Copy the code

2.2 Generated using the code generator of mybatisPlusservice,dao,mapperAnd other documents

See my previous article “MybatisPlus Code Generator” for more details.

Results:

3. Test the function of exporting Excel

3.1,TestController.java

/ * * *@author miao
 */
@RestController
@RequestMapping("/test")
public class TestController {

    @Resource
    private IUserService userService;

    @RequestMapping("/request")
    private String test(a) {
        return "ok";
    }

    @RequestMapping("/download")
    private void download(HttpServletResponse response) {
        List<User> list = userService.list();

        / / write 1
        String fileName = System.currentTimeMillis() + ".xlsx";
        try {
            
            response.setContentType("application/vnd.ms-excel; charset=utf-8");
            response.setHeader("Content-Disposition"."attachment; filename=" + fileName + ".xls");
            ServletOutputStream out = response.getOutputStream();

            EasyExcel.write(response.getOutputStream(), UserVo.class)
                    .sheet("sheet")
                    .doWrite(list);

        } catch (IOException e) {

            throw new RuntimeException("Failed to export file!"); }}}Copy the code

3.2 Requesting an Addresshttp://localhost:8081/test/request

Results:

== The test was successful, but the layout is still too ugly, you can set the header by yourself, but I did not study, I will study again when I have time! I hope you can give me a thumbs-up. The code address is put below. If you have any questions, you can leave a message

Github address: github.com/miaomk/Easy…