Hutool解析excel导入数据
本人是 hutool 的狂热粉丝~~~
话不多说,上代码!!!
hutool依赖,这里暂不赘述,,官网找即可。
/**
* 导入样本事故 controller
* @param file
*/
@PostMapping("/importTemplate")
@ApiOperation(value = "导入样本事故")
public R importTemplate(MultipartFile file) {
return new R(causeModelSampleService.importTemplate(file));
}
//service业务代码
@Override
public R importTemplate(MultipartFile file) {
InputStream inputStream = null;
try {
inputStream = file.getInputStream();
}catch (Exception e){
return new R(e.getMessage());
}
ExcelReader reader = ExcelUtil.getReader(inputStream, 0); //指定输入流和sheet
// 读取第二行到最后一行数据
List<List<Object>> read = reader.read(1, reader.getRowCount());
List<CauseModelSample> samples = new ArrayList<>();
for (List<Object> objects : read) {
CauseModelSample causeModelSample = new CauseModelSample();
Object sampleNo = objects.get(0);//读取某行第一列数据
Object sampleName = objects.get(1); //读取某行第二列数据
Object sampleLevel = objects.get(2); //读取某行第三列数据
Object accidentId = objects.get(3); //读取某行第四列数据
Object industryTypeId = objects.get(4); //读取某行第五列数据
Object corpScale = objects.get(5); //读取某行第六列数据
Object address = objects.get(6); //读取某行第七列数据
causeModelSample.setSampleNo(sampleNo.toString()); //样本编号
causeModelSample.setSampleName(sampleName.toString()); //样本名称
causeModelSample.setSampleLevel(sampleLevel.toString()); //样本事故级别
causeModelSample.setBelongAccidentId(Long.parseLong(accidentId.toString()));
causeModelSample.setBelongIndustryTypeId(Long.parseLong(industryTypeId.toString()));
causeModelSample.setCorpScale(corpScale.toString()); //企业规模
causeModelSample.setAddress(address.toString()); //详细地址
causeModelSample.setIsSample(SemConstants.PS_NO_SAMPLE); //导入的 不是样本N
causeModelSample.setCauseNetworkStatus(CauseNetworkEnum.TEMPORARY_STORAGE.value); //导入为暂存状态
samples.add(causeModelSample);
}
return new R(causeModelSampleManager.saveBatch(samples));
}