java中excel文件的導入,限制上傳的文件類型,文件的大小,顯示上傳文件的進度條


1. 前台代碼

  1 <!DOCTYPE html>
  2 <html lang="en" xmlns:th="http://www.thymeleaf.org">
  3 <head>
  4     <meta charset="UTF-8">
  5     <title>導入excel測試</title>
  6     <style>
  7         #fatherDiv{
  8             width:100px;
  9             height:20px;
 10             border:1px solid green;
 11         }
 12         #sonDiv{
 13             width:0px;
 14             height:20px;
 15             background:green;
 16         }
 17     </style>
 18 
 19     <script th:src="@{|/js/jquery-2.1.4.min.js/|}"></script>
 20     <script>
 21         $(function () {
 22 
 23             $("#importExcel").bind("click", function () {
 24 
 25                 /* 判斷是否有導入文件 */
 26                 if (!$("#excel").val()) {
 27                     window.alert("請導入excel文件");
 28                     return;
 29                 }
 30 
 31                 /* 判斷輸入的文件的類型 */
 32                 var splitArray = $("#excel").val().toLowerCase().split(".");
 33                 var type = splitArray[splitArray.length - 1];
 34                 if (type != "xls" && type != "xlsx") {
 35                     window.alert("導入的文件類型有誤");
 36                     return;
 37                 }
 38 
 39                 // 獲取excel對象
 40                 var excel = $("#excel")[0].files[0];
 41 
 42                 /* 判斷輸入的excel文件的大小 */
 43                 var excelSize = excel.size;
 44                 if (excelSize > 1024 * 1024 * 10) {
 45                     window.alert("當前上傳的excel文件的大小為" +
 46                         Math.round(excelSize / 1024 / 1024 * 100) / 100 +
 47                         "M,超過10M");
 48                     return;
 49                 }
 50 
 51                 var formData = new FormData();
 52                 formData.append("file", excel);
 53 
 54                 // ajax異步文件上傳
 55                 $.ajax({
 56                     type: "post",
 57                     url: "/importExcel",
 58                     data: formData,
 59                     contentType: false, // 不再采用普通的form表單元素提交方式。(multipart/form-data)
 60                     processData: false, // 提交文件,不是提交普通的字符串。
 61                     xhr: function () {  // 顯示上傳進度條
 62                         myXhr = $.ajaxSettings.xhr();
 63                         if (myXhr.upload) {
 64                             myXhr.upload.addEventListener('progress', function (e) {
 65                                 var loaded = e.loaded;//已經上傳大小情況
 66                                 var tot = e.total;//附件總大小
 67                                 var per = Math.floor(100 * loaded / tot); //已經上傳的百分比
 68                                 $("#sonDiv").html(per + "%");
 69                                 $("#sonDiv").css("width", per + "%");
 70                                 $("#schedule").html(Math.round(loaded / 1024 / 1024 * 100) / 100 + "M");
 71                                 console.log('附件總大小 = ' + loaded);
 72                                 console.log('已經上傳大小 = ' + tot);
 73                             }, false);
 74                         }
 75                         return myXhr;
 76                     },
 77                     success: function (json) {
 78                         if (json.result) {
 79                             alert("導入成功")
 80                         } else {
 81                             alert(json.errorMessage);
 82                         }
 83                     }
 84                 });
 85             })
 86         })
 87     </script>
 88 </head>
 89 <body>
 90     請選擇你要導入的excel文件(.xls;.xlsx)<br/>
 91     <input type="file" id="excel"/> &nbsp;&nbsp;
 92     <div id="fatherDiv">
 93         <div id="sonDiv"></div> <span id="schedule">已上傳 0M</span> &nbsp;&nbsp;
 94         <button id="importExcel">導入</button>
 95     </div><br/>
 96 
 97 
 98 
 99 </body>
100 </html>

 

