目 录CONTENT

文章目录

excel文件下载demo案例

在水一方
2022-02-24 / 0 评论 / 0 点赞 / 1,348 阅读 / 2,991 字 / 正在检测是否收录...

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

   <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.17</version>
        </dependency>

后端代码

@GetMapping("/exportExcel")
    public void exportExcel(HttpServletResponse response, String mntOrgName ) throws IOException {
        String fileName = "poi测试导出"+".xls";
        fileName = new String(fileName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1);
        response.setCharacterEncoding("UTF-8");
        List<MntOrg> list = Arrays.asList(new MntOrg("技术部", "123", "010"), new MntOrg("市场部", "907", "123213"));
        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet("信息表");
        HSSFSheet sheet2 = wb.createSheet("表2");

        HSSFRow titleRow = sheet.createRow(0);
        HSSFRow titleRow2 =  sheet2.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("传真");
        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());
        }

        for (int j = 0; j < 4; j++) {
            sheet2.setColumnWidth(j, 9000);

        }
        titleRow2.createCell(0).setCellValue("机构名称");
        titleRow2.createCell(1).setCellValue("联系人电话");
        titleRow2.createCell(2).setCellValue("传真");
        // titleRow.createCell(4).setCellValue("描述");
        for (MntOrg mntOrg : list) {
            HSSFRow dataRow2 = sheet2.createRow(sheet2.getLastRowNum() + 1);
            dataRow2.createCell(0).setCellValue(mntOrg.getMntOrgName());
            dataRow2.createCell(1).setCellValue(mntOrg.getContactNo());
            dataRow2.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前端代码


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
      });
     }

0

评论区