CSV文件导入到数据库中读取数据详解(接着上个帖子)

时间:2022-08-12 04:13:25

一、controller层

CSV文件导入到数据库中读取数据详解(接着上个帖子)

二、SERVICE层

@Override
public Result importJinjiangAssessResult(MultipartFile file) throws Exception {
Result result = new Result();
//1.判断获取文件是否为空
if (ValidateUtil.isEmpty(file) || file.isEmpty()) {
return ErrorEnum.OBJECT_LIST_ISNULL.getResult("丁博士上传文件");
}
String filename=file.getOriginalFilename();
filename =filename.substring(filename.lastIndexOf(".")+1);
if (!"csv".equals(filename)){
result.setCode(1);
result.setMsg("非csv文件,请检查!");
return result;
} //读取csv文件
BufferedReader br = new BufferedReader(new InputStreamReader(file.getInputStream(), "GBK"));
CSVParser parser = CSVFormat.EXCEL.withHeader("_ApplyNo", "_SurveyedName2", "QualityIndexGrade3", "EvaluatedGrade_method1").parse(br);
List<CSVRecord> csvRecords = parser.getRecords();
int size = csvRecords.size();
if (size > 1) {
for (int i = 1; i < size; i++) {
String applyNo = csvRecords.get(i).get("_ApplyNo");//申请编号
String assessResult = csvRecords.get(i).get("EvaluatedGrade_method1");//评定返回结果
if (ValidateUtil.isEmpty(applyNo)) {
result.setCode(1);
result.setMsg("申请编号为空,请检查数据!");
return result;
} if (ValidateUtil.isEmpty(assessResult)) {
result.setCode(1);
result.setMsg("丁博士导入返回结果为空,请检查数据!");
return result;
}
Integer adviseAssessLevel=null; }
} else {
result.setCode(1);
result.setMsg("不能上传无数据的文件,请检查!");
return result;
}
return result.ok();
}
三、按照上面的读取就可以了