spring boot + easypoi快速實現excel導入導出

easypoi封裝了poi讓咱們可以很是簡單的實現Excel導出,Excel模板導出,Excel導入,Word模板導出等,具體可見官網:http://www.afterturn.cn/spring


我這邊實現了一個excel的導出,記錄一下。sql

一、pom文件引入starter

<dependency>
   <groupId>cn.afterturn</groupId>
   <artifactId>easypoi-spring-boot-starter</artifactId>
   <version>3.2.0</version>
</dependency>

二、定義須要導出的DTO

@Table(name = "ORDERS")
public class OrderExcelDto{

    @Excel(name = "ORDER_ID", width = 25,orderNum = "0")
    private String orderId;

    @Excel(name = "CREATE_TIME",width = 20,exportFormat = "yyyy-MM-dd HH:mm:ss", orderNum = "1")
    private Date createTime;

    @Excel(name = "STATUS",width = 20,replace = {"created_01", "payment_02","used_03","time out_04"}, orderNum = "2")
    private String status;

    public String getOrderId() {
        return orderId;
    }

    public void setOrderId(String orderId) {
        this.orderId = orderId;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }
}
具體語義以下,詳情可見官網
name:列明;replace:前者是最後替換的值,後者是數據庫的值;exportFormat:導出的時間格式,以這個是否爲空來判斷是否須要格式化日期;width:列寬等

三、將查詢的sql映射到上述的DTO,而後經過starter中提供工具類作導出操做

@GetMapping("/orderExport")
    public void orderExport(HttpServletResponse response) throws Exception {
        response.setHeader("content-Type", "application/vnd.ms-excel");
        response.setHeader("Content-Disposition", "attachment;filename=orders.xls");
        List<OrderExcelDto> list = orderService.queryOrderExcel();
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(),OrderExcelDto.class,list);
        workbook.write(response.getOutputStream());
    }

四、前臺js中能夠直接定義事件觸發上述get請求

function  export() {
    document.location.href = host+"/orderExport";
}

就這樣就能夠很是快速的實現一個excel導出了,還有不少好用的功能能夠看官網。數據庫

相關文章
相關標籤/搜索