<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-spring-boot-starter</artifactId>
<version>4.1.2</version>
<exclusions>
<exclusion>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>*</artifactId>
</exclusion>
<exclusion>
<groupId>slf4j-log4j12</groupId>
<artifactId>*</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
</exclusion>
</exclusions>
</dependency>
public void imp(MultipartFile multipartFile ){
//导入参数
ImportParams params = new ImportParams();
//导入
List<SocietyCouncilList> list = ExcelImportUtil.importExcel(multipartFile.getInputStream(), SocietyCouncilList.class, params);
//list就是excel结果
}
实体类:(需要使用@Excel注解,对应的name为excel的表头数据)
import cn.afterturn.easypoi.excel.annotation.Excel;
import cn.afterturn.easypoi.excel.annotation.ExcelEntity;
import cn.afterturn.easypoi.excel.annotation.ExcelTarget;
@ExcelTarget("uploadExcel")
public class SocietyCouncilList extends BaseEntity {
//人员名称
@Excel(name = "姓名")
private String cname;
//性别:0:男,1:女
private int gender;
@Excel(name = "性别")
private String sex;
//民族
@Excel(name = "民族")
private String national;
}
//创建连接,path为文件路径
URI u = URI.create(path);
File fi = null;
//将文件转为流
try (InputStream inputStream = u.toURL().openStream()) {
//新创建个文件
fi = new File(profile + "/" + "人员test.xls");
//将文件流写入新创建的文件中
FileUtils.copyInputStreamToFile(inputStream, fi);
}
//将File文件转为 MultipartFile
MultipartFile file = getMultipartFile(fi);
File转MultipartFile
public static MultipartFile getMultipartFile(File file) {
FileItem item = new DiskFileItemFactory().createItem("file"
, MediaType.MULTIPART_FORM_DATA_VALUE
, true
, file.getName());
try (InputStream input = new FileInputStream(file);
OutputStream os = item.getOutputStream()) {
// 流转移
IOUtils.copy(input, os);
} catch (Exception e) {
throw new IllegalArgumentException("Invalid file: " + e, e);
}
return new CommonsMultipartFile(item);
}