首页 > 其他分享 >SpringBoot+Vue+EasyExcel实现excel简单导入导出

SpringBoot+Vue+EasyExcel实现excel简单导入导出

时间:2023-03-19 21:23:38浏览次数:33  
标签:arr Vue SpringBoot exportDictTypeExcel 导出 EasyExcel excel blob response

1. 先导入EasyExcel依赖

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>easyexcel</artifactId>
            <version>3.1.3</version>
        </dependency>

2. 给实体类上注解,标注导出的excel的表头属性,以及标注无需导出的实体属性

@ExcelProperty("表头单元格名称")    // 标注在需要导出的实体属性上

@ExcelIgnore                       // 标注在无需导出的实体属性上

3. 写后端excel导出通用方法

/**
     * 导出excel
     * @param list 需要导出的数据
     */
    public static <T> void exportExcel(List<T> list, Class<T> clazz) {
        ServletOutputStream outputStream;
        try {
            HttpServletResponse response = ServletUtils.getResponse();
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setCharacterEncoding("utf-8");
            outputStream = response.getOutputStream();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        // easyExcel导出的核心代码
        EasyExcel.write(outputStream)
                .head(clazz)
                .excelType(ExcelTypeEnum.XLSX)
                .sheet()
                .doWrite(list);
    }

4. 控制器简单编写示例

/**
     * 导出字典类型excel
     */
    @LogAnnotation(title = "导出字典表格", operateType = OperateType.EXPORT)
    @ApiOperation("导出字典类型excel表格")
    @PostMapping(value = "/exportDictTypeExcel")
    public void exportDictTypeExcel(@RequestBody List<Long> dictTypeIdList){
        List<DictTypeEntity> dictTypeEntityList = dictTypeDao.selectBatchIds(dictTypeIdList);
        ExcelUtils.exportExcel(dictTypeEntityList, DictTypeEntity.class);
    }

5. 前端vue的请求方法

js请求方法代码,需要加上responseType:'blob'
export function exportDictTypeExcel(data) {
    return request({
        url: '/system/dicttype/exportDictTypeExcel',
        method: 'post',
        data,
        responseType:'blob'
    })
}

        vue请求方法代码
        exportDict() {
            var arr = [];
            this.multipleSelection.forEach((row) => arr.push(row.dictId));
            if (arr.length === 0) {
                this.$message({
                    message: "请选择要导出的数据。",
                    type: "error",
                });
            } else {
                this.$confirm("确定导出吗", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "success",
                    callback: (action) => {
                        if (action === "confirm") {
                            //批量导出
                            exportDictTypeExcel(arr).then((res) => {
                            });
                        }
                    },
                });
            }
        },

6. 如果写完以上代码,前后端都能跑通,并且已经有响应的二进制编码,浏览器就是不弹出excel文件下载的弹框,找到你vue的request.js文件,看看请求是否被拦截,
如果被拦截,加上以下代码,即可实现excel导出文件下载的弹框

const contentType = response.headers['content-type'];
    if (contentType == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8") {
      let blob = new Blob([response.data], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
      let url = window.URL.createObjectURL(blob);
      const link = document.createElement('a'); // 创建a标签
      link.href = url;
      // link.download = 'test.xlsx';
      link.click();
      URL.revokeObjectURL(url); // 释放内存
      return
    }

标签:arr,Vue,SpringBoot,exportDictTypeExcel,导出,EasyExcel,excel,blob,response
From: https://www.cnblogs.com/18sui/p/17234333.html

相关文章

  • day07-SpringBoot接收参数相关注解
    SpringBoot接收参数相关注解1.基本介绍SpringBoot接收客户端提交数据/参数会使用到相关注解详解@PathVariable、@RequestHeader、@ModelAttribute、@RequestParam、@Co......
  • 谈谈 Vue shallowRef 和 shallowReactive
    深层次响应式reactive和ref创建的对象都是深层次的,对象的根属性和嵌套属性都是响应式的。深层次转换是递归地转为响应式,对象里的每个属性访问都将触发代理的依赖追踪,......
  • Vuex模块式开发
    背景:2个组件:home和search,将store仓库拆分成2个小仓库,home和search两个小仓库用于管理自己模块的数据store文件夹下新建2个文件夹:home和searchhome下index.js//home模块......
  • Vue和React项目中生成唯一ID
    Vue中唯一ID生成方式React中唯一ID生成方式......
  • SpringBoot集成Swagger错误总结
    错误展示rorstartingApplicationContext.Todisplaytheconditionsreportre-runyourapplicationwith'debug'enabled.2023-03-1915:37:55.307ERROR12980---......
  • vue中全局过滤器
    //全局的过滤器,进行时间的格式化Vue.filter('dateFormat',function(dateStr){//根据给定的实际那字符串,得到绑定的时间vardt=newDate(dateStr)//yyy--mm--ddvar......
  • vue+element-ui刷新路由的时候保持在当前页面小技巧
    前言:很多小伙伴在练习vue项目的时候会遇到这样一个问题,就是刷新页面的时候,路由没有显示到当前页面,而是重定向回首页了,那么该怎么解决呢,就请各位小伙伴看下面的内容介绍吧......
  • Vue 搜索案例:gitHub 用户搜索案例
    一:界面示例效果......
  • 第五天(SpringBoot基础第二篇)
    一、关于starterstater参赛人、发令员SpringBoot中的starter只是把我们某一模块,比如web开发时所需要的所有JAR包打包好给我们而已。不过它的厉害之处在于,能自动把......
  • 使用vue2+element-ui+axios实现后台管理系统的增删改查
    以下仅作为自己个人学习使用前言:需要后端的接口已经在另外一篇博客写了,需要的小伙伴们可以去那边参考,下面是链接https://www.cnblogs.com/Amyel/p/17233060.html正片......