1. 程式人生 > 實用技巧 >CSV檔案匯入到資料庫中讀取資料詳解(接著上個帖子)

CSV檔案匯入到資料庫中讀取資料詳解(接著上個帖子)

一、controller層

二、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();
}
三、按照上面的讀取就可以了