這篇文章主要講解了“SpringBoot怎么集成EasyExcel應(yīng)用”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“SpringBoot怎么集成EasyExcel應(yīng)用”吧!
目前成都創(chuàng)新互聯(lián)公司已為數(shù)千家的企業(yè)提供了網(wǎng)站建設(shè)、域名、虛擬空間、網(wǎng)站托管維護(hù)、企業(yè)網(wǎng)站設(shè)計(jì)、丁青網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長(zhǎng),共同發(fā)展。
特點(diǎn):
1、Java領(lǐng)域解析、生成Excel比較有名的框架有Apache poi、jxl等。但他們都存在一個(gè)嚴(yán)重的問題就是 非常的耗內(nèi)存。如果你的系統(tǒng)并發(fā)量不大的話可能還行,但是一旦并發(fā)上來后一定會(huì)OOM或 者JVM頻繁的full gc。
2、EasyExcel是阿里巴巴開源的一個(gè)excel處理框架,以使用簡(jiǎn)單、節(jié)省內(nèi)存著稱。EasyExcel能大大減 少占用內(nèi)存的主要原因是在解析Excel時(shí)沒有將文件數(shù)據(jù)一次性全部加載到內(nèi)存中,而是從磁盤上一 行行讀取數(shù)據(jù),逐個(gè)解析。
3、EasyExcel采用一行一行的解析模式,并將一行的解析結(jié)果以觀察者的模式通知處理 (AnalysisEventListener)。
1、數(shù)據(jù)導(dǎo)入:減輕錄入工作量
2、數(shù)據(jù)導(dǎo)出:統(tǒng)計(jì)信息歸檔
3、數(shù)據(jù)傳輸:異構(gòu)系統(tǒng)之間數(shù)據(jù)傳輸
sql
CREATE TABLE `edu_subject` ( `id` char(19) NOT NULL COMMENT "課程類別ID", `title` varchar(10) NOT NULL COMMENT "類別名稱", `parent_id` char(19) NOT NULL DEFAULT "0" COMMENT "父ID", `sort` int unsigned NOT NULL DEFAULT "0" COMMENT "排序字段", `gmt_create` datetime NOT NULL COMMENT "創(chuàng)建時(shí)間", `gmt_modified` datetime NOT NULL COMMENT "更新時(shí)間", PRIMARY KEY (`id`), KEY `idx_parent_id` (`parent_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci ROW_FORMAT=COMPACT COMMENT="課程科目";
轉(zhuǎn)成->
溫馨提示:以下版本不能更換,換了可能會(huì)不行
com.alibaba easyexcel 2.1.1 org.apache.poi poi 3.17 org.apache.poi poi-ooxml 3.17
package com.zhz.serviceedu.controller; import com.zhz.common.utils.R; import com.zhz.serviceedu.service.EduSubjectService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; /** ** 課程科目 前端控制器 *
* * @author zhz * @since 2021-07-03 */ @RestController @RequestMapping("/eduservice/subject") @CrossOrigin @Api(tags = "課程科目") public class EduSubjectController { @Autowired private EduSubjectService eduSubjectService; /** * 添加課程分類,獲取上傳過來的文件,把文件內(nèi)容讀取出來 */ @PostMapping("/addSubject") @ApiOperation(value = "添加課程分類,獲取上傳過來的文件,把文件內(nèi)容讀取出來") public R addSubject(MultipartFile file){ //上傳過來的excel文件 eduSubjectService.saveSubject(file,eduSubjectService); return R.ok(); } }
package com.zhz.serviceedu.service; import com.zhz.serviceedu.entity.EduSubject; import com.baomidou.mybatisplus.extension.service.IService; import org.springframework.web.multipart.MultipartFile; /** ** 課程科目 服務(wù)類 *
* * @author zhz * @since 2021-07-03 */ public interface EduSubjectService extends IService{ /** * 添加課程信息 * * @author zhz * @date 2021/07/02 02:18 * @param file 文件對(duì)象,用于獲取excel文件 * @param eduSubjectService 方便監(jiān)聽器部分引用 */ void saveSubject(MultipartFile file, EduSubjectService eduSubjectService); }
package com.zhz.serviceedu.service.impl; import com.alibaba.excel.EasyExcel; import com.zhz.serviceedu.entity.EduSubject; import com.zhz.serviceedu.entity.excel.SubjectData; import com.zhz.serviceedu.listener.SubjectExcelListener; import com.zhz.serviceedu.mapper.EduSubjectMapper; import com.zhz.serviceedu.service.EduSubjectService; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; import java.io.InputStream; /** ** 課程科目 服務(wù)實(shí)現(xiàn)類 *
* * @author zhz * @since 2021-07-03 */ @Service public class EduSubjectServiceImpl extends ServiceImplimplements EduSubjectService { /** * 添加課程信息 * * @param file 文件對(duì)象,用于獲取excel文件 * @param eduSubjectService * @author zhz * @date 2021/07/02 02:18 */ @Override public void saveSubject(MultipartFile file, EduSubjectService eduSubjectService) { try { //文件輸入流 InputStream in = file.getInputStream(); //調(diào)用方法進(jìn)行讀取 EasyExcel.read(in, SubjectData.class,new SubjectExcelListener(eduSubjectService)).sheet().doRead(); }catch (Exception e){ e.printStackTrace(); } } }
package com.zhz.serviceedu.listener; import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.event.AnalysisEventListener; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.zhz.servicebase.execptionhandler.GuliException; import com.zhz.serviceedu.entity.EduSubject; import com.zhz.serviceedu.entity.excel.SubjectData; import com.zhz.serviceedu.service.EduSubjectService; import lombok.extern.slf4j.Slf4j; import org.springframework.util.StringUtils; /** * @author zhouhengzhe * @Description: excel監(jiān)聽器 * @date 2021/7/3上午2:28 */ @Slf4j public class SubjectExcelListener extends AnalysisEventListener{ /** * 創(chuàng)建有參數(shù)構(gòu)造,傳遞subjectService用于操作數(shù)據(jù)庫 * 因?yàn)镾ubjectExcelListener不能交給spring去管理,所以需要自己new,不能注入對(duì)象 * 此處一定要是public,不然永遠(yuǎn)獲取不到對(duì)象 */ public EduSubjectService eduSubjectService; public SubjectExcelListener() { } public SubjectExcelListener(EduSubjectService eduSubjectService) { this.eduSubjectService = eduSubjectService; } /** * 讀取excel內(nèi)容,一行一行進(jìn)行讀取,此處全是業(yè)務(wù)處理 * @param subjectData * @param analysisContext */ @Override public void invoke(SubjectData subjectData, AnalysisContext analysisContext) { log.info("進(jìn)入方法調(diào)用"); if (StringUtils.isEmpty(subjectData)){ throw new GuliException(20001,"文件數(shù)據(jù)為空"); } //一行一行去讀取excel內(nèi)容,每次讀取有兩個(gè)值,第一個(gè)值為一級(jí)分類,第二個(gè)值為二級(jí)分類 //判斷一級(jí)分類是否重復(fù) EduSubject existOneSubject = this.existOneSubject(eduSubjectService, subjectData.getOneSubjectName()); if (StringUtils.isEmpty(existOneSubject)){ existOneSubject=new EduSubject(); existOneSubject.setParentId("0"); //一級(jí)分類名稱 existOneSubject.setTitle(subjectData.getOneSubjectName()); eduSubjectService.save(existOneSubject); } //獲取一級(jí)分類的pid值 String pid=existOneSubject.getId(); //添加二級(jí)分類 // 判斷二級(jí)分類是否重復(fù) EduSubject existTwoSubject = this.existTwoSubject(eduSubjectService, subjectData.getTwoSubjectName(), pid); if (StringUtils.isEmpty(existTwoSubject)){ existTwoSubject=new EduSubject(); existTwoSubject.setParentId(pid); //二級(jí)分類名稱 existTwoSubject.setTitle(subjectData.getTwoSubjectName()); eduSubjectService.save(existTwoSubject); } } /** * 讀取完成后執(zhí)行 */ @Override public void doAfterAllAnalysed(AnalysisContext analysisContext) { } /** * 判斷一級(jí)分類是否重復(fù) * 因?yàn)檎n程的的parent_id為0時(shí),代表是一級(jí)分類,并且一級(jí)分類數(shù)據(jù)不重復(fù) * @param eduSubjectService * @param name * @return */ private EduSubject existOneSubject(EduSubjectService eduSubjectService,String name){ QueryWrapper wrapper=new QueryWrapper<>(); wrapper.eq("title",name); wrapper.eq("parent_id","0"); EduSubject subject = eduSubjectService.getOne(wrapper); return subject; } /** * 判斷二級(jí)分類是否重復(fù) * * @param eduSubjectService * @param name * @param pid * @return */ private EduSubject existTwoSubject(EduSubjectService eduSubjectService,String name,String pid){ QueryWrapper wrapper=new QueryWrapper<>(); wrapper.eq("title",name); wrapper.eq("parent_id",pid); EduSubject eduSubject = eduSubjectService.getOne(wrapper); return eduSubject; } }
因?yàn)閙ybatisplus生成的實(shí)體類的主鍵生成策略是IdType.ID_WORKER,所以需要修改成IdType.ID_WORKER_STR,否則會(huì)有轉(zhuǎn)換問題
package com.zhz.serviceedu.entity; import com.baomidou.mybatisplus.annotation.FieldFill; import com.baomidou.mybatisplus.annotation.IdType; import java.util.Date; import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableId; import java.io.Serializable; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.Data; import lombok.EqualsAndHashCode; import lombok.experimental.Accessors; /** ** 課程科目 *
* * @author zhz * @since 2021-07-03 */ @Data @EqualsAndHashCode(callSuper = false) @Accessors(chain = true) @ApiModel(value="EduSubject對(duì)象", description="課程科目") public class EduSubject implements Serializable { private static final long serialVersionUID = 1L; @ApiModelProperty(value = "課程類別ID") @TableId(value = "id", type = IdType.ID_WORKER_STR) private String id; @ApiModelProperty(value = "類別名稱") private String title; @ApiModelProperty(value = "父ID") private String parentId; @ApiModelProperty(value = "排序字段") private Integer sort; @ApiModelProperty(value = "創(chuàng)建時(shí)間") @TableField(fill = FieldFill.INSERT) private Date gmtCreate; @ApiModelProperty(value = "更新時(shí)間") @TableField(fill = FieldFill.INSERT_UPDATE) private Date gmtModified; }
感謝各位的閱讀,以上就是“SpringBoot怎么集成EasyExcel應(yīng)用”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對(duì)SpringBoot怎么集成EasyExcel應(yīng)用這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!