프런트 엔드 :
<li class = "btn_uploads"> <input type = "file"id = "wjsc"οnchange = 'importf ()'> <i class = "Hui-iconfont"> & # xe645; </ i> & nbsp; 导入 < / li>
js :
// 导入
function importf () { var txt = document.getElementById ( "wjsc"). value.substr ( document.getElementById ( "wjsc"). value.lastIndexOf ( ".")) .toLowerCase (); if (txt! = '.xls'&& txt! = '.xlsx') { layer.alert ( "文件 只能 是 xls 文件 或者 是 xlsx 文件 !!!"); this.value = ""; 거짓 반환; } else { var formData = new FormData (); formData.append ( "file", $ ( "# wjsc") [0] .files [0]); $ .ajax ({ url : "/ addproduct / lead", 유형 : "post",
// Content-Type 요청 헤더를 설정하지 않도록 jQuery에 알립니다
. contentType : false,
data : formData,
success : function (result) { if (result.code == "1") { layer.alert ( "import successful") $ ( "#isbn"). val (result.data) } else { layer.alert ( "가져 오기 실패") } } }); }
제어 장치
@RequestMapping (value = "/ lead")
@ResponseBody
public Map <String, Object> readXlsx (@RequestParam ( "file") MultipartFile file) 예외 발생 { try { InputStream inputStream = file.getInputStream (); POIFSFileSystem poifsFileSystem = new POIFSFileSystem (inputStream); 통합 문서 통합 문서 = WorkbookFactory.create (poifsFileSystem); 시트 hssfSheet = workbook.getSheetAt (0); // 축소 시트 StringBuilder isbns = new StringBuilder ();
for (int rowNum = 1; rowNum <= hssfSheet.getLastRowNum (); rowNum ++) { 행 xssfRow = hssfSheet.getRow (rowNum); int minColIx = xssfRow.getFirstCellNum (); int maxColIx = xssfRow.getLastCellNum ();
for (int colIx = minColIx; colIx <maxColIx; colIx ++) { Cell cell = xssfRow.getCell (colIx); if (cell == null) { 계속; } isbns = isbns.append (cell.toString () + ","); } } String sb = isbns.toString (). substring (0, isbns.toString (). length ()-1);
return getResultObjectData (ResultCode.SUCCESS, sb, "수입 정보를 성공적으로 획득했습니다!");
} catch (예외 e) { return getResultObjectData (ResultCode.FAILED, null, "가져 오기 정보를 가져 오지 못했습니다!"); } }
pom.xml
<!-excel 表格 处理->
<dependency>
<groupId> org.apache.poi </ groupId>
<artifactId> poi </ artifactId>
<version> 3.9 </ version>
</ dependency>
<dependency>
<groupId> org.apache.poi </ groupId>
<artifactId> poi-ooxml </ artifactId>
<버전> 3.9 </ version>
</ dependency>
<dependency>
<groupId> javax.activation </ groupId>
<artifactId> 활성화 </ artifactId>
<version> 1.1.1 </ version>
</ dependency>
<dependency>
<groupId> javax.mail </ groupId>
<artifactId> mail </ artifactId>
<version> 1.4.7 < / version>
</ dependency>
<dependency>
<groupId> com.sun.mail </ groupId>
<artifactId> smtp </ artifactId>
<version> 1.6.0 </ version>
</ dependency>
<dependency>
<groupId> net.sourceforge.jexcelapi </ groupId>
<artifactId> jxl </ artifactId>
<version> 2.6.10 </ version>
</ dependency >
효과: