java 導(dǎo)入Excel思路及代碼示例
導(dǎo)出就是將List轉(zhuǎn)化為Excel(listToExcel)
導(dǎo)入就是將Excel轉(zhuǎn)化為List(excelToList)
一、思路分析
1、我們要做導(dǎo)入,實際上也就是先文件上傳,然后讀取文件的數(shù)據(jù)。
2、我們要有一個導(dǎo)入的模板,因為我們導(dǎo)入的Excel列要和我們的數(shù)據(jù)字段匹配上,所以我們要給它來一個規(guī)定,也就是模板。
3、按照我們公司的套路,是做了一個導(dǎo)入信息的臨時表,用來存導(dǎo)入文件中的信息。每當(dāng)導(dǎo)入的時候,我們先把表信息清空,再拿到數(shù)據(jù)放進(jìn)來,然后我們對導(dǎo)入的數(shù)據(jù)進(jìn)行檢查,最后才全部導(dǎo)入。
這樣做的目的是防止導(dǎo)入的數(shù)據(jù)和列沒有對上卻也直接導(dǎo)到了庫里面,要真這樣了就很尷尬。我們做了三個按鈕,一個導(dǎo)入,一個確認(rèn)導(dǎo)入,一個導(dǎo)入模板下載。
4、捋一下過程:
點擊批量導(dǎo)入按鈕,跳轉(zhuǎn)到導(dǎo)入頁面。
點擊模板下載。(事先寫好一個模板.xls文件,此按鈕放置下載鏈接)
在模板中錄入數(shù)據(jù)。
點擊導(dǎo)入按鈕,跳出文件上傳模態(tài)框。
選擇文件,上傳文件。
上傳成功后調(diào)用后臺方法,讀取文件內(nèi)容。
把內(nèi)容生成表格,顯示到導(dǎo)入頁面。
觀察數(shù)據(jù)有沒有出錯。
點擊確認(rèn)導(dǎo)入,調(diào)用后臺方法,執(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: "請選擇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、后臺功能代碼
前端有四個請求,一個初始化頁面數(shù)據(jù)加載,當(dāng)然,一開始是沒有數(shù)據(jù)的;一個導(dǎo)入文件上傳;一個確認(rèn)導(dǎo)入;一個導(dǎo)入完成后頁面跳轉(zhuǎn)回要信息頁面(信息頁面有一個批量導(dǎo)入跳轉(zhuǎn)到這的導(dǎo)入頁面)。
第一個后最后一個就不講了。講一下第二個和第三個。
?、俚诙€
//上傳文件后調(diào)用 public void importExcel() { try { //清空臨時表的數(shù)據(jù) baseAlumniImportSrv.deleteAll(); //讀取文件 File file = gridFsDao.readFile(f_id); //把文件信息給臨時表 int count = excelXYSrv.importExcel(file); //清空上傳的文件 file.delete(); sendSuccessMsg(count, "上傳成功" + count + "條數(shù)據(jù)"); } catch (IOException e) { LOGGER.error(e); sendFailMsg(null, "上傳失敗"); } }
@Override //使用MongoDB GridFS,具體詳解請各自初學(xué)者自行百度,注釋寫不下去了,我也不會,心態(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ù),并存儲到數(shù)據(jù)庫臨時表中 * @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; }
?、诘谌齻€
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; }
沒啥好講的……會的應(yīng)該都能看懂,看不懂的我也不會……
三、結(jié)果圖
總結(jié)
以上就是本文關(guān)于java 導(dǎo)入Excel思路及代碼示例的全部內(nèi)容,希望對大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站:
如有不足之處,歡迎留言指出。
相關(guān)文章
Spring boot實現(xiàn)文件上傳實例(多文件上傳)
本篇文章主要介紹了Spring boot實現(xiàn)文件上傳實例(多文件上傳),具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-05-05Java9新特性對HTTP2協(xié)議支持與非阻塞HTTP?API
這篇文章主要為大家介紹了Java9新特性對HTTP2協(xié)議的支持與非阻塞HTTP?API,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-03-03關(guān)于break和continue以及l(fā)abel的區(qū)別和作用(詳解)
下面小編就為大家?guī)硪黄P(guān)于break和continue以及l(fā)abel的區(qū)別和作用(詳解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-05-05

Java的Cglib動態(tài)代理實現(xiàn)方式詳解