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

优化

parent 7d929c28
...@@ -9,6 +9,7 @@ import org.springframework.web.bind.annotation.*; ...@@ -9,6 +9,7 @@ import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.io.IOException;
import java.util.List; import java.util.List;
/** /**
...@@ -29,7 +30,7 @@ public class AnalysisController { ...@@ -29,7 +30,7 @@ public class AnalysisController {
@PostMapping("/canvas") @PostMapping("/canvas")
public Result<?> canvas(@RequestParam(value = "files") List<MultipartFile> files, public Result<?> canvas(@RequestParam(value = "files") List<MultipartFile> files,
@RequestParam(value = "caseId") int caseId, @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); return analysisService.canvas(files,caseId,folderName);
} }
......
...@@ -4,6 +4,7 @@ import com.platform.model.resp.Result; ...@@ -4,6 +4,7 @@ import com.platform.model.resp.Result;
import com.platform.model.vo.TableHeaderVO; import com.platform.model.vo.TableHeaderVO;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.List; import java.util.List;
/** /**
...@@ -19,7 +20,7 @@ public interface AnalysisService { ...@@ -19,7 +20,7 @@ public interface AnalysisService {
* @param files * @param files
* @return * @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 { ...@@ -61,7 +61,7 @@ public class AnalysisServiceImpl implements AnalysisService {
@Override @Override
@Transactional @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(); ExcelTemplateEntity ExcelTemplate = excelTemplateService.getMaxId();
List<TableHeaderVO> tableHeaderList; List<TableHeaderVO> tableHeaderList;
...@@ -111,14 +111,16 @@ public class AnalysisServiceImpl implements AnalysisService { ...@@ -111,14 +111,16 @@ public class AnalysisServiceImpl implements AnalysisService {
} catch (IOException e) { } catch (IOException e) {
throw new BusinessException("文件转换错误"); throw new BusinessException("文件转换错误");
} }
try { boolean b = processSingleExcelFile(tempFile, tableHeaderList, folderName, fileUploadVO.getId(), caseId, groupedMap, excelData);
processSingleExcelFile(tempFile, tableHeaderList, folderName, fileUploadVO.getId(), caseId, groupedMap, excelData); if (b) {
} catch (IOException e) { //文件上传
throw new BusinessException("文件解析错误"); fileUploadList.add(fileUploadVO);
totalCount++;
} else {
errorCount++;
totalCount++;
detail.add(AnalysisVO.builder().folderName(folderName).fileName(file.getOriginalFilename()).errorReason("文件格式错误").build());
} }
//文件上传
fileUploadList.add(fileUploadVO);
totalCount++;
} else { } else {
errorCount++; errorCount++;
totalCount++; totalCount++;
...@@ -401,9 +403,13 @@ public class AnalysisServiceImpl implements AnalysisService { ...@@ -401,9 +403,13 @@ public class AnalysisServiceImpl implements AnalysisService {
return false; 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 sheet = getWorkbook(excelFile).getSheetAt(0); Sheet sheet = workbook.getSheetAt(0);
//获取sheet长度 //获取sheet长度
int sheetLength = sheet.getLastRowNum(); int sheetLength = sheet.getLastRowNum();
for (int i = 1; i < sheetLength; i++) { for (int i = 1; i < sheetLength; i++) {
...@@ -424,24 +430,25 @@ public class AnalysisServiceImpl implements AnalysisService { ...@@ -424,24 +430,25 @@ public class AnalysisServiceImpl implements AnalysisService {
} }
} }
} }
return true;
} }
// 根据文件类型创建Workbook // 根据文件类型创建Workbook
private Workbook getWorkbook(File excelFile) throws IOException { private Workbook getWorkbook(File excelFile) {
String fileName = excelFile.getName().toLowerCase(); String fileName = excelFile.getName().toLowerCase();
if (fileName.endsWith(".xlsx")) { if (fileName.endsWith(".xlsx")) {
try { try {
return new XSSFWorkbook(new FileInputStream(excelFile)); return new XSSFWorkbook(new FileInputStream(excelFile));
} catch (Exception e) { } catch (Exception e) {
return new HSSFWorkbook(new FileInputStream(excelFile)); return null;
} }
} else if (fileName.endsWith(".xls")) { } else if (fileName.endsWith(".xls")) {
try { try {
return new HSSFWorkbook(new FileInputStream(excelFile)); return new HSSFWorkbook(new FileInputStream(excelFile));
} catch (Exception e) { } catch (Exception e) {
return new XSSFWorkbook(new FileInputStream(excelFile)); return null;
} }
} else { } else {
throw new IllegalArgumentException("不支持的Excel文件格式"); 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