SpringBoot2.x使用EasyPOI導入Excel淺談

SpringBoot2.x使用EasyPOI導入Excel淺談

平時常常遇到客戶要幫忙導入一些數據到數據庫中,有些數據比較多有時候手動錄入就會很耗時間,因此就本身寫一個Excel導入的demo記錄一下我對EasyPOI的誤區;本文使用SpringBoot2.0,EasyPOIjava

開發框架

框架:SpringBoot2.0
java jdk 1.8
開發工具:Eclipse
數據庫:Orcal

一.首先在pom.xml中導入EasyPOI的架包

pom.xml的主要文件信息以下:git

<!-- easypoi --> <dependency> <groupId>cn.afterturn</groupId> <artifactId>easypoi-base</artifactId> <version>3.0.3</version> </dependency> <dependency> <groupId>cn.afterturn</groupId> <artifactId>easypoi-web</artifactId> <version>3.0.3</version> </dependency> <dependency> <groupId>cn.afterturn</groupId> <artifactId>easypoi-annotation</artifactId> <version>3.0.3</version> </dependency> 

二.而後編寫Controller

Controller:github

/** * @author lr * @date 20181226下午6:51:43 * @version V1.0.0 */ package com.louis.sql.tools.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; import com.louis.sql.tools.model.LongHuaAreaDO; import com.louis.sql.tools.result.HttpResult; import com.louis.sql.tools.service.LongHuaAreaService; @RestController @RequestMapping("longhua") public class LongHuaController { @Autowired private LongHuaAreaService longHuaAreaService; /** * @Title: listAllData * @Description: 查詢處全部的數據並展現 * @param @return 參數說明 * @return HttpResult 返回類型 * @throws */ @RequestMapping(value = "/list", method = RequestMethod.GET) public HttpResult listAllData(){ return HttpResult.ok(longHuaAreaService.listAll()); } @RequestMapping(value = "/deleteByName", method = RequestMethod.DELETE) public HttpResult deleteByName(@RequestBody List<LongHuaAreaDO> records) { return HttpResult.ok(longHuaAreaService.deleteByName(records)); } /** * @Title: importExcel * @Description: 導入Excel方法 * @param @param file 參數說明 * @return void 返回類型 * @throws */ @PostMapping("/importExcel") public void importExcel(@RequestParam("file") MultipartFile file) { String fileName = file.getOriginalFilename(); try { longHuaAreaService.batchImport(fileName, file); } catch (Exception e) { e.printStackTrace(); } } } 

三.接口LongHuaAreaService

/** * @author lr * @date 2018年12月26日 下午9:26:02 * @version V1.0.0 */ package com.louis.sql.tools.service; import java.io.IOException; import java.util.List; import org.springframework.web.multipart.MultipartFile; import com.louis.sql.tools.model.LongHuaAreaDO; public interface LongHuaAreaService { /** * @Title: listAll * @Description: 查詢全部的龍華區監管人員 * @param @return 參數說明 * @return String 返回類型 * @throws */ List<LongHuaAreaDO> listAll(); /** * @Title: deleteByName * @Description: TODO(這裏用一句話描述這個方法的做用) * @param @param records * @param @return 參數說明 * @return String 返回類型 * @throws */ int deleteByName(List<LongHuaAreaDO> records); /** * @Title: delete * @Description: TODO(這裏用一句話描述這個方法的做用) * @param @param record * @param @return 參數說明 * @return int 返回類型 * @throws */ int delete(LongHuaAreaDO record); /** * @Title: batchImport * @Description: 導入Excel中的數據 * @param @param fileName * @param @param file 參數說明 * @return void 返回類型 * @throws */ void batchImport(String fileName, MultipartFile file) throws IOException, Exception; } 

四.實現方法LongHuaAreaServiceImpl

/** * @author lr * @date 2018年12月26日 下午9:26:50 * @version V1.0.0 */ package com.louis.sql.tools.service.impl; import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.multipart.MultipartFile; import com.louis.sql.tools.dao.LongHuaAreaMapper; import com.louis.sql.tools.model.LongHuaAreaDO; import com.louis.sql.tools.service.LongHuaAreaService; import cn.afterturn.easypoi.excel.ExcelImportUtil; import cn.afterturn.easypoi.excel.entity.ImportParams; @Service public class LongHuaAreaServiceImpl implements LongHuaAreaService{ @Autowired private LongHuaAreaMapper longHuaAreaMapper; @Override public List<LongHuaAreaDO> listAll() { return longHuaAreaMapper.listAll(); } @Override public int delete(LongHuaAreaDO record) { return longHuaAreaMapper.deleteByName(record.getName()); } @Override public int deleteByName(List<LongHuaAreaDO> records) { for (LongHuaAreaDO record : records) { delete(record); } return 1; } /* * Title: batchImport *Description: * @param fileName * @param file * @see com.louis.sql.tools.service.LongHuaAreaService#batchImport(java.lang.String, org.springframework.web.multipart.MultipartFile) */ @Transactional(readOnly = false,rollbackFor = Exception.class) @Override public void batchImport(String fileName, MultipartFile file) throws IOException, Exception { ImportParams params = new ImportParams(); params.setTitleRows(0); params.setHeadRows(1); List<LongHuaAreaDO> list = ExcelImportUtil.importExcel(file.getInputStream(), LongHuaAreaDO.class, params); for (int i = 0; i <list.size() ; i++) { System.out.println(list.get(i)); System.out.println(list.get(i).getId()); } } } 

五.實體類方法LongHuaAreaDO

