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ù)庫
if (list.size() >= 0 && StringUtil.isNotEmpty(list)) {
//批量插入sql語句
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ù)庫表名")
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 表格傳來的值如果等于 是,則字段內(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使用默認(rèn)瀏覽器打開指定URL的方法(二種方法)
Java使用默認(rèn)瀏覽器打開指定URL。2013-10-10
IntelliJ IDEA中折疊所有Java代碼,再也不怕大段的代碼了
今天小編就為大家分享一篇關(guān)于IntelliJ IDEA中折疊所有Java代碼,再也不怕大段的代碼了,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-10-10
解決springboot無法注入JpaRepository的問題
這篇文章主要介紹了解決springboot無法注入JpaRepository的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-01-01
用StopWatch優(yōu)雅替代currentTimeMillis計(jì)算程序執(zhí)行耗時(shí)
別再用System.currentTimeMillis()計(jì)算程序執(zhí)行耗時(shí)了,擁抱StopWatch優(yōu)雅來優(yōu)雅的計(jì)算,代碼更簡潔效率更高,本文帶你了解StopWatch的使用2021-09-09
MyBatis Plus like參數(shù)為百分號(hào)%查詢結(jié)果異常原因分析及解決
文章指出在MyBatisPlus使用LIKE查詢時(shí),用戶輸入的%可能導(dǎo)致通配符誤匹配,推薦使用ESCAPE轉(zhuǎn)義功能,將%轉(zhuǎn)換為普通字符,既支持模糊查詢又避免全表匹配,提升準(zhǔn)確性和性能2025-09-09
關(guān)于Java兩個(gè)浮點(diǎn)型數(shù)字加減乘除的問題
由于浮點(diǎn)數(shù)在計(jì)算機(jī)中是以二進(jìn)制表示的,直接進(jìn)行加減乘除運(yùn)算會(huì)出現(xiàn)精度誤差,想要得到精確結(jié)果,應(yīng)使用BigDecimal類進(jìn)行運(yùn)算2024-10-10
利用Spring Validation實(shí)現(xiàn)輸入驗(yàn)證功能
這篇文章主要給大家介紹了如何利用Spring Validation完美的實(shí)現(xiàn)輸入驗(yàn)證功能,文中有詳細(xì)的代碼示例,具有一定的參考價(jià)值,感興趣的朋友可以借鑒一下2023-06-06

