java批量導(dǎo)入Excel數(shù)據(jù)超詳細(xì)實(shí)例
1.后臺(tái)導(dǎo)入代碼
import cn.afterturn.easypoi.excel.entity.ImportParams; import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult; import cn.afterturn.easypoi.excel.imports.ExcelImportService; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.multipart.MultipartFile; @ApiOperation(value = "以導(dǎo)入excel方式") @PostMapping(value = "/uuApplyUserInfo") public String importMonitor(@RequestParam MultipartFile file) throws Exception { if (file == null) { return ValueUtil.isError("導(dǎo)入失敗,上傳文件數(shù)據(jù)不能為空"); } ImportParams params = new ImportParams(); params.setNeedVerify(true);//是否開啟校驗(yàn) params.setHeadRows(1); //頭行忽略的行數(shù) final ExcelImportService excelImportService = new ExcelImportService(); ExcelImportResult excelImportResult = excelImportService.importExcelByIs(file.getInputStream(), YzLicensedUnit.class, params, false); //校驗(yàn)成功數(shù)據(jù) List<YzLicensedUnit> list = excelImportResult.getList(); final Field failCollection = ExcelImportService.class.getDeclaredField("failCollection"); failCollection.setAccessible(true); //校驗(yàn)失敗數(shù)據(jù) List<YzLicensedUnit> failList = (List) failCollection.get(excelImportService); if (list.size() == 0 && failList.size() == 0) { return ValueUtil.isError("導(dǎo)入失敗,上傳文件數(shù)據(jù)不能為空"); } if (failList.size() > 0){ return ValueUtil.isError("導(dǎo)入失敗,上傳文件數(shù)據(jù)與模板不一致"); } //如果沒有錯(cuò)誤,可以存入數(shù)據(jù)庫(kù) if (list.size() >= 0 && StringUtil.isNotEmpty(list)) { //批量插入sql語(yǔ)句 licensedUnitService.saveBatch(list); }else{ return ValueUtil.isError("導(dǎo)入失敗,上傳文件數(shù)據(jù)不能為空"); } return ValueUtil.toJson("導(dǎo)入成功"); }
2.實(shí)體類
import cn.afterturn.easypoi.excel.annotation.Excel; @Data @TableName("數(shù)據(jù)庫(kù)表名") public class YzLicensedUnit { //表格有的字段都要加Execl,并且name要跟表格字段一致 @Excel(name = "持證面積/畝") @NotNull(message = "持證面積/畝不能為空") private BigDecimal acreage; @ApiModelProperty(value = "經(jīng)度") private String longitude; @ApiModelProperty(value = "緯度") private String latitude; //replace 表格傳來(lái)的值如果等于 是,則字段內(nèi)容插到表中的是0,否就是1 @Excel(name = "苗種生產(chǎn)許可證持證單位",replace ={"是_0","否_1"}) @NotNull(message = "苗種生產(chǎn)許可證持證單位不能為空") private String permit; @Excel(name = "持證編號(hào)") @NotNull(message = "持證編號(hào)不能為空") private String number; @Excel(name = "持證單位") @NotNull(message = "持證單位不能為空") private String entName;
2.1設(shè)置表格下拉選項(xiàng)
3.vue前端導(dǎo)入功能代碼
<el-upload :auto-upload="true" :multiple="false" :on-change="handleChange" :on-success="fileUploadSuccess" :on-error="fileUploadError" :file-list="fileList" :action="BASE_API" name="file" accept="application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" > <el-button size="small" type="primary">批量導(dǎo)入</el-button> </el-upload> export default { data() { return { fileList: [], //批量導(dǎo)入接口地址 BASE_API: this.http_url + "/api/uuApplyUserInfo", }; }, methods: { handleChange() { }, // 上傳多于一個(gè)文件時(shí) fileUploadExceed() { this.$message.warning("只能選取一個(gè)文件"); }, //上傳成功回調(diào):通信成功 fileUploadSuccess(row) { //業(yè)務(wù)失敗 if (row.code == '500') { this.$message.error(row.msg); } else { //業(yè)務(wù)成功 this.$message.success(row.msg); } this.fileList = []; this.search(); }, //上傳失敗回調(diào):通信失敗 fileUploadError(error) { error = JSON.parse(error.toString().substr(6)); this.$message.error(error.msg); } }
總結(jié)
到此這篇關(guān)于java批量導(dǎo)入Excel數(shù)據(jù)的文章就介紹到這了,更多相關(guān)java批量導(dǎo)入Excel數(shù)據(jù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java多線程工具CompletableFuture的使用教程
CompletableFuture實(shí)現(xiàn)了CompletionStage接口和Future接口,前者是對(duì)后者的一個(gè)擴(kuò)展,增加了異步回調(diào)、流式處理、多個(gè)Future組合處理的能力。本文就來(lái)詳細(xì)講講CompletableFuture的使用方式,需要的可以參考一下2022-08-08Spring自動(dòng)裝配之方法、構(gòu)造器位置的自動(dòng)注入操作
這篇文章主要介紹了Spring自動(dòng)裝配之方法、構(gòu)造器位置的自動(dòng)注入操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08Java+EasyExcel實(shí)現(xiàn)文件的導(dǎo)入導(dǎo)出
在項(xiàng)目中我們常常需要Excel文件的導(dǎo)入與導(dǎo)出,手動(dòng)輸入相對(duì)有些繁瑣,所以本文教大家如何在Java中輕松導(dǎo)入與導(dǎo)出Excel文件,感興趣的可以學(xué)習(xí)一下2021-12-12Java?靜態(tài)代理與動(dòng)態(tài)代理解析
這篇文章主要介紹了Java?靜態(tài)代理與動(dòng)態(tài)代理解析,關(guān)于靜態(tài)代理與動(dòng)態(tài)代理,一直是比較困擾很多新人開發(fā),但實(shí)際我們開發(fā)中,小到寫的某個(gè)工具類,大到經(jīng)常使用的Retrofit?其內(nèi)部都使用了動(dòng)態(tài)代理,所以這篇文章從基礎(chǔ)到源碼解析,以便簡(jiǎn)單理解靜態(tài)代理與Jdk中的動(dòng)態(tài)代理2022-02-02基于java實(shí)現(xiàn)停車場(chǎng)管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了基于java實(shí)現(xiàn)停車場(chǎng)管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11scala+redis實(shí)現(xiàn)分布式鎖的示例代碼
這篇文章主要介紹了scala+redis實(shí)現(xiàn)分布式鎖的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06