Home > Java > javaTutorial > Example of how Java uses jxl to read excel and save it to the database

Example of how Java uses jxl to read excel and save it to the database

黄舟
Release: 2017-10-20 09:34:05
Original
1615 people have browsed it

这篇文章主要为大家详细介绍了Java用jxl读取excel并保存到数据库的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

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

基本的思路:

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

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

代码:

页面:

新模excel导入


<input type="file" name="excel" id="xinmu">
Copy after login


<input type="button" id="newmj" value="导入">
Copy after login

js


//通过ajax进行操作
$(function(){
   $("#newmj").click(function(){
    alert("haha");
    $.ajax({
     url:&#39;${pageContext.request.contextPath}/UploadExcelServlet?type=xinmu&filename=&#39;+$("#xinmu").val(),
     type:&#39;get&#39;,
     success:function(result){
      //alert("haha");
      alert(result);
       var json= eval(&#39;(&#39; + result + &#39;)&#39;);
              }
    })
   })
  });
Copy after login

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("文件不存在");
  }
  
 
 }
Copy after login

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;
 }
}
Copy after login

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

The above is the detailed content of Example of how Java uses jxl to read excel and save it to the database. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template