SSM+Vue导出excel文件

24 篇文章 1 订阅

引入maven依赖


<!--    Excel文件导出-->
    <!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
    <dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi</artifactId>
      <version>3.17</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
    <dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi-ooxml</artifactId>
      <version>3.17</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml-schemas -->
    <dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi-ooxml-schemas</artifactId>
      <version>3.17</version>
    </dependency>

controller

//excel导出
    @RequestMapping("/export")
    @ResponseBody
    public String export(HttpServletResponse response, FreVO freVO) throws IOException {
        response.setCharacterEncoding("UTF-8");
        List<Freceipt> list = freceiptService.queryExcelInfo(freVO);
        System.out.println(list);
        //创建excel文件
        HSSFWorkbook wb = new HSSFWorkbook();
        //创建sheet页
        HSSFSheet sheet = wb.createSheet("保单信息表");
        //创建标题行
        HSSFRow titleRow = sheet.createRow(0);
        titleRow.createCell(0).setCellValue("ID");
        titleRow.createCell(1).setCellValue("姓名");
        titleRow.createCell(2).setCellValue("电话");
        titleRow.createCell(3).setCellValue("身份证");
        titleRow.createCell(4).setCellValue("地址");
        titleRow.createCell(5).setCellValue("班级");
        titleRow.createCell(6).setCellValue("年段");
        titleRow.createCell(7).setCellValue("年龄");
        titleRow.createCell(8).setCellValue("籍贯");
        titleRow.createCell(9).setCellValue("宗教");
        

//	        titleRow.createCell(9).setCellValue("创建时间 ");
//	        titleRow.createCell(10).setCellValue("更新时间");
        //遍历将数据放到excel列中
        for (Freceipt freceipt : list) {
            HSSFRow dataRow = sheet.createRow(sheet.getLastRowNum()+1);
            dataRow.createCell(0).setCellValue(freceipt.getId());
            dataRow.createCell(1).setCellValue(freceipt.getPolicyno());
            dataRow.createCell(2).setCellValue(freceipt.getDeptName());
            dataRow.createCell(3).setCellValue(freceipt.getOvernum());
            dataRow.createCell(4).setCellValue(freceipt.getOperator());
            dataRow.createCell(5).setCellValue(freceipt.getApprover());
            dataRow.createCell(6).setCellValue(freceipt.getCutScale());
            dataRow.createCell(7).setCellValue(freceipt.getCutMoney());
            dataRow.createCell(8).setCellValue(freceipt.getRealMonthCut());
            dataRow.createCell(9).setCellValue(freceipt.getCutNum());
            
        }
            /*   // 设置下载时客户端Excel的名称
            String filename =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + ".xls";
               response.setContentType("application/vnd.ms-excel");
               response.setHeader("Content-disposition", "attachment;filename=" + filename);  */

        // 设置下载时客户端Excel的名称   (上面注释的改进版本,上面的中文不支持)
        response.setContentType("application/octet-stream;charset=utf-8");
        response.setHeader("Content-Disposition", "attachment;filename="
                + new String("保单信息".getBytes(),"iso-8859-1") + ".xls");


        OutputStream ouputStream = response.getOutputStream();
        wb.write(ouputStream);
        ouputStream.flush();
        ouputStream.close();

        return ouputStream.toString();


    }

vue

exportExcel() {
		this.$axios.post("/freceipt/export", this.$qs.stringify(this.checkform), {
				responseType: 'arraybuffer'
			})
			.then(res => {
				console.log(res.data);
				if (res) {
					// _this.loading = false;
					const aLink = document.createElement("a");
					let blob = new Blob([res.data], {
						type: "application/vnd.ms-excel"
					})
					aLink.href = URL.createObjectURL(blob)
					aLink.setAttribute('download', '保单信息' + '.xlsx') // 设置下载文件名称
					aLink.click()
					this.$refs.loadElement.appendChild(aLink)
				}
			})
			.catch(error => {
				console.log(error);
			})
}
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值