public ResponseData readExcel(MultipartFile file) throws Exception {
if (null == file) {
throw new ServiceException(FILE_NOT_FOUND);
}
Sheet sheet = new Sheet(1, 1, ClanExcelVo.class);
InputStream fileInputStream = new FileInputStream(multipartFileToFile(file));
List<Object> read1 = EasyExcelFactory.read(fileInputStream, sheet);
// 存 ExcelMode 实体的 集合
List<ClanExcelVo> list = new ArrayList<ClanExcelVo>();
for (Object o : read1) {
list.add((ClanExcelVo) o);
}
return ResponseData.success(list);
}
下面是将MultipartFile转file
public static File multipartFileToFile(MultipartFile file) throws Exception {
File toFile = null;
if (file.equals("") || file.getSize() <= 0) {
file = null;
} else {
InputStream ins = null;
ins = file.getInputStream();
toFile = new File(file.getOriginalFilename());
inputStreamToFile(ins, toFile);
ins.close();
}
return toFile;
}
public static void inputStreamToFile(InputStream ins, File file) {
try {
OutputStream os = new FileOutputStream(file);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
os.write(buffer, 0, bytesRead);
}
os.close();
ins.close();
} catch (Exception e) {
e.printStackTrace();
}
}