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