Commit 92e7440e authored by wujipeng's avatar wujipeng
Browse files

优化

parent 7d929c28
......@@ -9,6 +9,7 @@ import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
/**
......@@ -29,7 +30,7 @@ public class AnalysisController {
@PostMapping("/canvas")
public Result<?> canvas(@RequestParam(value = "files") List<MultipartFile> files,
@RequestParam(value = "caseId") int caseId,
@RequestParam(value = "folderName", required = false) String folderName) {
@RequestParam(value = "folderName", required = false) String folderName) throws IOException {
return analysisService.canvas(files,caseId,folderName);
}
......
......@@ -4,6 +4,7 @@ import com.platform.model.resp.Result;
import com.platform.model.vo.TableHeaderVO;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.List;
/**
......@@ -19,7 +20,7 @@ public interface AnalysisService {
* @param files
* @return
*/
Result<?> canvas(List<MultipartFile> files,int caseId,String folderName);
Result<?> canvas(List<MultipartFile> files,int caseId,String folderName) throws IOException;
/**
* 读取模板文件
......
......@@ -61,7 +61,7 @@ public class AnalysisServiceImpl implements AnalysisService {
@Override
@Transactional
public Result<?> canvas(List<MultipartFile> files, int caseId, String folderName) {
public Result<?> canvas(List<MultipartFile> files, int caseId, String folderName) throws IOException {
//查询模板
ExcelTemplateEntity ExcelTemplate = excelTemplateService.getMaxId();
List<TableHeaderVO> tableHeaderList;
......@@ -111,11 +111,8 @@ public class AnalysisServiceImpl implements AnalysisService {
} catch (IOException e) {
throw new BusinessException("文件转换错误");
}
try {
processSingleExcelFile(tempFile, tableHeaderList, folderName, fileUploadVO.getId(), caseId, groupedMap, excelData);
} catch (IOException e) {
throw new BusinessException("文件解析错误");
}
boolean b = processSingleExcelFile(tempFile, tableHeaderList, folderName, fileUploadVO.getId(), caseId, groupedMap, excelData);
if (b) {
//文件上传
fileUploadList.add(fileUploadVO);
totalCount++;
......@@ -124,6 +121,11 @@ public class AnalysisServiceImpl implements AnalysisService {
totalCount++;
detail.add(AnalysisVO.builder().folderName(folderName).fileName(file.getOriginalFilename()).errorReason("文件格式错误").build());
}
} else {
errorCount++;
totalCount++;
detail.add(AnalysisVO.builder().folderName(folderName).fileName(file.getOriginalFilename()).errorReason("文件格式错误").build());
}
}
//把tableHeaderList中filter为true索引组成一个数字类型的集合
List<Integer> filterIndex = Lists.newArrayList();
......@@ -401,9 +403,13 @@ public class AnalysisServiceImpl implements AnalysisService {
return false;
}
private void processSingleExcelFile(File excelFile, List<TableHeaderVO> tableHeaderList, String folderName, String fileId, int caseId, Map<String, String> groupedMap, List<ExcelDataEntity> excelData) throws IOException {
private boolean processSingleExcelFile(File excelFile, List<TableHeaderVO> tableHeaderList, String folderName, String fileId, int caseId, Map<String, String> groupedMap, List<ExcelDataEntity> excelData) throws IOException {
Workbook workbook = getWorkbook(excelFile);
if (ObjectUtils.isEmpty(workbook)) {
return false;
}
//读取第一个sheet
Sheet sheet = getWorkbook(excelFile).getSheetAt(0);
Sheet sheet = workbook.getSheetAt(0);
//获取sheet长度
int sheetLength = sheet.getLastRowNum();
for (int i = 1; i < sheetLength; i++) {
......@@ -424,24 +430,25 @@ public class AnalysisServiceImpl implements AnalysisService {
}
}
}
return true;
}
// 根据文件类型创建Workbook
private Workbook getWorkbook(File excelFile) throws IOException {
private Workbook getWorkbook(File excelFile) {
String fileName = excelFile.getName().toLowerCase();
if (fileName.endsWith(".xlsx")) {
try {
return new XSSFWorkbook(new FileInputStream(excelFile));
} catch (Exception e) {
return new HSSFWorkbook(new FileInputStream(excelFile));
return null;
}
} else if (fileName.endsWith(".xls")) {
try {
return new HSSFWorkbook(new FileInputStream(excelFile));
} catch (Exception e) {
return new XSSFWorkbook(new FileInputStream(excelFile));
return null;
}
} else {
throw new IllegalArgumentException("不支持的Excel文件格式");
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment