星空网 > 软件开发 > Java

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

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

基本的思路:

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

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

代码:

页面:

新模excel导入<input type="file" name="excel" id="xinmu">  <input type="button" id="newmj" value="导入">

js

//通过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

 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处理类

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不可用,而且文件名字不可以是中文。

原标题:Java 用jxl读取excel并保存到数据库

关键词:JAVA

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

寄快递到泰国要多久:https://www.goluckyvip.com/tag/103749.html
快递寄到泰国要多久:https://www.goluckyvip.com/tag/103750.html
泰国快递安全吗:https://www.goluckyvip.com/tag/103751.html
泰国寄快递到中国多少钱一公斤:https://www.goluckyvip.com/tag/103752.html
快递可以寄到泰国吗:https://www.goluckyvip.com/tag/103756.html
往泰国发快递怎么发:https://www.goluckyvip.com/tag/103757.html
深圳有没有比较好玩的景点 深圳有没有比较好玩的景点推荐一下:https://www.vstour.cn/a/366175.html
长治婚庆女司仪和主持人:https://www.vstour.cn/a/366176.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流