java 导入Excel思路及代码示例

2025-05-27 0 71

导出就是将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思路及代码示例

java 导入Excel思路及代码示例

java 导入Excel思路及代码示例

java 导入Excel思路及代码示例

总结

以上就是本文关于java 导入excel思路及代码示例的全部内容,希望对大家有所帮助。如有不足之处,欢迎留言指出。

原文链接:http://www.cnblogs.com/qq765065332/p/7843407.html

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 java 导入Excel思路及代码示例 https://www.kuaiidc.com/77589.html

相关文章

发表评论
暂无评论