直播平台搭建源码,使用EasyExcel实现导入导出功能

发布时间 2023-04-27 14:19:37作者: 云豹科技-苏凌霄

直播平台搭建源码,使用EasyExcel实现导入导出功能

使用,添加依赖

 


<dependencies>
 <!-- https://mvnrepository.com/artifact/com.alibaba/easyexcel -->
 <dependency>
 <groupId>com.alibaba</groupId>
 <artifactId>easyexcel</artifactId>
 <version>2.1.1</version>
 </dependency>
</dependencies>

编写service层方法

 


//导出
    @Override
    public void exportData(HttpServletResponse response) {
        try {
            //设置文件类型
            response.setContentType("application/vnd.ms-excel");
            //防止内容中有乱码
            response.setCharacterEncoding("utf-8");
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode("数据字典", "UTF-8");
            //设置响应头
            response.setHeader("Content-disposition", "attachment;filename="+ fileName+".xlsx");
            //查询数据库得到数据
            List<Dict> dictList = baseMapper.selectList(null);
            //因为自己封装了返回类,与实体类字段不匹配
            List<DictEeVo> dictVoList = new ArrayList<>(dictList.size());
            for(Dict dict : dictList) {
                //dict对象 转成vo对象
                DictEeVo dictVo = new DictEeVo();
                BeanUtils.copyProperties(dict,dictVo);
                dictVoList.add(dictVo);
            }
            EasyExcel.write(response.getOutputStream(), DictEeVo.class).sheet("数据字典").doWrite(dictVoList);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

 

控制层

 


@ApiOperation(value="导出")
@GetMapping(value = "/exportData")
public void exportData(HttpServletResponse response) {
 dictService.exportData(response);
}

 

vue页面添加按钮

 


<div class="el-toolbar">
 <div class="el-toolbar-body" style="justify-content: flex-start;">
 <el-button type="text" @click="exportData"><i class="fa fa-plus"/> 导出</el-b
 </div>
</div>

 

添加方法

 


methods: {
     //导出
     exportData(){
         window.open("http://localhost:8202/admin/cmn/dict/exportData")
     },

 

 以上就是 直播平台搭建源码,使用EasyExcel实现导入导出功能,更多内容欢迎关注之后的文章