導出就是將list轉化為excel(listtoexcel)
導入就是將excel轉化為list(exceltolist)
一、思路分析
1、我們要做導入,實際上也就是先文件上傳,然后讀取文件的數據。
2、我們要有一個導入的模板,因為我們導入的excel列要和我們的數據字段匹配上,所以我們要給它來一個規定,也就是模板。
3、按照我們公司的套路,是做了一個導入信息的臨時表,用來存導入文件中的信息。每當導入的時候,我們先把表信息清空,再拿到數據放進來,然后我們對導入的數據進行檢查,最后才全部導入。
這樣做的目的是防止導入的數據和列沒有對上卻也直接導到了庫里面,要真這樣了就很尷尬。我們做了三個按鈕,一個導入,一個確認導入,一個導入模板下載。
4、捋一下過程:
點擊批量導入按鈕,跳轉到導入頁面。
點擊模板下載。(事先寫好一個模板.xls文件,此按鈕放置下載鏈接)
在模板中錄入數據。
點擊導入按鈕,跳出文件上傳模態框。
選擇文件,上傳文件。
上傳成功后調用后臺方法,讀取文件內容。
把內容生成表格,顯示到導入頁面。
觀察數據有沒有出錯。
點擊確認導入,調用后臺方法,執行導入操作。
回調函數,導入成功/導入失敗 的提示。
二、代碼分析
1、前端js代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
var actionpath = contextpath + "/alumni-import" ; $(function() { //加載數據 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" ); } }); //導入 $( "#btn-import" ).click(function() { var html = template( "importtpl" ); $( "#import-body" ).html(html); $( "#mymodal-import" ).modal(); }); //確認導入 $( "#btn-sure" ).click(function() { var type = $( "#indentity-type" ).val(); alertify.confirm( "確定要全部導入嗎?" , function(e) { if (!e) { return ; } else { $.post( "/alumni-import!savereal?type=" + type, function(data) { alertify.alert( "導入成功!" , function() { location.href = "/alumni!hrefpage" ; }); }, "json" ); } }); }); }); 50 function loaddata() { var options = { url: actionpath + "!page" }; loadpaginationdata(options); } |
2、后臺功能代碼
前端有四個請求,一個初始化頁面數據加載,當然,一開始是沒有數據的;一個導入文件上傳;一個確認導入;一個導入完成后頁面跳轉回要信息頁面(信息頁面有一個批量導入跳轉到這的導入頁面)。
第一個后最后一個就不講了。講一下第二個和第三個。
①第二個
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
//上傳文件后調用 public void importexcel() { try { //清空臨時表的數據 basealumniimportsrv.deleteall(); //讀取文件 file file = gridfsdao.readfile(f_id); //把文件信息給臨時表 int count = excelxysrv.importexcel(file); //清空上傳的文件 file.delete(); sendsuccessmsg(count, "上傳成功" + count + "條數據" ); } catch (ioexception e) { logger.error(e); sendfailmsg( null , "上傳失敗" ); } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
@override //使用mongodb gridfs,具體詳解請各自初學者自行百度,注釋寫不下去了,我也不會,心態爆炸~~~ 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; } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
/** * @param excelfile * 從excel中讀取數據,并存儲到數據庫臨時表中 * @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; } |
②第三個
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
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, "導入成功" + importlist.size() + "條數據" ); } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
@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; } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
@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; } |
沒啥好講的……會的應該都能看懂,看不懂的我也不會……
三、結果圖
總結
以上就是本文關于java 導入excel思路及代碼示例的全部內容,希望對大家有所幫助。如有不足之處,歡迎留言指出。
原文鏈接:http://www.cnblogs.com/qq765065332/p/7843407.html