<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-spring-boot-starter</artifactId>
<version>4.3.0</version>
</dependency>
一、
- vue使用window.open get请求一个
exportData() {
window.open(’/employee/basic/export’, ‘_parent’);
},
- spring boot 接受请求 查找数据库对应信息 生成对应的excle
@ApiOperation(value = "导出员工数据",produces = "application/octet-stream")
@GetMapping("/export")
public void exportEmployee(HttpServletResponse response){
List<Employee> list = employeeService.getEmployee(null);
//ExcelType.HSSF 2003 office | XSSF 2007
ExportParams params = new ExportParams("员工表", "员工表", ExcelType.HSSF);
Workbook workbook = ExcelExportUtil.exportExcel(params, Employee.class, list);
ServletOutputStream outputStream=null;
try {
//流形式
response.setHeader("content-type","application/octet-stream");
//防止中文乱码
response.setHeader("content-disposition","attachment;filename="+
URLEncoder.encode("员工表.xls","UTF-8"));
outputStream = response.getOutputStream();
workbook.write(outputStream);
} catch (IOException e) {
e.printStackTrace();
}finally {
if(null!=outputStream){
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
- 在java实体类上添加
@Excel(name = "列名")
- 调用vue接口即可实现对数据的导出
二、 - 前端使用ES6 语法 对 response 进行操作,这样就可以使用post请求,对@RequestBody进行传参
(使用普通的ajax请求不会进行下载操作,ajax的返回值类型是json,text,html,xml类型,或者可以说ajax的接收类型只能是string字符串,不是流类型,所以无法实现文件下载。)这边使用 new Promise((resolve, reject)进行
exportData(){
return new Promise((resolve, reject) => {
this.$axios.post('/api/common/export',this.params,{
responseType: "blob" // 表明返回服务器返回的数据类型
}).then(
(response) => {
resolve(response);
let fileName = this.params.impType + Date.parse(new Date()) + ".xls";
if (window.navigator.msSaveOrOpenBlob) {
navigator.msSaveBlob(response.data, fileName);
} else {
let link = document.createElement("a");
link.href = window.URL.createObjectURL(response.data);
link.download = fileName;
link.click();
// 释放内存
window.URL.revokeObjectURL(link.href);
}
},
(err) => {
reject(err);
}
);
});
}
- 后端使用post进行接收 这边使用object接受所有数据 并使用JSON解析对数据进行加工 相应给前端
@ApiOperation(value = "导出xls数据",produces = "application/octet-stream")
@PostMapping("/export")
public void exportEmployee(HttpServletResponse response, @RequestBody Object entity) throws InvocationTargetException, IllegalAccessException {
/**
* 将传入的object转成 json格式字符串
*/
String jsonObject = JSON.toJSON(entity).toString();
/**
* 获取 jsonObject 在TafTmpType里面存在的数据
*/
TagImpType type = JSON.parseObject(jsonObject,TagImpType.class);
/**
* 判断传入的是那个实体类的数据 获取并转换到对应的workbook
*/
Workbook workbook=null;
switch (type.getImpType()){
case "1":
FmEnterpriseInfo fmEnterpriseInfo=JSON.parseObject(jsonObject, FmEnterpriseInfo.class);
workbook = ExcelExportUtil.exportExcel(new ExportParams(), FmEnterpriseInfo.class,
fmEnterpriseInfoService.listByPage(fmEnterpriseInfo));
break;
case "2":
FmProjectInfo fmProjectInfo=JSON.parseObject(jsonObject,FmProjectInfo.class);
workbook = ExcelExportUtil.exportExcel(new ExportParams(), FmProjectInfo.class,
fmProjectInfoService.export(fmProjectInfo));
break;
case "3":
FmAuthenticationInfo fmAuthenticationInfo=JSON.parseObject(jsonObject, FmAuthenticationInfo.class);
workbook = ExcelExportUtil.exportExcel(new ExportParams(), FmAuthenticationInfo.class,
fmAuthenticationInfoService.listByPage(fmAuthenticationInfo,type.getCitycode(),type.getCountycode()));
break;
case "4":
FmUsuallyInfo fmUsuallyInfo=JSON.parseObject(jsonObject, FmUsuallyInfo.class);
workbook = ExcelExportUtil.exportExcel(new ExportParams(), FmUsuallyInfo.class,
fmUsuallyInfoService.lists(fmUsuallyInfo,type.getCitycode(),type.getCountycode(),type.getYear(),type.getQuerytype()));
break;
case "5":
FmServiceInfo fmServiceInfo=JSON.parseObject(jsonObject, FmServiceInfo.class);
workbook = ExcelExportUtil.exportExcel(new ExportParams(), FmServiceInfo.class,
fmServiceInfoService.lists(fmServiceInfo));
break;
case "6":
FmRefusecInfo fmRefusecInfo=JSON.parseObject(jsonObject, FmRefusecInfo.class);
workbook = ExcelExportUtil.exportExcel(new ExportParams(), FmRefusecInfo.class,
fmRefusecinfoService.lists(fmRefusecInfo));
break;
case "7":
FmTaxInfo fmTaxInfo=JSON.parseObject(jsonObject, FmTaxInfo.class);
workbook = ExcelExportUtil.exportExcel(new ExportParams(), FmTaxInfo.class,
fmTaxInfoService.lists(fmTaxInfo,type.getCitycode(),type.getCountycode()));
break;
default:
return;
}
/**
* 将数据存入 response 返还给前端
*/
ServletOutputStream outputStream=null;
try {
response.setHeader("content-type","application/octet-stream");
response.setHeader("content-disposition","attachment;filename="+
URLEncoder.encode("1.xls","UTF-8"));
outputStream = response.getOutputStream();
workbook.write(outputStream);
} catch (IOException e) {
e.printStackTrace();
}finally {
if(null!=outputStream){
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
前端发起请求(下周试试这个方法)
var iframe = document.createElement("iframe");
iframe.style.display = 'none';
iframe.src = "/emp/basic/exportEmp";
iframe.onload=function () {
document.body.removeChild(iframe);
}
document.body.appendChild(iframe);