首先是springboot对数据的处理
依赖的导入
org.apache.poi poi 4.1.2
@RequestMapping("/exportExcel")
public R exportResult(HttpServletResponse response) {XSSFWorkbook xssfSheets = kucunService.formGeneration();String fileName = "Goods报表.xlsx";OutputStream outputStream = null;try {fileName = URLEncoder.encode(fileName, "UTF-8");//设置ContentType请求信息格式response.setContentType("application/vnd.ms-excel");response.setHeader("Content-disposition", "attachment;filename=" + fileName);outputStream = response.getOutputStream();xssfSheets.write(outputStream);System.out.println(xssfSheets);outputStream.flush();outputStream.close();} catch (UnsupportedEncodingException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}return R.ok();
}
service层
public XSSFWorkbook formGeneration() {//查询数据库中的数据List listBlogVOS = kucunDao.getAllKuchunList();XSSFWorkbook xssfSheets = new XSSFWorkbook();XSSFSheet userList = xssfSheets.createSheet("库存药品表");Row titleRow = userList.createRow(0);//创建第一行,起始为0titleRow.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("是否盘点");for (int i = 0; i < listBlogVOS.size(); i++) {Row row = userList.createRow(i + 1);//设置对哪一行操作row.createCell(0).setCellValue(listBlogVOS.get(i).getId());row.createCell(1).setCellValue(listBlogVOS.get(i).getDrugcode());row.createCell(2).setCellValue(listBlogVOS.get(i).getDrugname());row.createCell(3).setCellValue(listBlogVOS.get(i).getStorename());row.createCell(4).setCellValue(listBlogVOS.get(i).getNumber());row.createCell(5).setCellValue(listBlogVOS.get(i).getPandianstock());if(listBlogVOS.get(i).getNormal()=="0"){row.createCell(6).setCellValue("库存正常");}else{row.createCell(6).setCellValue("库存不正常");}}return xssfSheets;}
dao层
List getAllKuchunList();
xml文件
前端excel对文件流进行对应的处理
let exportExcelParam=()=>{exportExcel().then(res=>{console.log(res)const link = document.createElement("a");const blob = new Blob([res], {type: "application/vnd.ms-excel;charset=utf-8",});link.style.display = "none";link.href = URL.createObjectURL(blob);link.setAttribute("download", `库存药品.xls`);document.body.appendChild(link);link.click();document.body.removeChild(link);})}




二:接口(注意:一定要加上responseType: ‘blob’)
// 导出
export function exportAllList (data) {
return request({
url: ‘/word/group/exportWords’,
method: ‘post’,
data: data,
responseType: ‘blob’
})
}
exportAllList(params).then((res) => {
//这里是重点
const link = document.createElement(“a”);
const blob = new Blob([res], {
type: “application/vnd.ms-excel;charset=utf-8”,
});
link.style.display = “none”;
link.href = URL.createObjectURL(blob);
link.setAttribute(“download”, 词库数据.xls);
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
this.msgSuccess(“导出成功”);
});