1. 程式人生 > 實用技巧 >基於EasyExcel和boostrapTable的檔案匯入和匯出

基於EasyExcel和boostrapTable的檔案匯入和匯出

1.需要引入阿里巴巴的EasyExcel的依賴

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>easyexcel</artifactId>
            <version>2.2.6</version>
        </dependency>

2.封裝簡單的匯入匯出的工具

package com.woniu.erp.util;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter; import com.alibaba.excel.support.ExcelTypeEnum; import com.woniu.erp.entity.Repository; import org.junit.Test; import org.springframework.stereotype.Component; import javax.servlet.http.HttpServletResponse; import java.io.FileNotFoundException; import java.io.FileOutputStream;
import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.List; @Component public class ExcelUtil<E> { /** * 簡單的表格匯出工具 * @param list 匯出到表格的資料 * @param resp 響應物件 * @param sheetName 工作表名稱 * @return 是否匯出成功 */ public
boolean excelExport(List<E> list, HttpServletResponse resp, String sheetName) throws IOException { //如果傳入資訊不正確,直接返回false if (list == null || list.size() == 0 || resp == null){ return false; } //獲取傳入資料的class物件 Class<?> clazz = list.get(0).getClass(); //如果工作表名稱為空,預設修改為物件名稱 if (sheetName == null || "".equals(sheetName)){ sheetName = clazz.getSimpleName(); } //使用EasyExcel匯出資料到表格輸出到前端 EasyExcel.write(resp.getOutputStream(),clazz).sheet(sheetName).doWrite(list); return true; } /** * * @param inputStream 前端傳輸檔案的輸入流 * @param clazz 讀取表格轉換的物件class * @return */ public List<E> excelImport(InputStream inputStream,Class<?> clazz){ return EasyExcel.read(inputStream).head(clazz).sheet().doReadSync(); } }

封裝匯出工具的原理:

1.匯出的原理

方法的形參需要有 1.匯出物件的list集合(從後端根據條件通過service查出來的) 2.前端的響應HttpServlrtResponse resp 3.需要匯出的excel表名sheetName

<1>首先判斷list是否為空,如果不為空則通過list中的物件的到物件的class物件(clazz=list.get(0).getClass())

<2>判斷sheetname是否為空,如果sheetname為空的話則將class物件的名稱作為表名 sheetname=clazz.getSimpleName()

<3>呼叫easyexcel中的方法將表格輸出到前端,呼叫鏈式程式設計Easy.write(輸出流,物件的class物件).sheet(表名).doWrite(要匯出的物件集合)

response的作用是將查詢出來的list通過輸出流輸出到瀏覽器

class物件的作用是保證輸出的是這個物件的資料

sheet的作用是確保輸出表格的名稱

dowrite要輸出的集合

前端向後端傳送要匯出的資料的條件==》後端接受到資料後查詢出list集合,然後通過easyexcel的方法將list集合轉換成表格併發送到瀏覽器。

2.匯入的原理

方法的形參有 1.輸入流物件 2.物件的 class物件

通過easyexcel的鏈式程式設計將前端上傳的檔案的內容轉換成物件的list集合,然後再將list集合儲存到資料庫

3.檔案匯入和匯出的具體實現

《1.匯入》

    @RequestMapping("/importGoods")
    public Map<String,Object> importGoods(@RequestParam("file") MultipartFile multipartFile){
        try {
            List<Good> list = excelUtil.excelImport(multipartFile.getInputStream(), Good.class);//1.讀取前端的excel檔案並轉換成list集合
            Map<String,Object> map=new HashMap<>();
            if (list == null || list.size() == 0) {
                map.put("msg", "error");
                map.put("total",0);
                map.put("available",0);
                return map;
            }
            int row = goodsManageService.addGoodsList(list);//2.封裝向前端返回的結果
            map.put("total",list.size());
            map.put("msg","success");
            map.put("available",row);
            return map;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

《2.匯出》

   @RequestMapping("/exportGoods")
    public void exportGoods(String searchType, String keyWord, HttpServletResponse resp){
        Map<String, Object> goodsList = goodsManageService.getGoodsListBycons(searchType, keyWord);//1.根據前端傳過來的資料查詢相應的list集合
        List<Good> list = (List<Good>) goodsList.get("rows");
        resp.setHeader("Content-Disposition","attachment;fileName=goods.xlsx");//2.新增檔案下載的響應頭
        try {
            excelUtil.excelExport(list,resp,null);//3.將list轉換成excel表格並傳送到瀏覽器
        } catch (IOException e) {
            System.out.println("IO異常");
        }
    }

《3.物件的屬性需要加入的註解》

   @ExcelProperty(value = "商品id",index = 0)
    private Integer goodId;//商品id
    @ExcelProperty(value = "商品名稱",index = 1)
    private String goodName;//商品名稱
    @ExcelProperty(value = "商品型別",index = 2)
    private String goodType;//商品型別
    @ExcelProperty(value = "商品尺寸",index = 3)
    private String goodSize;//商品尺寸
    @ExcelProperty(value = "商品價值",index = 4)
    private Integer goodValue;//商品價值