vue实现文件的导出_SpringBoot+Vue实现Excel导出

本文介绍如何在SpringBoot环境下,结合Vue来实现文件的导出,特别是Excel导出。通过调用接口,解决前端数据传输不触发文件下载的问题,利用文件流创建超链接并绑定点击事件,从而实现文件的直接下载。
摘要由CSDN通过智能技术生成
环境

SpringBoot 1.5.20Vue 2.5.2

SpringBoot
  • 依赖


<dependency>
<groupId>org.apache.poigroupId>
<artifactId>poi-ooxmlartifactId>
<version>4.0.1version>
dependency>
  • Controller

@PostMapping(value="/exportExcelAll")
@ApiOperation(value="导出", notes="导出记录")
@ApiParam(name = "", value = "", required = false)
public void exportExcel(HttpServletRequest request,HttpServletResponse response) {
    czjlService.exportExcelAll(request,response);
}
  • Service

public void exportExcelAll(HttpServletRequest request, HttpServletResponse response) {
try {

    List records = ... 业务逻辑这里就不粘贴了,查询要导出的数据即可
    @SuppressWarnings("resource")
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet("记录");
    HSSFRow row = null;int columnIndex = 0;
    row = sheet.createRow(0);
    row.setHeight((short) (22.50 * 20));//设置行高
    row.createCell(columnIndex).setCellValue("序号");
    row.createCell(++columnIndex).setCellValue("操作员编号");
    row.createCell(++columnIndex).setCellValue("登录账号");
    row.createCell(++columnIndex).setCellValue("功能链接地址");
    row.createCell(++columnIndex).setCellValue("链接地址附加参数");
    row.createCell(++columnIndex).setCellValue("操作开始时间");
    row.createCell(++columnIndex).setCellValue("操作结束时间");
    row.createCell(++columnIndex).setCellValue("操作结果");
    row.createCell(++columnIndex).setCellValue("结果原因描述");
    row.createCell(++columnIndex).setCellValue("操作员IP地址");
    row.createCell(++columnIndex).setCellValue("接口说明");for (int i = 0; i < records.size(); i++) {
row = sheet.createRow(i + 1);
Entity entity = records.get(i);
columnIndex = 0;
row.createCell(columnIndex).setCellValue(i + 1);
row.createCell(++columnIndex).setCellValue(entity.getCzybh());
row.createCell(++columnIndex).setCellValue(entity.getCjrbh());
row.createCell(++columnIndex).setCellValue(entity.getGnljdz());
row.createCell(++columnIndex).setCellValue(entity.getLjdzfjcs());
row.createCell(++columnIndex).setCellValue(entity.getCzkssj());
row.createCell(++columnIndex).setCellValue(entity.getCzjssj());
row.createCell(++columnIndex).setCellValue(entity.getCzjgdm().equals("0") ? "成功" : "失败");
row.createCell(++columnIndex).setCellValue(entity.getJgyyms().equals("0") ? "无" : entity.getJgyyms());
row.createCell(++columnIndex).setCellValue(entity.getCzyip());
row.createCell(++columnIndex).setCellValue(entity.getJksm());
    }
    sheet.setDefaultRowHeight((short) (16.5 * 20));//列宽自适应for (int i = 0; i <= 11; i++) {
sheet.autoSizeColumn(i);
    }
    String title= "czjl_all";
    response.setHeader("Content-disposition", "attachment;fileName=" + title + ".xls");
    response.setContentType("application/octet-stream;charset=utf-8");
    OutputStream ouputStream = response.getOutputStream();
    wb.write(ouputStream);
    ouputStream.flush();
    ouputStream.close();
} catch (IOException e) {
    e.printStackTrace();
}
}
  • 这里我们用swagger调用一下接口,可以看到还要点击Download file才可以下载文件

cb3b9fded908223b63c6977287300111.png

  • vue

/**
* 导出全部记录
* @param vueCtx
* @param param
* @param callback
*/
export function exportExcelAll(vueCtx,param,callback) {
vueCtx.sendWorkOrder({
url : "/czjl/exportExcelAll",
data: param,
procgress: true,
back: callback,
responseType: 'blob' // 表明返回服务器返回的数据类型,要加在请求头里面,否则识别不了文件流
});
}

"primary" size="mini" @click="exportExcelAll">导出全部

exportExcelAll(){
Czjl.exportExcelAll(this,{},resp=>{
console.log(resp);
});
},
  • 用前端调接口则发现数据传输过来并没有弹出文件框下载

8b9c5ccd446b33084c5ab35e5704437b.png

  • 将文件流转成blob形式,创建一个超链接,将文件流赋进去,然后实现这个超链接的单击事件

/**
* 导出全部按钮点击事件
*/
exportExcelAll(){
Czjl.exportExcelAll(this,{},resp=>{
this.downloadFile(resp);
});
},
/**
* 文件导出
*/
downloadFile(data) {
if (!data) {
return
}
const link = document.createElement('a');
let blob = new Blob([data], {type: 'application/vnd.ms-excel'});
link.style.display = 'none';
link.href = URL.createObjectURL(blob);

link.setAttribute('download', '记录信息' + '.xls');
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
},

4714199cd549e5103bbdfd5602284613.png14c99e4fb6a4064b0ef42fb4fb5111d3.png

.end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值