溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Java實(shí)現(xiàn)Excel批量導(dǎo)入數(shù)據(jù)

發(fā)布時(shí)間:2020-09-06 16:11:23 來源:腳本之家 閱讀:836 作者:樹狀細(xì)胞 欄目:開發(fā)技術(shù)

Excel的批量導(dǎo)入是很常見的功能,這里采用 Jxl實(shí)現(xiàn),數(shù)據(jù)量或樣式要求較高可以采用 poi

框架環(huán)境:Spring + SpringMvc(注解實(shí)現(xiàn))

1.首先導(dǎo)入依賴jar包

<dependency>
  <groupId>net.sourceforge.jexcelapi</groupId>
  <artifactId>jxl</artifactId>
  <version>2.6.10</version>
</dependency>

2.前端頁面–jsp(enctype必須為"multipart/form-data"

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<div >
 <form class="form" method="post" data-submit="ajax" enctype="multipart/form-data" 
 action="/hpersonnel/import.do">
  //<input type="hidden" name="id" value="<%= request.getParameter("id") %>" />   
  <div class="form-group">   
   <label class="form-label w100"> </label>
   <label class="form-label ">1、下載模板。
   <a class="btn btn-default submit-form" href="/template/personnel.xls" rel="external nofollow" >下載</a>
   </label>
  </div>
  <div class="form-group">
   <label class="form-label w100"> </label>
   <label class="form-label ">2、按照模板表頭填寫內(nèi)容,請勿修改文件格式</label>
  </div>
  <div class="form-group">
   <label class="form-label w100"> </label>
   <label class="form-label ">3、<input type="file" name="file" /></label>
  </div>   
  <div class="btns dialog-button">
   <button class="dialog-btn-yes" type="submit">提交</button>
   <button class="dialog-btn-no" type="button" onclick="app.closeDialog(this)">取消</button>
  </div> 
 </form>
</div>

3.視圖模板

Java實(shí)現(xiàn)Excel批量導(dǎo)入數(shù)據(jù)

4.Controller

@ResponseBody
@RequestMapping(value = "import")
public Object import(MultipartFile file) { 
 
 if (file.isEmpty()) {
  return ResultObject.failure("文件為空");
 }
 ResultObject result = new ResultObject();
 //記錄集合
 List<Map> mapList = new ArrayList<Map>();
 //校驗(yàn)結(jié)果
 boolean reqFlag = true;
 //回復(fù)消息
 String reqMsg = "";
 //報(bào)錯消息數(shù)
 Integer error = 0;
 //解析文件
 try {
  //轉(zhuǎn)換成輸入流
  InputStream is = file.getInputStream();
  //得到excel
  Workbook workbook = Workbook.getWorkbook(is);
  //得到sheet
  Sheet sheet = workbook.getSheet(0);
  //得到列數(shù)
  int colsNum = sheet.getColumns();
  //得到行數(shù)
  int rowsNum = sheet.getRows();
  if (rowsNum == 1) return ResultObject.failure("沒有數(shù)據(jù)");
  //單元格
  Cell cell;
  //數(shù)據(jù)校驗(yàn)
  for (int i = 1; i < rowsNum; i++) {//第一行是標(biāo)題,所以i從1開始
   Map<Integer, Object> map = new HashMap<Integer, Object>();
   for (int j = 0; j < colsNum; j++) {
    cell = sheet.getCell(j, i);//第一個參數(shù)是列.第二個參數(shù)是行
    if (j < 4 && "".equals(cell.getContents())) { //----這里判斷必填項(xiàng)(前4列)
     reqFlag = false;
     reqMsg += "第" + (i + 1) + "行錯誤,錯誤信息:" + "必填項(xiàng)缺漏";
     reqMsg += "<br>";
     error++;
     break;
    }
    String cellString = cell.getContents();
    cellString = cellString.trim();
    switch (j) {
     case 1: {
      //進(jìn)行校驗(yàn)處理,例如手機(jī)號
       if (!StringUtil.isMobileNo(cellString)) {
       reqFlag = false;
       reqMsg += "第" + (i + 1) + "行錯誤,錯誤信息:" + "聯(lián)系電話有誤";
       reqMsg += "<br>";
       error++;
      } else {
       map.put(j, cellString);
      }
      break;
     }
     case 2: { ### break; }
     case 3: { ### break; }
     //無需校驗(yàn),歸入default
     default: {
      map.put(j, cell.getContents());
     }
    }
   }
   if (reqFlag) {//校驗(yàn)通過
    mapList.add(map);
   }
  }
 } catch (IOException e) {
  e.printStackTrace();
 } catch (BiffException e) {
  e.printStackTrace();
 }
 //入庫
 try {
  if (mapList.size() > 0 && reqFlag) {
   //此處try,catch應(yīng)優(yōu)化為事務(wù)處理maplist實(shí)現(xiàn)全記錄成功或失敗
   result.setSuccess("提交成功");
  } else {    
   if (error > 10) {//設(shè)置要顯示的錯誤數(shù)
    int index = StringUtil.getIndex(reqMsg, 10, "<br>");
    reqMsg = reqMsg.substring(0, index + 4);
    reqMsg += "未顯示錯誤數(shù):" + (error - 10) + "條";
    reqMsg += "<br>";
   }
   result.setFailure(reqMsg);
  }
 } catch (Exception e) {
  result.setFailure("入庫錯誤,請聯(lián)系管理員!");
  e.printStackTrace();
 }
 return result;
}

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

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI