<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.16-beta1</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.16-beta1</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.16-beta1</version>
</dependency>
<dependency>
<groupId>org.apache.xmlbeans</groupId>
<artifactId>xmlbeans</artifactId>
<version>3.1.0</version>
</dependency>
只适合2007以下版本:
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.9</version>
</dependency>
package com.king.king.admin.util;
import com.king.king.admin.model.entity.School;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Administrator on 2019/3/21.
* 报表导入
*/
@Slf4j
public class ImportDataUtil {
public static final String excel_new = "xlsx";//新版本
public static final String excel_old = "xls";//旧版本
/**
* 执行导入
*
* @param file
* @return
*/
public List<School> importSchool(MultipartFile file) {
List<School> tableList = new ArrayList<>();
InputStream inputStream = null;
Workbook workbook;
String path = file.getOriginalFilename();
try {
inputStream = file.getInputStream();
if (isPathName(path)) {
workbook = new XSSFWorkbook(inputStream);
} else {
workbook = new HSSFWorkbook(inputStream);
}
Sheet sheet = workbook.getSheetAt(0);
for (int i = 0; i < sheet.getLastRowNum(); i++) {
School school = new School();
Row row = sheet.getRow(i);
school.setClassName(row.getCell(1).getStringCellValue());
school.setCourseName(row.getCell(2).getStringCellValue());
school.setClassroom(row.getCell(3).getStringCellValue());
school.setNote(row.getCell(4).getStringCellValue());
school.setTeacherName(row.getCell(5).getStringCellValue());
tableList.add(school);
}
} catch (IOException e) {
log.info("workbook", e);
e.printStackTrace();
return null;
} finally {
try {
inputStream.close();
} catch (IOException e) {
log.info("inputStream.close()", e);
e.printStackTrace();
return null;
}
}
return tableList;
}
public Boolean isPathName(String path) {
Integer index = path.lastIndexOf(".");
String name = path.substring(index + 1);
if (name.equals(excel_new)) {
return true;
} else {
return false;
}
}
}
import org.springframework.web.multipart.MultipartFile;
/**
* 报表导入
*
* @return
*/
@PostMapping("/importSchool")
public R<Boolean> importSchoolData(@RequestParam("file") MultipartFile file) {
if (file == null) {
return new R<>(false);
}
try {
List<School> tableList = new ImportDataUtil().importSchoolTime(file);
System.out.println(tableList.toString());
if (tableList.size()>0) {
schoolService.insertBatch(tableList);
}
} catch (Exception e) {
log.info("error", e);
return new R<>(false);
}
return new R<>(true);
}