springboot+vue实现excel的导出
创始人
2024-01-28 00:33:55

首先是springboot对数据的处理

依赖的导入

 org.apache.poipoi4.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);})}

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

excel文件的导出

在这里插入图片描述

Vue前端处理后端返回的文件流乱码,导出Excel

二:接口(注意:一定要加上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(“导出成功”);
});

相关内容

热门资讯

万的繁体字怎么写 繁体字怎么写... 一、繁体字大全简化偏旁讠[訁] 饣[飠] [昜] 纟[糹] [臤] 只[戠] 钅[釒] 呙[咼]A爱...
埃菲尔铁塔在哪 中国仿建埃菲尔... 2019年4月26日,广西南宁市,街头惊现一座巨型山寨版埃菲尔铁塔,高约20米,白色塔身,造型逼真,...
苗族的传统节日 贵州苗族节日有... 【岜沙苗族芦笙节】岜沙,苗语叫“分送”,距从江县城7.5公里,是世界上最崇拜树木并以树为神的枪手部落...
北京的名胜古迹 北京最著名的景... 北京从元代开始,逐渐走上帝国首都的道路,先是成为大辽朝五大首都之一的南京城,随着金灭辽,金代从海陵王...
长白山自助游攻略 吉林长白山游... 昨天介绍了西坡的景点详细请看链接:一个人的旅行,据说能看到长白山天池全凭运气,您的运气如何?今日介绍...
应用未安装解决办法 平板应用未... ---IT小技术,每天Get一个小技能!一、前言描述苹果IPad2居然不能安装怎么办?与此IPad不...
脚上的穴位图 脚面经络图对应的... 人体穴位作用图解大全更清晰直观的标注了各个人体穴位的作用,包括头部穴位图、胸部穴位图、背部穴位图、胳...
猫咪吃了塑料袋怎么办 猫咪误食... 你知道吗?塑料袋放久了会长猫哦!要说猫咪对塑料袋的喜爱程度完完全全可以媲美纸箱家里只要一有塑料袋的响...
世界上最漂亮的人 世界上最漂亮... 此前在某网上,选出了全球265万颜值姣好的女性。从这些数量庞大的女性群体中,人们投票选出了心目中最美...
埃菲尔铁塔在哪 中国仿建埃菲尔... 2019年4月26日,广西南宁市,街头惊现一座巨型山寨版埃菲尔铁塔,高约20米,白色塔身,造型逼真,...
苗族的传统节日 贵州苗族节日有... 【岜沙苗族芦笙节】岜沙,苗语叫“分送”,距从江县城7.5公里,是世界上最崇拜树木并以树为神的枪手部落...
桂林市属于哪个省 桂林为什么划... 桂林 [guì lín]广西壮族自治区下辖市桂林,简称桂,是世界著名风景游览城市、全国重要高新技术产...
北京的名胜古迹 北京最著名的景... 北京从元代开始,逐渐走上帝国首都的道路,先是成为大辽朝五大首都之一的南京城,随着金灭辽,金代从海陵王...