比POI好用的EasyExcel簡單使用記錄

今天看到了一篇文章說了POIEasyExcel的區(qū)別(說的是EasyExcel是基于java的簡單、省內(nèi)存的Excel),因為我只用過POI,所以想試一下EasyExcel如何使用,所以才有了這篇文章

EasyExcel的官方文檔地址:https://www.yuque.com/easyexcel/doc/easyexcel

1、引入依賴

 <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.2.4</version>
        </dependency>
        
    </dependencies>

2、配置mysql數(shù)據(jù)庫信息并使用代碼生成器生成dao文件

2.1 在application.yml中配置數(shù)據(jù)庫信息

spring:
  datasource:
    url: jdbc:mysql://ip:port/dbName?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull&serverTimezone=Asia/Shanghai&allowMultiQueries=true&rewriteBatchedStatements=true
    username: username #你自己的用戶名
    password: password #你自己的密碼
  application:
    name: excel-demo


server:
  port: 8081
# xml的位置
mybatis-plus:
  mapper-locations: classpath:/mapper/*.xml  

2.2 使用mybatisPlus的代碼生成器生成service、daomapper等文件

詳情請看我上一篇文章《MybatisPlus 代碼生成器》

結(jié)果:

image

3、測試導(dǎo)出excel的功能

3.1、TestController.java

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

    @Resource
    private IUserService userService;

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

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

        // 寫法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("導(dǎo)出文件失敗!");
        }

    }
}

3.2 請求地址 http://localhost:8081/test/request

結(jié)果:


image

測試成功了,但是這個排版還是太丑了,是可以自己設(shè)置表頭的,但是我沒有研究,等有時間再研究吧!希望大家點個贊吧,代碼地址放在下面,有問題的可以留言交流喲

github地址:https://github.com/miaomk/EasyExcel-demo

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容