springBoot+Vue导出Excel

springBoot+Vue导出Excel


后端下载依赖包:

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>4.1.2</version>
</dependency>

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>4.1.2</version>
</dependency>

后端代码:

 @RequestMapping("/testExprotExcelx")
 public String  testExprotExcelx(){
    
    
        try {
    
    
            //创建一个数组用于设置表头
            List<String> header =new ArrayList<>();
            header.add("姓名");
            header.add("年龄");
            header.add("性别");
            //数据
            List<Map<String,String>> list=new ArrayList<>();
            Map<String ,String> map=new HashMap<>();
            map.put("姓名","张三");
            map.put("年龄","12");
            map.put("性别","男");
            list.add(map);
            Map<String ,String> map1=new HashMap<>();
            map1.put("姓名","李四");
            map1.put("年龄","13");
            map1.put("性别","男");
            list.add(map1);
            Map<String ,String> map2=new HashMap<>();
            map2.put("姓名","赵梅");
            map2.put("年龄","12");
            map2.put("性别","女");
            list.add(map2);

            //声明一个工作簿
            XSSFWorkbook workbook = new XSSFWorkbook();
            //生成一个表格,设置表格名称为"每日库存情况表"
            XSSFSheet sheet = workbook.createSheet("每日库存情况表");
            //设置表格列宽度为10个字节
            sheet.setDefaultColumnWidth(10);
            //创建第一行表头
            XSSFRow headrow = sheet.createRow(0);
            //遍历添加表头
            for (int i = 0; i < header.size(); i++) {
    
    
                //创建一个单元格
                XSSFCell cell = headrow.createCell(i);
                //创建一个内容对象
                XSSFRichTextString text = new XSSFRichTextString(header.get(i));
                //将内容对象的文字内容写入到单元格中
                cell.setCellValue(text);
            }
            //模拟遍历结果集,把内容加入表格
            for (int i = 0; i < list.size(); i++) {
    
    
                XSSFRow row1 = sheet.createRow(i+1);
                for (int j=0;j<header.size();j++){
    
    
                    XSSFCell cell = row1.createCell(j);
                    XSSFRichTextString text = new XSSFRichTextString(list.get(i).get(header.get(j))+"");
                    cell.setCellValue(text);
                }
            }
            //准备将Excel的输出流通过response输出到页面下载
            response.setContentType("application/octet-stream");
            response.setHeader("Content-disposition", "attachment;filename=student.xlsx");
            //刷新缓冲
            response.flushBuffer();
            //workbook将Excel写入到response的输出流中,供页面下载
            workbook.write(response.getOutputStream());
            return "导出成功";
        } catch (Exception e) {
    
    
            e.printStackTrace();
            return "导出失败";
        }

    }

前端代码:

outExcel(){
    
    
     const loading = this.$loading({
    
    
       lock: true,
       text: '努力加载中',
       spinner: 'el-icon-loading',
       background: 'rgba(0, 0, 0, 0.7)'
     });
     this.$axios({
    
    
       method: 'get',
       url: '/api/inventory/testExprotExcelx',
       responseType: 'blob'
     }).then((res) => {
    
    
       const content = res.data
       const blob = new Blob([content])//构造一个blob对象来处理数据
       const fileName = 'test.xlsx'
       //对于<a>标签,只有 Firefox 和 Chrome(内核) 支持 download 属性
       //IE10以上支持blob但是依然不支持download
       if ('download' in document.createElement('a')) {
    
     //支持a标签download的浏览器
         const link = document.createElement('a')//创建a标签
         link.download = fileName//a标签添加属性
         link.style.display = 'none'
         link.href = URL.createObjectURL(blob)
         document.body.appendChild(link)
         link.click()//执行下载
         URL.revokeObjectURL(link.href) //释放url
         document.body.removeChild(link)//释放标签
       } else {
    
     //其他浏览器
         navigator.msSaveBlob(blob, fileName)
       }
       loading.close()
     }, (error) => {
    
    
       this.$message.error('请求异常');
       loading.close()
     })
},

猜你喜欢

转载自blog.csdn.net/zhouxianglian1/article/details/109096876