java 導(dǎo)入Excel思路及代碼示例
導(dǎo)出就是將List轉(zhuǎn)化為Excel(listToExcel)
導(dǎo)入就是將Excel轉(zhuǎn)化為L(zhǎng)ist(excelToList)
一、思路分析
1、我們要做導(dǎo)入,實(shí)際上也就是先文件上傳,然后讀取文件的數(shù)據(jù)。
2、我們要有一個(gè)導(dǎo)入的模板,因?yàn)槲覀儗?dǎo)入的Excel列要和我們的數(shù)據(jù)字段匹配上,所以我們要給它來(lái)一個(gè)規(guī)定,也就是模板。
3、按照我們公司的套路,是做了一個(gè)導(dǎo)入信息的臨時(shí)表,用來(lái)存導(dǎo)入文件中的信息。每當(dāng)導(dǎo)入的時(shí)候,我們先把表信息清空,再拿到數(shù)據(jù)放進(jìn)來(lái),然后我們對(duì)導(dǎo)入的數(shù)據(jù)進(jìn)行檢查,最后才全部導(dǎo)入。
這樣做的目的是防止導(dǎo)入的數(shù)據(jù)和列沒(méi)有對(duì)上卻也直接導(dǎo)到了庫(kù)里面,要真這樣了就很尷尬。我們做了三個(gè)按鈕,一個(gè)導(dǎo)入,一個(gè)確認(rèn)導(dǎo)入,一個(gè)導(dǎo)入模板下載。
4、捋一下過(guò)程:
點(diǎn)擊批量導(dǎo)入按鈕,跳轉(zhuǎn)到導(dǎo)入頁(yè)面。
點(diǎn)擊模板下載。(事先寫好一個(gè)模板.xls文件,此按鈕放置下載鏈接)
在模板中錄入數(shù)據(jù)。
點(diǎn)擊導(dǎo)入按鈕,跳出文件上傳模態(tài)框。
選擇文件,上傳文件。
上傳成功后調(diào)用后臺(tái)方法,讀取文件內(nèi)容。
把內(nèi)容生成表格,顯示到導(dǎo)入頁(yè)面。
觀察數(shù)據(jù)有沒(méi)有出錯(cuò)。
點(diǎn)擊確認(rèn)導(dǎo)入,調(diào)用后臺(tái)方法,執(zhí)行導(dǎo)入操作。
回調(diào)函數(shù),導(dǎo)入成功/導(dǎo)入失敗 的提示。
二、代碼分析
1、前端js代碼:
var actionPath = contextPath + "/alumni-import";
$(function() {
//加載數(shù)據(jù)
loadData();
//上傳文件
uploadFile({
subfix: ['xls'],
subfixTip: "請(qǐng)選擇Excel的xls文件!",
successCall: function(data, status, a) {
$('[name=attachementPath]').val(data.fileName);
$.post(actionPath + "!importExcel", { "f_id": data.f_id }, function(data) {
if (data.success) {
alertify.alert(data.message);
$("#myModal-import").modal("hide");
loadData();
} else {
alertify.alert(data.message);
}
}, "json");
}
});
//導(dǎo)入
$("#btn-import").click(function() {
var html = template("importTpl");
$("#import-body").html(html);
$("#myModal-import").modal();
});
//確認(rèn)導(dǎo)入
$("#btn-sure").click(function() {
var type = $("#indentity-type").val();
alertify.confirm("確定要全部導(dǎo)入嗎?", function(e) {
if (!e) { return; } else {
$.post("/alumni-import!saveReal?type=" + type, function(data) {
alertify.alert("導(dǎo)入成功!", function() {
location.href = "/alumni!hrefPage";
});
}, "json");
}
});
});
});50
function loadData() {
var options = {
url: actionPath + "!page"
};
loadPaginationData(options);
}
2、后臺(tái)功能代碼
前端有四個(gè)請(qǐng)求,一個(gè)初始化頁(yè)面數(shù)據(jù)加載,當(dāng)然,一開(kāi)始是沒(méi)有數(shù)據(jù)的;一個(gè)導(dǎo)入文件上傳;一個(gè)確認(rèn)導(dǎo)入;一個(gè)導(dǎo)入完成后頁(yè)面跳轉(zhuǎn)回要信息頁(yè)面(信息頁(yè)面有一個(gè)批量導(dǎo)入跳轉(zhuǎn)到這的導(dǎo)入頁(yè)面)。
第一個(gè)后最后一個(gè)就不講了。講一下第二個(gè)和第三個(gè)。
?、俚诙€(gè)
//上傳文件后調(diào)用
public void importExcel() {
try {
//清空臨時(shí)表的數(shù)據(jù)
baseAlumniImportSrv.deleteAll();
//讀取文件
File file = gridFsDao.readFile(f_id);
//把文件信息給臨時(shí)表
int count = excelXYSrv.importExcel(file);
//清空上傳的文件
file.delete();
sendSuccessMsg(count, "上傳成功" + count + "條數(shù)據(jù)");
} catch (IOException e) {
LOGGER.error(e);
sendFailMsg(null, "上傳失敗");
}
}
@Override //使用MongoDB GridFS,具體詳解請(qǐng)各自初學(xué)者自行百度,注釋寫不下去了,我也不會(huì),心態(tài)爆炸~~~
public File readFile(String f_id) {
//拿到文件
GridFSDBFile gridFSFile = mongoGridFs.findOne(new Query(Criteria.where(F_ID).is(f_id)));
if (gridFSFile == null) {
return null;
}
String fileName = gridFSFile.getFilename();
String extension = fileName.substring(fileName.lastIndexOf("."), fileName.length());
InputStream ins = gridFSFile.getInputStream();
String tmpFile = UUID.randomUUID().toString() + extension;
File file = new File(tmpFile);
try {
OutputStream os = new FileOutputStream(file);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
os.write(buffer, 0, bytesRead);
}
os.close();
ins.close();
} catch (IOException e) {
e.printStackTrace();
}
return file;
}
/**
* @param excelFile
* 從excel中讀取數(shù)據(jù),并存儲(chǔ)到數(shù)據(jù)庫(kù)臨時(shí)表中
* @return
* @throws IOException
*/
@Override
public int importExcel(File excelFile) throws IOException {
List<List<Object>> datas = ExcelImportUtil.readExcel(excelFile);
int count = 0;
for (int i = 1; i < datas.size(); i++) {
BaseAlumniImport entity = this.convert2Entity(datas.get(i));
this.baseAlumniImportSrv.save(entity);
count++;
}
return count;
}
?、诘谌齻€(gè)
public void saveReal() {
int count = 0;
List<BaseAlumniImport> importList = this.baseAlumniImportSrv.findAll();
for (int i = 0; i < importList.size(); i += 10) {
List<BaseAlumniImport> newlist = new ArrayList<>();
if ((i + 10) < importList.size()) {
newlist = importList.subList(i, i + 10);
} else {
newlist = importList.subList(i, importList.size());
}
count += excelXYSrv.saveReal(newlist, this.type);
}
sendSuccessMsg(count, "導(dǎo)入成功" + importList.size() + "條數(shù)據(jù)");
}
@Override
public int saveReal(List<BaseAlumniImport> importList, String type) {
int count = 0;
String alumniIdentityName = dicSrv.findById(DicAlumniIdentity.class, Integer.parseInt(type)).getValue();
for (BaseAlumniImport inst : importList) {
LOGGER.info(inst.getId());
BaseAlumni v = this.importExportSrv.convert(inst);
v.setId(IdKit.uuid());
v.setCreateTime(new Date());
v.setLastUpdate(new Date());
this.baseAlumnidDao.save(v);
this.baseAlumniImportSrv.deleteById(inst.getId());
count++;
}
return count;
}
@Override
public int saveReal(List<BaseAlumniImport> importList, String type) {
int count = 0;
String alumniIdentityName = dicSrv.findById(DicAlumniIdentity.class, Integer.parseInt(type)).getValue();
for (BaseAlumniImport inst : importList) {
LOGGER.info(inst.getId());
BaseAlumni v = this.importExportSrv.convert(inst);
v.setId(IdKit.uuid());
v.setCreateTime(new Date());
v.setLastUpdate(new Date());
this.baseAlumnidDao.save(v);
this.baseAlumniImportSrv.deleteById(inst.getId());
count++;
}
return count;
}
沒(méi)啥好講的……會(huì)的應(yīng)該都能看懂,看不懂的我也不會(huì)……
三、結(jié)果圖




總結(jié)
以上就是本文關(guān)于java 導(dǎo)入Excel思路及代碼示例的全部?jī)?nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站:
Java探索之Thread+IO文件的加密解密代碼實(shí)例
如有不足之處,歡迎留言指出。
相關(guān)文章
Spring boot實(shí)現(xiàn)文件上傳實(shí)例(多文件上傳)
本篇文章主要介紹了Spring boot實(shí)現(xiàn)文件上傳實(shí)例(多文件上傳),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05
Java9新特性對(duì)HTTP2協(xié)議支持與非阻塞HTTP?API
這篇文章主要為大家介紹了Java9新特性對(duì)HTTP2協(xié)議的支持與非阻塞HTTP?API,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-03-03
關(guān)于break和continue以及l(fā)abel的區(qū)別和作用(詳解)
下面小編就為大家?guī)?lái)一篇關(guān)于break和continue以及l(fā)abel的區(qū)別和作用(詳解)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-05-05
Java的Cglib動(dòng)態(tài)代理實(shí)現(xiàn)方式詳解
Java實(shí)現(xiàn)鼠標(biāo)拖放功能的方法

