Java用jxl读取excel并保存到数据库的方法

2025-05-29 0 97

项目中涉及到读取excel中的数据,保存到数据库中,用jxl做起来比较简单。

基本的思路:

把excel放到固定盘里,然后前段页面选择文件,把文件的名字传到后台,再利用jxl进行数据读取,把读取到的数据存到list中,通过遍历list,得到map,存到数据库中。

首先导入jar包:在网上都有,

代码:

页面:

新模excel导入

?

1
<input type="file" name="excel" id="xinmu">

?

1
<input type="button" id="newmj" value="导入">

js

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15
//通过ajax进行操作

$(function(){

$("#newmj").click(function(){

alert("haha");

$.ajax({

url:'${pageContext.request.contextPath}/UploadExcelServlet?type=xinmu&filename='+$("#xinmu").val(),

type:'get',

success:function(result){

//alert("haha");

alert(result);

var json= eval('(' + result + ')');

              }

})

})

});

servlet

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

//request.setCharacterEncoding("utf-8");

System.out.println("jinru");

String type=request.getParameter("type");

String filename=request.getParameter("filename");

//System.out.println(filename);

File file = new File("D:\\\\"+filename);// 表格存储的位置

JSONObject jsonObject = new JSONObject();

//记录一下文件是否存在

if (file.exists()) {

jsonObject.put("exist", "文件存在");

List<Map<String, String>>list=ReadExcel.readExcel(file);

MuJUService mjService = new MuJUService();

for (Map<String, String> map : list) {

jsonObject = mjService.addNewMuJu(map);

}

} else {

jsonObject.put("exist", "文件不存在");

System.out.println("文件不存在");

}

}

jxl处理类

?

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
import java.io.File;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import jxl.Cell;

import jxl.Sheet;

import jxl.Workbook;

public class ReadExcel {

public static List<Map<String,String>> readExcel(File file){

List<Map<String, String>>list =new ArrayList<Map<String,String>>();

try {

// 判断文件是否存在

// 创建工作簿

Workbook workbook = Workbook.getWorkbook(file);

// 获得第一个工作表sheet1

Sheet sheet = workbook.getSheet(0);

// 获得数据

for (int i = 1; i < sheet.getRows(); i++) {// sheet.getRows():获得表格文件行数

Map<String, String>map = new HashMap<String, String>();

for (int j = 0; j < sheet.getColumns(); j++) {// sheet.getColumns():获得表格文件列数

Cell cell = sheet.getCell(j, i);

// System.out.print(cell.getContents() + " ");

map.put(sheet.getCell(j,0).getContents(), cell.getContents());

//(列,行)

}

//System.out.println("");// 换行

list.add(map);

}

//调用方法进行数据库的操作

//.......

System.out.println(list);

workbook.close();// 关闭

} catch (Exception e) {

e.printStackTrace();

}

return list;

}

}

如此就能完成了,但是值得注意的是,我现在写的这段代码,无法自由选择文件路径进行读取,excel必须放在固定盘里。excel后缀必须是.xls,所以wps的excel不可用,而且文件名字不可以是中文。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。

原文链接:http://www.cnblogs.com/stepbystepwhx/archive/2017/10/19/7693298.html

收藏 (0) 打赏

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

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

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

快网idc优惠网 建站教程 Java用jxl读取excel并保存到数据库的方法 https://www.kuaiidc.com/114460.html

相关文章

发表评论
暂无评论