controller类
@PostMapping("/importData") public Result importData(MultipartFile file){ dictService.importDictData(file); return Result.ok(); }
service
package com.atguigu.yygh.cmn.service; import com.atguigu.yygh.model.cmn.Dict; import com.baomidou.mybatisplus.extension.service.IService; import org.springframework.web.multipart.MultipartFile; public interface DictService extends IService<Dict> { void importDictData(MultipartFile file); }
service实现类
package com.atguigu.yygh.cmn.service.impl; import com.alibaba.excel.EasyExcel; import com.atguigu.yygh.cmn.listener.DictListener; import com.atguigu.yygh.cmn.mapper.DictMapper; import com.atguigu.yygh.cmn.service.DictService; import com.atguigu.yygh.model.cmn.Dict; import com.atguigu.yygh.vo.cmn.DictEeVo; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; import java.io.IOException; @Service public class DictServiceImpl extends ServiceImpl<DictMapper, Dict> implements DictService { @Autowired private DictMapper dictMapper; @Override public void importDictData(MultipartFile file) { try { EasyExcel.read(file.getInputStream(), DictEeVo.class,new DictListener(dictMapper)) .sheet().doRead(); } catch (IOException e) { throw new RuntimeException(e); } } }
监听器
package com.atguigu.yygh.cmn.listener; import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.event.AnalysisEventListener; import com.atguigu.yygh.cmn.mapper.DictMapper; import com.atguigu.yygh.model.cmn.Dict; import com.atguigu.yygh.vo.cmn.DictEeVo; import org.springframework.beans.BeanUtils; public class DictListener extends AnalysisEventListener<DictEeVo> { private DictMapper dictMapper; //使用构造器注入dictMapper public DictListener(DictMapper dictMapper) { this.dictMapper = dictMapper; } //一行一行的读取,从第2行开始 @Override public void invoke(DictEeVo dictEeVo, AnalysisContext analysisContext) { Dict dict = new Dict(); BeanUtils.copyProperties(dictEeVo,dict); dictMapper.insert(dict); } @Override public void doAfterAllAnalysed(AnalysisContext analysisContext) { } }
前端
<el-button type="text" @click="importData"><i class="fa fa-plus"/> 导入</el-button> <el-dialog title="导入" :visible.sync="dialogImportVisible" width="480px"> <el-form label-position="right" label-width="170px"> <el-form-item label="文件"> <el-upload :multiple="false" <!--成功后回调的方法--> :on-success="onUploadSuccess" :action="'http://localhost:8202/admin/cmn/dict/importData'" class="upload-demo"> <el-button size="small" type="primary">点击上传</el-button> <div slot="tip" class="el-upload__tip">只能上传excel文件,且不超过500kb</div> </el-upload> </el-form-item> </el-form> <div slot="footer" class="dialog-footer"> <el-button @click="dialogImportVisible = false"> 取消 </el-button> </div> </el-dialog>
标签:文件,cmn,EasyExcel,导入,atguigu,import,com,dictMapper,yygh From: https://www.cnblogs.com/ixtao/p/17389683.html