SSM項目導入Excel,話不多說,直接干貨
1:添加maven相關依賴
<dependency> <groupId>org.jxls</groupId> <artifactId>jxls-reader</artifactId> <version>2.0.2</version> </dependency> <dependency> <groupId>org.jxls</groupId> <artifactId>jxls</artifactId> <version>2.4.6</version> </dependency>
2:ExcelHead類:存放Excel表的表頭信息,導入時要完全匹配表頭信息才可導入
1 public class ExcelHead { 2 //讀取表頭 3 private String head; 4 //節點 這里要導入的Excel的表頭必須和這兒完全匹配 5 private String point = "節點編號*,節點名稱*,節點描述,節點類別,倉庫編號*,倉庫名稱*,是否自動接收*,備注"; 6 public String getHead() { 7 return head; 8 } 9 10 public void setHead(String head) { 11 if (StringUtil.isEmpty(this.head)) { 12 this.head = head.trim(); 13 } else { 14 this.head = String.format("%s,%s", this.head, head.trim()); 15 } 16 } 17 18 public String getPoint() { 19 return point; 20 } 21 22 public void setPoint(String point) { 23 this.point = point; 24 } 25 26 }
point-body.xml文件.這里存放的是表體信息
1 <?xml version="1.0" encoding="UTF-8"?> 2 <workbook> 3 <worksheet idx="0"> 4 <!-- 表頭開始至結束行 --> 5 <section startRow="0" endRow="0"/> 6 <!-- 開始循環讀取文件數據,配置開始行,items映射的list var映射的bean varType 類路徑 7 startRow:開始循環的行數 8 endRow-startRow:循環體的大小,0代表一行,依次論推 9 每循環一次,判斷是否結束,不結束繼續循環,直至結束 10 --> 11 <loop startRow="1" endRow="1" items="pointList" var="sku" 12 varType="com.eglsc.ams.domain.vo.PointVo"> 13 <!-- 循環開始行 --> 14 <section startRow="1" endRow="1"> 15 <!-- 節點編號 --> 16 <mapping row="1" col="0" nullAllowed="true"> 17 sku.pointNo 18 </mapping> 19 <!-- 節點名稱 --> 20 <mapping row="1" col="1" nullAllowed="true"> 21 sku.pointName 22 </mapping> 23 <!-- 節點描述 --> 24 <mapping row="1" col="2" nullAllowed="true"> 25 sku.pointDescription 26 </mapping> 27 <!-- 節點類別 --> 28 <mapping row="1" col="3" nullAllowed="true"> 29 sku.pointCategory 30 </mapping> 31 <!-- 倉庫編號 --> 32 <mapping row="1" col="4" nullAllowed="true"> 33 sku.siteNo 34 </mapping> 35 <!-- 倉庫名稱 --> 36 <mapping row="1" col="5" nullAllowed="true"> 37 sku.siteName 38 </mapping> 39 <!-- 是否自動接收 --> 40 <mapping row="1" col="6" nullAllowed="true"> 41 sku.automaticFlag 42 </mapping> 43 <!-- 備注 --> 44 <mapping row="1" col="7" nullAllowed="true"> 45 sku.remark 46 </mapping> 47 </section> 48 <!-- 結束條件配置 --> 49 <loopbreakcondition> 50 <rowcheck offset="0"> 51 <!-- 空白結束不填 --> 52 <cellcheck offset="0"></cellcheck> 53 </rowcheck> 54 </loopbreakcondition> 55 </loop> 56 </worksheet> 57 </workbook>
point-head.xml文件.這里配置的是表頭信息
1 <?xml version="1.0" encoding="UTF-8"?> 2 <workbook> 3 <worksheet idx="0"> 4 <!-- 表頭開始至結束行 --> 5 <section startRow="0" endRow="0"> 6 <!-- 節點編號 --> 7 <mapping row="0" col="0" nullAllowed="true"> 8 excelHead.head 9 </mapping> 10 <!-- 節點名稱 --> 11 <mapping row="0" col="1" nullAllowed="true"> 12 excelHead.head 13 </mapping> 14 <!-- 節點描述 --> 15 <mapping row="0" col="2" nullAllowed="true"> 16 excelHead.head 17 </mapping> 18 <!-- 節點類別 --> 19 <mapping row="0" col="3" nullAllowed="true"> 20 excelHead.head 21 </mapping> 22 <!-- 倉庫編號 --> 23 <mapping row="0" col="4" nullAllowed="true"> 24 excelHead.head 25 </mapping> 26 <!-- 倉庫名稱 --> 27 <mapping row="0" col="5" nullAllowed="true"> 28 excelHead.head 29 </mapping> 30 <!-- 是否自動接收 --> 31 <mapping row="0" col="6" nullAllowed="true"> 32 excelHead.head 33 </mapping> 34 <!--備注--> 35 <mapping row="0" col="7" nullAllowed="true"> 36 excelHead.head 37 </mapping> 38 </section> 39 </worksheet> 40 </workbook>
導入代碼:
2 @RequestMapping(value = "/import", method = RequestMethod.POST)
@Response 3 @Transactional 4 public Response importExcel(@RequestParam(required = true) MultipartFile file, HttpServletRequest request) { 5 String fileName = file.getOriginalFilename().toLowerCase(); 6 if(!fileName.endsWith(".xls") && !fileName.endsWith(".xlsx")){ 7 throw new ApplicationException("請上傳Excel文件"); 8 } 9 //獲取文件 10 CommonsMultipartFile commonsMultipartFile = (CommonsMultipartFile)file; 11 //獲取xml文件頭配置(我的xml文件是放在/template/import/point目錄下的) 12 String xmlConfigPath = String.format("%s%s", request.getServletContext().getRealPath("/"), "/template/import/point/"); 13 String xmlConfigName = String.format("%s%s", xmlConfigPath, "point-head.xml"); 14 Map<String,Object> beans = new HashMap<>(); 15 ExcelHead excelHead = new ExcelHead(); 16 beans.put("excelHead",excelHead); 17 //解析文件頭 18 this.parseExcelService.parseExcel(xmlConfigName,commonsMultipartFile,beans); 19 if(!excelHead.getPoint().equals(excelHead.getHead())){ 20 throw new RuntimeException("模板錯誤"); 21 } 22 //獲取文件體信息 23 xmlConfigName = String.format("%s%s", xmlConfigPath, "point-body.xml"); 24 List<PointVo> pointVoList = new ArrayList<>(); 25 beans.clear(); 26 beans.put("pointList",pointVoList); 27 //解析文件體 28 parseExcelService.parseExcel(xmlConfigName,commonsMultipartFile,beans); 29 this.pointService.importExcel(pointVoList); 30 return Response.success("導入成功"); 31 }
Response類攜帶着操作結束后的信息,供前端人員調用
執行到this.pointService.importExcel(pointVoList)這段代碼意味着要導入的Excel中的數據已經放到pointList中了,執行pointService中的方法把數據批量導入進數據庫中就完成了.
解析Excel工具類:ParseExcelService
public class ParseExcelService { /** * 解析Excel * * @param xmlConfigName xml配置(字段和Excel單元格的映射) * @param commonsMultipartFile 上傳的文件 * @param beans 解析后的數據 */ public void parseExcel(String xmlConfigName, CommonsMultipartFile commonsMultipartFile, Map<String, Object> beans) { InputStream inputStream = null; FileInputStream xmlConfig = null; InputStream inputXML = null; InputStream inputXLS = null; try { //上傳的文件流 inputStream = commonsMultipartFile.getInputStream(); //xml配置的文件流 xmlConfig = new FileInputStream(xmlConfigName); //執行解析 inputXML = new BufferedInputStream(xmlConfig); XLSReader mainReader = ReaderBuilder.buildFromXML(inputXML); inputXLS = new BufferedInputStream(inputStream); mainReader.read(inputXLS, beans); } catch (XLSDataReadException e) { logger.error("數據轉換異常:", e); throw new ApplicationException("數據轉換異常"); } catch (InvalidFormatException e) { logger.error("無效格式異常:", e); throw new ApplicationException("無效格式異常"); } catch (FileNotFoundException e) { logger.error("文件未找到:", e); throw new ApplicationException("文件未找到"); } catch (SAXException e) { logger.error("SAX解析異常:", e); throw new ApplicationException("SAX解析異常"); } catch (IOException e) { logger.error("IO流異常:", e); throw new ApplicationException("IO流異常"); } finally { try { if (inputXLS != null) inputXLS.close(); if (inputXML != null) inputXML.close(); if (xmlConfig != null) xmlConfig.close(); if (inputStream != null) inputStream.close(); } catch (IOException e) { logger.error("parse excel error", e); } } } }