阿里EasyExcel導入導出Excel表格篇(前端JSP,後端SSM)簡單明瞭!(內含源碼)

Echarts之柱狀圖動態加載數據篇

文件上傳下載篇

點擊獲取源碼

老規矩,先上效果:

模擬業務流程:1.在本地的excel模板表中填寫好數據,點擊“選擇文件”→“上傳Excel表格”,將Excel表解析,並展示在頁面上,並持久化數據。

                          2.點擊“導出成Excel表格”,將頁面上的數據,導出成Excel表格。

前端使用H-ui修改的界面。

依賴:

<!--easyexcel依賴-->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>easyexcel</artifactId>
      <version>2.1.2</version>
    </dependency>
    <!--poi依賴-->
    <dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi</artifactId>
      <version>4.0.0</version>
    </dependency>
    <dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi-ooxml</artifactId>
      <version>4.0.0</version>
    </dependency>
    <dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi-ooxml-schemas</artifactId>
      <version>4.0.0</version>
    </dependency>

前端 部分 代碼:

<div class="mt-20">
        <table class="table table-border  table-bordered table-bg">
            <thead>
            <tr class="text-c">
                <th width="120">id</th>
                <th width="70">藥品名稱</th>
                <th width="80">銷售件數</th>
                <th width="200">藥品價格</th>
                <th>銷售日期</th>
                <th width="100">用戶名</th>
            </tr>
            </thead>
            <tbody>
            <c:forEach items="${excels}" var="sale">
                <tr class="text-c">
                    <td>${sale.id}</td>
                    <td>${sale.salmedicinename}</td>
                    <td>${sale.salamount}件</td>
                    <td>${sale.salprice}元</td> 
                    <%--格式化日期--%>
                    <td><fmt:formatDate value="${sale.saldate}" pattern="yyyy-MM-dd hh:mm:ss"/></td>
                    <td>${sale.salname}</td>
                </tr>
            </c:forEach>
            </tbody>
        </table>
    </div>
</div>

Controller:

/**
     * 解析上傳的excel表格,並顯示到頁面上
     * @param file
     * @param request
     * @return
     * @throws IOException
     */

    List<Excel> ExcelList = null;

    @RequestMapping("/excel/import")
    public String ImportExcel(@RequestParam MultipartFile file , HttpServletRequest request) throws IOException {

        ExcelExample example = new ExcelExample();
        //檢查數據庫excel表已經存在數據
        List<Excel> nullornot = tssi.ExcelIsNull(example);
        //存在的話,清空excel表數據
        if(nullornot.size() != 0){
            tssi.DeleteExcel();
        }

        InputStream inputStream = file.getInputStream();

        ExcelList = EasyExcel.read(inputStream)
                .head(Excel.class)
                // 設置sheet,默認讀取第一個
                .sheet()
                // 設置標題所在行數
                .headRowNumber(1)
                .doReadSync();

        //持久化excel表
        for (Excel excel : ExcelList) {
            tssi.InsertExcel(excel);
        }

        HttpSession session = request.getSession();
        session.setAttribute("excels" , ExcelList);

        return "redirect:/excel";
    }

    /**
     * 導出頁面上的excel表
     * @param response
     * @throws IOException
     */
    @RequestMapping("/excel/export")
    public void ExportExcel(HttpServletResponse response) throws IOException {

        // 獲取頁面上的Excel
        List<Excel> excels = new ArrayList<>();
        //將頁面上的數據導出成excel
        for ( Excel excel : ExcelList) {
            excels.add(excel);
        }


        //設置JSP頁面導出爲excel表
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        // 防止中文亂碼
        String fileName = URLEncoder.encode("導出", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), Excel.class)
                .sheet("sheet0")
                .doWrite(excels);
    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章