easyexcel導出兩種方式response返回文件流下載和保存到服務器返回下載鏈接


1、response方式返回excel文件流

@GetMapping("/exportExcel")
    public void exportExcel(@RequestParam(value = "menu") String menu,
                            @RequestParam(value = "dwflglId") int dwflglId,
                            @RequestParam(value = "qjCode") String qjCode,
                            HttpServletResponse response) throws IOException {
      //內容集合
        List<ZjlrysIndexView> jthz = getJthz(dwflglId, qjCode);
        //表頭集合
        List<ZjlrysTableHead> header = getHeaderOfHz(dwflglId, qjCode);
        response.setHeader("Content-Disposition", "attachment; filename=Zjlryshz.xlsx");
        // 響應類型,編碼
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        String bigTitle = "資金流入預算表";
        EasyExcel.write(response.getOutputStream()).head(getExcelHeader(header, bigTitle)).registerWriteHandler(new Custemhandler()).sheet("資金流入預算導出模板").doWrite(getExportData(jthz));
    }

2、通過在服務器中生成excel臨時文件,返回下載鏈接給前端下載

            // 獲取當前時間
            String updTm = DateUtil.getAllTime();
            //filePath為服務器存放臨時文件的路徑,本地開發環境的時候先可以用本地電腦的路徑,比如c:users/,這里加上時間是因為不想被覆蓋,也可以加uuid之類的
            FileOutputStream fileOutputStream = new FileOutputStream(filePath + "xx導出模板" + updTm + ".xlsx");

            EasyExcel.write(fileOutputStream).head(getDownLoadExcelHead())
                    .registerWriteHandler(new CustomSheetWriteHandler(map)).registerWriteHandler(new Custemhandler())
                    .sheet("數據字典導出模板").doWrite(downLoadExcelData);
            //url_prefix為服務器的域名之類的下載路徑
            String url = url_prefix + "xx導出模板" + updTm + ".xlsx";
            

            /*
             * try {
             * 
             * // 若不進行編碼在IE下會亂碼
             * 
             * url = URLEncoder.encode(url, "UTF-8");
             * 
             * } catch (UnsupportedEncodingException e) {
             * 
             * e.printStackTrace();
             * 
             * }
             */    

 3、以流的形式上傳到服務器

//創建流
ByteArrayOutputStream bos=new ByteArrayOutputStream();
//生成easyexcel的流
EasyExcel.write(bos).head(getHead()).sheet(sheetName).doWrite(getData());
//然后把bos上傳到服務器,這里我是寫了上傳工具類,返回下載鏈接
String path=OssClientUtil.upload(fileName,bos.toByteArray());

參考網址:https://www.cnblogs.com/chenchengxuyuan/p/14374057.html

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM