@RequestMapping("/classify")
public void classify(@RequestParam MultipartFile file) throws IOException {
InputStream inputStream = null;
List<ClassifyExcelVO> classifyVOS = new ArrayList<>();
try {
inputStream = file.getInputStream();
classifyVOS = EasyExcelUtil.read(inputStream, ClassifyExcelVO.class);
LOGGER.info("解析数据:"+classifyVOS);
} catch (IOException e) {
log.error("文件错误!");
} finally {
if (inputStream != null) {
inputStream.close();
}
}
}
package com.oppo.ipd.pdm.standard.listener;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import java.util.ArrayList;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class DataListener<T> extends AnalysisEventListener<T> {
private static final Logger LOGGER = LoggerFactory.getLogger(DataListener.class);
private final List<T> rows = new ArrayList<>();
@Override
public void invoke(T t, AnalysisContext analysisContext) {
rows.add(t);
}
@Override
public void doAfterAllAnalysed(AnalysisContext context) {
LOGGER.info("解析完成!读取{}行", rows.size());
}
public List<T> getRows() {
return rows;
}
}
**
* 读取Excel数据
*/
public class EasyExcelUtil {
public static <T> List<T> read(InputStream inputStream, final Class<?> clazz) {
if (inputStream == null) {
throw new RuntimeException("解析出错了,文件流是null");
}
DataListener<T> listener = new DataListener<T>();
EasyExcel.read(inputStream,clazz,listener).sheet().doRead();
return listener.getRows();
}
}
@Data
public class ImportFileNewsVO implements Serializable {
@ExcelProperty(value = "文件类型",index = 0)
private String fileType;
@ExcelProperty(value = "名称",index = 1)
private String name;
@ExcelProperty(value = "简介",index = 2)
private String introduction;
@ExcelProperty(value = "文件类容",index = 3)
private String files;
@ExcelProperty(value = "归档位置",index = 4)
private String location;
@ExcelProperty(value = "适用产品类型",index = 6)
private String applyProductType;
@ExcelProperty(value = "是否限制限制权限",index = 7)
private String isLimitDownloadAuth;
@ExcelProperty(value = "下载人员名单",index = 8)
private String downloadPersonnelList;
@ExcelProperty(value = "版本说明",index = 9)
private String versionInstruction;
}