2. 后台spring代碼

  2.1 controller層

  1 package com.bjpowernode.excel.importExcel.controller;
  2 
  3 import com.bjpowernode.excel.importExcel.service.ImportExcelService;
  4 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  5 import org.apache.poi.ss.usermodel.Workbook;
  6 import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  7 import org.springframework.beans.factory.annotation.Autowired;
  8 import org.springframework.stereotype.Controller;
  9 import org.springframework.web.bind.annotation.RequestMapping;
 10 import org.springframework.web.bind.annotation.RequestParam;
 11 import org.springframework.web.bind.annotation.ResponseBody;
 12 import org.springframework.web.multipart.MultipartFile;
 13 
 14 import java.io.IOException;
 15 import java.io.InputStream;
 16 import java.lang.reflect.InvocationTargetException;
 17 import java.util.HashMap;
 18 import java.util.Map;
 19 
 20 /**
 21  * ClassName:ImportExcel
 22  * Package:com.bjpowernode.excel.importExcel.controller
 23  * Description:
 24  *
 25  * @Date:2019/2/25 21:46
 26  * @Author: 鄭軍
 27  */
 28 @Controller
 29 public class ImportExcelController {
 30 
 31     @Autowired
 32     private ImportExcelService importExcelService;
 33 
 34     @RequestMapping("/")
 35     public String toIndex() {
 36         return "index";
 37     }
 38 
 39     @RequestMapping("/importExcel")
 40     @ResponseBody
 41     public Map<String, Object> importExcel(@RequestParam("file") MultipartFile file) {
 42 
 43         Map<String, Object> map = new HashMap<>();
 44 
 45         /**
 46          * 判斷文件是否為空
 47          */
 48         if (file.isEmpty()) {
 49             map.put("result", false);
 50             map.put("errorMessage", "導入數據為空");
 51             return map;
 52         }
 53 
 54         /**
 55          * 判斷文件類型是否正確
 56          */
 57         String originalFilename = file.getOriginalFilename();
 58         String fileType = originalFilename.substring(originalFilename.lastIndexOf("."));
 59         if (!".xls".equals(fileType) && !".xlsx".equals(fileType)) {
 60             map.put("result", false);
 61             map.put("errorMessage", "導入的文件類型有誤");
 62             return map;
 63         }
 64 
 65         /**
 66          * 判斷文件的大小
 67          */
 68         long size = file.getSize();
 69         if (size > 1024 * 1024 * 10) {
 70             map.put("result", false);
 71             map.put("errorMessage", "當前文件大小為"+Math.ceil(size * 100 / 1024/ 1024 / 10 / 100) +
 72             "M,超過10M");
 73             return map;
 74         }
 75 
 76         try {
 77             InputStream inputStream = file.getInputStream();
 78             /**
 79              *  通過上傳的不同的文件后綴,創建不同的workbook
 80              */
 81             Workbook workbook = null;
 82             if (".xls".equals(fileType)) {
 83                 workbook = new HSSFWorkbook(inputStream);
 84             }else {
 85                 workbook = new XSSFWorkbook(inputStream);
 86             }
 87 
 88             /**
 89              * excel文件解析並且導入數據庫。
 90              */
 91             Map<String, Object> resultMap = importExcelService.importExcel(workbook);
 92             if (resultMap.size() > 0) {
 93                 map.put("result", resultMap.get("result"));
 94                 map.put("errorMessage", resultMap.get("errorMessage"));
 95             } else {
 96                 map.put("result", false);
 97                 map.put("errorMessage", "導入失敗");
 98             }
 99 
100         } catch (Exception e) {
101             e.printStackTrace();
102             map.put("result", false);
103             map.put("errorMessage", "導入失敗");
104         }
105 
106         return  map;
107 
108     }
109 
110 }

 

 

