首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >excel文件下载demo案例

excel文件下载demo案例

作者头像
在水一方
发布2022-06-14 16:50:33
发布2022-06-14 16:50:33
1.1K0
举报
文章被收录于专栏:在水一方在水一方

实际项目中excel文件下载是一个非常常见的功能,对于这个部分功能来做一个整理

代码语言:javascript
复制
   <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.17</version>
        </dependency>

后端代码

代码语言:javascript
复制
 @GetMapping("/exportExcel")
    @ApiOperation("可根据查询条件导出excel")
    public void exportExcel(HttpServletResponse response,@RequestParam(value = "mntOrgName",required = false) String mntOrgName ) throws IOException {
        response.setCharacterEncoding("UTF-8");
        String orgId = FilterContextHandler.getOrgID();
        mntOrgName = mntOrgName == null ? null : mntOrgName.trim();
        List<MntOrg> list = mntTeamOrgService.searchAll(mntOrgName,orgId);
        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet("信息表");

        HSSFRow titleRow = sheet.createRow(0);
        // 设置单元格的宽度
        for (int i = 0; i < 4; i++) {
            sheet.setColumnWidth(i, 9000);

        }

        titleRow.createCell(0).setCellValue("机构名称");
        titleRow.createCell(1).setCellValue("联系人电话");
        titleRow.createCell(2).setCellValue("传真");
        // titleRow.createCell(4).setCellValue("描述");
        for (MntOrg mntOrg : list) {
            HSSFRow dataRow = sheet.createRow(sheet.getLastRowNum() + 1);
            dataRow.createCell(0).setCellValue(mntOrg.getMntOrgName());
            dataRow.createCell(1).setCellValue(mntOrg.getContactNo());
            dataRow.createCell(2).setCellValue(mntOrg.getFax());
        }
        String filename = DateUtil.getNowDate() + ".xls";
        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Content-disposition", "attachment;filename="+filename);
        OutputStream ouputStream = response.getOutputStream();
        wb.write(ouputStream);
        ouputStream.flush();
        ouputStream.close();
    }

vue前端代码

代码语言:javascript
复制
download(){
          this.$axios.get('/exam/downloadVerifierTemplate', { responseType: "blob" }).then(res => {
          const fileName = "测试表格123.xlsx";  
          let nowDate = new Date()
       let date = {
       year: nowDate.getFullYear(),
       month: nowDate.getMonth() + 1,
        date: nowDate.getDate()
  }
    this.systemTime = date.year + '-' + date.month + '-' + date.date
    console.log(this.systemTime)
        
          //   res.data:请求到的二进制数据 
          const blob = new Blob([res.data], { type: "application/vnd.ms-excel" }); //1.创建一个blob
          const link = document.createElement("a"); //2.创建一个a链接
          link.download = fileName; //3.设置名称
          link.style.display = "none"; // 4.默认不显示
          link.href = URL.createObjectURL(blob); // 5.设置a链接href
          document.body.appendChild(link);//6.将a链接dom插入当前html中
          link.click();//7.点击事件
          URL.revokeObjectURL(link.href); //8.释放url对象
          document.body.removeChild(link); //9.移除a链接dom
      });
     }
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 后端代码
  • vue前端代码
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档