/** * @author lr * @date 2018年12月26日 下午6:32:48 * @version V1.0.0 */ package com.louis.sql.tools.model; import java.io.Serializable; import java.util.Date; import cn.afterturn.easypoi.excel.annotation.Excel; import cn.afterturn.easypoi.excel.annotation.ExcelTarget; @ExcelTarget("BZ_JCRY") public class LongHuaAreaDO implements Serializable{ private static final long serialVersionUID = -4277023292843262708L; @Excel(name = "ID", orderNum = "0") private String id; @Excel(name = "編號", orderNum = "1") private String cerNo; @Excel(name = "姓名", orderNum = "2") private String legalNo; @Excel(name = "姓名", orderNum = "3") private String name; @Excel(name = "性別", replace = {"男_0", "女_1"}, orderNum = "4") private String sex; @Excel(name = "手機號", orderNum = "5") private String mobTel; @Excel(name = "單位名稱", orderNum = "6") private String corpName; @Excel(name = "部門名稱", orderNum = "7") private String deptName; @Excel(name = "職位", orderNum = "8") private String post; @Excel(name = "專門", orderNum = "9") private String expertise; @Excel(name = "組織機構", orderNum = "10") private String regOrg; @Excel(name = "組織機構簡稱", orderNum = "11") private String bizOrg; @Excel(name = "更新時間", exportFormat = "yyyy-MM-dd HH:mm:ss", orderNum = "12") private Date updateTime; @Excel(name = "辦公室名稱", orderNum = "13") private String officeName; @Excel(name = "單位編碼", orderNum = "14") private String corpCode; @Excel(name = "部門編碼", orderNum = "15") private String deptCode; @Excel(name = "建立時間", exportFormat = "yyyy-MM-dd HH:mm:ss", orderNum = "16") private Date createTime; @Excel(name = "僱傭編號", orderNum = "17") private String employeeCode; @Excel(name = "是否在職", orderNum = "18") private String isextract; @Override public String toString() { return "LongHuaAreaDO [id=" + id + ", cerNo=" + cerNo + ", legalNo=" + legalNo + ", name=" + name + ", sex=" + sex + ", mobTel=" + mobTel + ", corpName=" + corpName + ", deptName=" + deptName + ", post=" + post + ", expertise=" + expertise + ", regOrg=" + regOrg + ", bizOrg=" + bizOrg + ", updateTime=" + updateTime + ", officeName=" + officeName + ", corpCode=" + corpCode + ", deptCode=" + deptCode + ", createTime=" + createTime + ", employeeCode=" + employeeCode + ", isextract=" + isextract + "]"; } public String getId() { return id; } public void setId(String id) { this.id = id == null ? null : id.trim(); } public String getCerNo() { return cerNo; } public void setCerNo(String cerNo) { this.cerNo = cerNo == null ? null : cerNo.trim(); } public String getLegalNo() { return legalNo; } public void setLegalNo(String legalNo) { this.legalNo = legalNo == null ? null : legalNo.trim(); } public String getName() { return name; } public void setName(String name) { this.name = name == null ? null : name.trim(); } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex == null ? null : sex.trim(); } public String getMobTel() { return mobTel; } public void setMobTel(String mobTel) { this.mobTel = mobTel == null ? null : mobTel.trim(); } public String getCorpName() { return corpName; } public void setCorpName(String corpName) { this.corpName = corpName == null ? null : corpName.trim(); } public String getDeptName() { return deptName; } public void setDeptName(String deptName) { this.deptName = deptName == null ? null : deptName.trim(); } public String getPost() { return post; } public void setPost(String post) { this.post = post == null ? null : post.trim(); } public String getExpertise() { return expertise; } public void setExpertise(String expertise) { this.expertise = expertise == null ? null : expertise.trim(); } public String getRegOrg() { return regOrg; } public void setRegOrg(String regOrg) { this.regOrg = regOrg == null ? null : regOrg.trim(); } public String getBizOrg() { return bizOrg; } public void setBizOrg(String bizOrg) { this.bizOrg = bizOrg == null ? null : bizOrg.trim(); } public Date getUpdateTime() { return updateTime; } public void setUpdateTime(Date updateTime) { this.updateTime = updateTime; } public String getOfficeName() { return officeName; } public void setOfficeName(String officeName) { this.officeName = officeName == null ? null : officeName.trim(); } public String getCorpCode() { return corpCode; } public void setCorpCode(String corpCode) { this.corpCode = corpCode == null ? null : corpCode.trim(); } public String getDeptCode() { return deptCode; } public void setDeptCode(String deptCode) { this.deptCode = deptCode == null ? null : deptCode.trim(); } public Date getCreateTime() { return createTime; } public void setCreateTime(Date createTime) { this.createTime = createTime; } public String getEmployeeCode() { return employeeCode; } public void setEmployeeCode(String employeeCode) { this.employeeCode = employeeCode == null ? null : employeeCode.trim(); } public String getIsextract() { return isextract; } public void setIsextract(String isextract) { this.isextract = isextract == null ? null : isextract.trim(); } } 

注意:必定要有toString構造方法web

六.要導入的Excel中的內容格式以下:

Excel內容格式

七.使用Swagger-ui的的接口方法進行導入

導入方法

八.導入後的效果以下

導入後臺打印效果

九.Demo下載

下載地址:https://github.com/PlayTaoist/SpringBoot2-FastDFSspring

目前只寫到從Excel中讀取到了數據,後續如何插入到數據庫中你們能夠本身完善一下sql

=======================================================================================shell

博客地址:https://www.codepeople.cn數據庫

=======================================================================================springboot

相關文章
相關標籤/搜索