2.2 service業務層

  1 package com.bjpowernode.excel.importExcel.service.impl;
  2 
  3 import com.bjpowernode.excel.importExcel.domain.Salary;
  4 import com.bjpowernode.excel.importExcel.mapper.SalaryMapper;
  5 import com.bjpowernode.excel.importExcel.service.ImportExcelService;
  6 import org.apache.commons.beanutils.BeanUtils;
  7 import org.apache.ibatis.transaction.Transaction;
  8 import org.apache.poi.ss.usermodel.Cell;
  9 import org.apache.poi.ss.usermodel.Row;
 10 import org.apache.poi.ss.usermodel.Sheet;
 11 import org.apache.poi.ss.usermodel.Workbook;
 12 import org.springframework.beans.factory.annotation.Autowired;
 13 import org.springframework.stereotype.Service;
 14 import org.springframework.transaction.annotation.Transactional;
 15 
 16 import java.io.IOException;
 17 import java.lang.reflect.InvocationTargetException;
 18 import java.util.ArrayList;
 19 import java.util.HashMap;
 20 import java.util.List;
 21 import java.util.Map;
 22 
 23 /**
 24  * ClassName:ImportExcelServiceImpl
 25  * Package:com.bjpowernode.excel.importExcel.service.impl
 26  * Description:
 27  *
 28  * @Date:2019/2/26 21:57
 29  * @Author: 鄭軍
 30  */
 31 @Service
 32 public class ImportExcelServiceImpl implements ImportExcelService {
 33 
 34     @Autowired
 35     private SalaryMapper salaryMapper;
 36 
 37     @Override
 38     @Transactional(rollbackFor = RuntimeException.class)
 39     public Map<String, Object> importExcel(Workbook workbook) throws InvocationTargetException, IllegalAccessException {
 40 
 41         Map<String, Object> resultMap = new HashMap<>();
 42         List<List<Salary>> salaryDateList = new ArrayList<>();
 43         int numberOfSheets = workbook.getNumberOfSheets();
 44 
 45 
 46         for (int i = 0; i < numberOfSheets; i++) {
 47             Sheet sheet = workbook.getSheetAt(i);
 48             // 將sheet頁中的數據,轉換成list集合
 49             List<Salary> salaryList = parseExcelToList(sheet);
 50             // 將每一個sheet頁的數據,然后放在一個list集合中。
 51             salaryDateList.add(salaryList);
 52 
 53         }
 54 
 55         // 按照業務邏輯,將不同sheet頁的數據,插入不同的數據庫表中。這個地方只用了一張表
 56         for (List<Salary> salaryList : salaryDateList) {
 57             salaryMapper.insertExcelData(salaryList);
 58         }
 59 
 60         // 導入成功之后,向resultMap中添加返回成功信息
 61         resultMap.put("result", true);
 62         resultMap.put("errorMessage", "導入成功");
 63 
 64         return resultMap;
 65     }
 66 
 67     /**
 68      * 將sheet中的數據轉換成一個list集合。
 69      *
 70      * @param sheet
 71      * @return
 72      */
 73     private List<Salary> parseExcelToList(Sheet sheet) throws InvocationTargetException, IllegalAccessException {
 74 
 75         // excel導入的時候,對應的數據庫中的字段數組。
 76         String[] property = {"account", "money"};
 77         List<Salary> salaryList = new ArrayList<>();
 78 
 79         int lastRowNum = sheet.getLastRowNum();
 80         // 如果sheet頁的數據為空
 81         if (lastRowNum < 1) {
 82             return null;
 83         }
 84 
 85         for (int i = 1; i < lastRowNum + 1; i++) {
 86 
 87             Map<String, Object> map = new HashMap<>();
 88 
 89             Row row = sheet.getRow(i);
 90             if (row == null) {
 91                 continue;
 92             }
 93 
 94             for (int j = 0; j < property.length; j++) {
 95                 Cell cell = row.getCell(j);
 96                 // 獲取單元格對應的數據值。單元格中的數據對應着可能為日期,字符串,數字
 97                 Object value = getCellValue(cell);
 98                 map.put(property[j], value);
 99             }
100 
101             Salary salary = new Salary();
102             BeanUtils.populate(salary, map);
103             salary.setsId(i + "");
104             salaryList.add(salary);
105 
106         }
107 
108         return salaryList;
109     }
110 
111     /**
112      * 獲取單元格對應的值
113      *
114      * @param cell 單元格對象
115      * @return 單元格對應的值
116      */
117     private Object getCellValue(Cell cell) {
118 
119         int cellType = cell.getCellType();
120         Object cellValue = null;
121 
122         switch (cellType) {
123 
124             // 數值獲取對應的數據
125             case 0:
126                 cellValue = cell.getNumericCellValue();
127                 break;
128 
129             // 字符串獲取去除首位空格
130             case 1:
131                 cellValue = cell.getStringCellValue().trim();
132                 break;
133 
134             case 2:
135                 cell.setCellType(1);
136                 if (cell.getStringCellValue() == null
137                         || cell.getStringCellValue().length() == 0)
138                     break;
139                 cellValue = cell.getStringCellValue().replaceAll("#N/A", "").trim();
140                 cellValue += "&CELL_TYPE_FORMULA";
141                 break;
142 
143             case 3:
144                 cellValue = "";
145                 break;
146 
147             case 4:
148                 cellValue = Boolean.toString(cell.getBooleanCellValue());
149                 break;
150 
151             case 5:
152                 cellValue = "";
153                 break;
154 
155             default:
156                 cellValue = "";
157         }
158 
159         return cellValue;
160 
161     }
162 
163 }
View Code

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM