一.pom.xml中導入所需要的依賴:
<!--讀取excel文件-->
//操作03版的Excel,即以.xls結尾的excel表
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>
//操作07版的Excel,即以.xlsx結尾的Excel表
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
二.編寫前端代碼:
//HTML:
<input id="articleImageFile" name="excelFile" type="file" class="form-control" style="width: 300px; display: inline;" /> <a class="btn btn-warning" id = "import" onclick="importExcel()" shiro:hasPermission="system:student:import"> <i class="fa fa-upload"></i> 導入</a>
//js代碼:
<script> function importExcel() { var formData = new FormData(); var name = $("#articleImageFile").val(); formData.append("file", $("#articleImageFile")[0].files[0]); formData.append("name", name);//這個地方可以傳遞多個參數 $.ajax({ url: '/system/student/importExcel', type: 'POST', async: false, data: formData, // 告訴jQuery不要去處理發送的數據 processData: false, // 告訴jQuery不要去設置Content-Type請求頭 contentType: false, beforeSend: function () { console.log("正在進行,請稍候"); }, success: function (responseStr) { if (responseStr == "上傳成功") { alert("導入成功"); } else { alert("導入失敗"); } } }); } </script>
三.controller層代碼:
@RequestMapping("/importExcel")
@ResponseBody
public String importExcel(@RequestParam("file") MultipartFile file, HttpServletRequest request, HttpServletResponse
response) {
System.out.println("file"+file.getSize());
try {
// @RequestParam("file") MultipartFile file 是用來接收前端傳遞過來的文件
// 1.創建workbook對象,讀取整個文檔
InputStream inputStream = file.getInputStream();
//POIFSFileSystem poifsFileSystem = new POIFSFileSystem(inputStream);
XSSFWorkbook wb = new XSSFWorkbook(inputStream);
// 2.讀取頁腳sheet
XSSFSheet sheetAt = wb.getSheetAt(0);
// 3.循環讀取某一行
int index = 0;
for (Row row : sheetAt) {
// 4.讀取每一行的單元格
if (index == 0) {
index++;
continue;
}
//創建一個學生對象
SysStudent student = new SysStudent();
//將Excel表中單元格的值與學生對象的值對應
student.setName(row.getCell(0).getStringCellValue());
//因為學號是數字,Excel默認是數字類型,我的數據庫是字符串類型,所以需要設置下類型
row.getCell(1).setCellType(CellType.STRING);
student.setStuId(row.getCell(1).getStringCellValue());
student.setIdentity(row.getCell(2).getStringCellValue());
student.setDescription(row.getCell(3).getStringCellValue());
student.setProvince(row.getCell(4).getStringCellValue());
//將對應好的學生對象存入數據庫中
sysStudentService.insertSysStudent(student);
row.getCell(0).setCellType(CellType.STRING);
String stringCellValue = row.getCell(0).getStringCellValue();
row.getCell(1).setCellType(CellType.STRING);
String stringCellValue2 = row.getCell(1).getStringCellValue();
row.getCell(2).setCellType(CellType.STRING);
String stringCellValue3 = row.getCell(2).getStringCellValue();
row.getCell(3).setCellType(CellType.STRING);
String stringCellValue4 = row.getCell(3).getStringCellValue();
row.getCell(4).setCellType(CellType.STRING);
String stringCellValue5 = row.getCell(4).getStringCellValue();
// 寫多少個具體看大家上傳的文件有多少列.....
// 后台測試是否讀取到數據,及數據的正確性
System.out.println(stringCellValue);
System.out.println(stringCellValue2);
System.out.println(stringCellValue3);
System.out.println(stringCellValue4);
System.out.println(stringCellValue5);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "上傳成功";
}
