欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java用jxl讀取excel并保存到數(shù)據(jù)庫的方法

 更新時間:2017年10月19日 17:05:57   作者:王懷霞  
這篇文章主要為大家詳細(xì)介紹了Java用jxl讀取excel并保存到數(shù)據(jù)庫的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下

項目中涉及到讀取excel中的數(shù)據(jù),保存到數(shù)據(jù)庫中,用jxl做起來比較簡單。

基本的思路:

把excel放到固定盤里,然后前段頁面選擇文件,把文件的名字傳到后臺,再利用jxl進(jìn)行數(shù)據(jù)讀取,把讀取到的數(shù)據(jù)存到list中,通過遍歷list,得到map,存到數(shù)據(jù)庫中。

首先導(dǎo)入jar包:在網(wǎng)上都有,

代碼:

頁面:

新模excel導(dǎo)入

<input type="file" name="excel" id="xinmu">
<input type="button" id="newmj" value="導(dǎo)入">

js

//通過ajax進(jìn)行操作
$(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 {
   // 判斷文件是否存在
    // 創(chuàng)建工作簿
    Workbook workbook = Workbook.getWorkbook(file);
    // 獲得第一個工作表sheet1
    Sheet sheet = workbook.getSheet(0);
    // 獲得數(shù)據(jù)
    for (int i = 1; i < sheet.getRows(); i++) {// sheet.getRows():獲得表格文件行數(shù)
     Map<String, String>map = new HashMap<String, String>();
     for (int j = 0; j < sheet.getColumns(); j++) {// sheet.getColumns():獲得表格文件列數(shù)
      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);
    }
    //調(diào)用方法進(jìn)行數(shù)據(jù)庫的操作
    //.......
    System.out.println(list);
    workbook.close();// 關(guān)閉
   } catch (Exception e) {
    e.printStackTrace();
   }
  return list;
 }
}

如此就能完成了,但是值得注意的是,我現(xiàn)在寫的這段代碼,無法自由選擇文件路徑進(jìn)行讀取,excel必須放在固定盤里。excel后綴必須是.xls,所以wps的excel不可用,而且文件名字不可以是中文。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論