Commit ef8c067c authored by wujipeng's avatar wujipeng
Browse files

解析画布边文件上传excel内容修改为可以上传多个文件

parent 0d623f92
...@@ -25,11 +25,11 @@ public class AnalysisController { ...@@ -25,11 +25,11 @@ public class AnalysisController {
@Resource @Resource
private AnalysisService analysisService; private AnalysisService analysisService;
@ApiOperation("解析画布边文件上传excel或文件夹") @ApiOperation("解析画布边文件上传excel内容")
@PostMapping("/canvas") @PostMapping("/canvas")
public Result<?> canvas(@RequestParam(value = "file") MultipartFile file, public Result<?> canvas(@RequestParam(value = "files") MultipartFile[] files,
@RequestParam(value = "templateId") Integer templateId) { @RequestParam(value = "templateId") Integer templateId) {
return analysisService.canvas(file,templateId); return analysisService.canvas(files,templateId);
} }
@ApiOperation("解析模板表头") @ApiOperation("解析模板表头")
......
...@@ -16,10 +16,10 @@ public interface AnalysisService { ...@@ -16,10 +16,10 @@ public interface AnalysisService {
/** /**
* 读取excel文件 * 读取excel文件
* @param file * @param files
* @return * @return
*/ */
Result<?> canvas(MultipartFile file,Integer templateId); Result<?> canvas(MultipartFile[] files,Integer templateId);
/** /**
* 读取模板文件 * 读取模板文件
......
...@@ -49,49 +49,28 @@ public class AnalysisServiceImpl implements AnalysisService { ...@@ -49,49 +49,28 @@ public class AnalysisServiceImpl implements AnalysisService {
private static final String[] EXCEL_EXTENSIONS = {".xlsx", ".xls"}; private static final String[] EXCEL_EXTENSIONS = {".xlsx", ".xls"};
@Override @Override
public Result<?> canvas(MultipartFile file, Integer templateId) { public Result<?> canvas(MultipartFile[] files, Integer templateId) {
//查询模板 //查询模板
ExcelTemplateEntity ExcelTemplate = excelTemplateService.getById(templateId); ExcelTemplateEntity ExcelTemplate = excelTemplateService.getById(templateId);
if(ObjectUtils.isEmpty(ExcelTemplate)){ if(ObjectUtils.isEmpty(ExcelTemplate)){
return Result.fail("模板不存在"); return Result.fail("模板不存在");
} }
try { try {
if (file.isEmpty()) { if (files.length == 0) {
return Result.fail("上传的文件为空"); return Result.fail("上传的文件为空");
} }
// 检查是否是Excel文件 List<FileUploadVO> fileUploadList = com.google.common.collect.Lists.newArrayList();
else if (isExcelFile(file.getOriginalFilename())) { for (MultipartFile file : files) {
File tempFile = convertMultipartFileToFile(file); // 检查是否是Excel文件
processSingleExcelFile(tempFile,ExcelTemplate); if (isExcelFile(file.getOriginalFilename())) {
//文件上传 File tempFile = convertMultipartFileToFile(file);
FileUploadVO fileUploadVO = fileUploadService.uploadFile(file); processSingleExcelFile(tempFile,ExcelTemplate);
return Result.data(fileUploadVO); //文件上传
} FileUploadVO fileUploadVO = fileUploadService.uploadFile(file);
// 检查是否是目录(通过检查是否没有扩展名) fileUploadList.add(fileUploadVO);
else if (isLikelyDirectory(file.getOriginalFilename())) {
// 保存上传的临时文件
Path tempDir = createTempDirectory();
Path uploadedFile = tempDir.resolve(file.getOriginalFilename());
Files.copy(file.getInputStream(), uploadedFile, StandardCopyOption.REPLACE_EXISTING);
// 处理目录
List<File> excelFiles = new ArrayList<>();
if (Files.isDirectory(uploadedFile)) {
// 如果是目录,遍历其中的文件
Files.walk(uploadedFile)
.filter(Files::isRegularFile)
.filter(path -> isExcelFile(path.getFileName().toString()))
.forEach(path -> excelFiles.add(path.toFile()));
} else {
// 如果是文件,检查是否是Excel文件
if (isExcelFile(uploadedFile.getFileName().toString())) {
excelFiles.add(uploadedFile.toFile());
}
} }
return Result.data(processExcelFiles(excelFiles));
} else {
return Result.fail("不支持的文件类型,请上传Excel文件(.xlsx, .xls)或包含Excel文件的ZIP文件夹");
} }
return Result.data(fileUploadList);
} catch (Exception e) { } catch (Exception e) {
return Result.fail("文件处理失败: " + e.getMessage()); return Result.fail("文件处理失败: " + e.getMessage());
} }
...@@ -128,17 +107,6 @@ public class AnalysisServiceImpl implements AnalysisService { ...@@ -128,17 +107,6 @@ public class AnalysisServiceImpl implements AnalysisService {
return headers; return headers;
} }
private boolean isLikelyDirectory(String filename) {
if (filename == null) return false;
String lowerCaseFilename = filename.toLowerCase();
for (String extension : EXCEL_EXTENSIONS) {
if (lowerCaseFilename.endsWith(extension)) {
return false;
}
}
return !lowerCaseFilename.contains(".");
}
// 检查是否是Excel文件 // 检查是否是Excel文件
private boolean isExcelFile(String filename) { private boolean isExcelFile(String filename) {
if (filename == null) return false; if (filename == null) return false;
...@@ -151,30 +119,6 @@ public class AnalysisServiceImpl implements AnalysisService { ...@@ -151,30 +119,6 @@ public class AnalysisServiceImpl implements AnalysisService {
return false; return false;
} }
// 处理多个Excel文件
private String processExcelFiles(List<File> excelFiles) throws IOException {
if (excelFiles.isEmpty()) {
return "未找到任何Excel文件";
}
StringBuilder result = new StringBuilder();
result.append("共找到 ").append(excelFiles.size()).append(" 个Excel文件:\n");
for (File excelFile : excelFiles) {
result.append("处理文件: ").append(excelFile.getName()).append("\n");
String fileContent = readExcelFile(excelFile);
result.append(fileContent).append("\n\n");
// 删除临时文件
if (!excelFile.delete()) {
excelFile.deleteOnExit();
}
}
return result.toString();
}
// 处理单个Excel文件
private void processSingleExcelFile(File excelFile,ExcelTemplateEntity ExcelTemplate) throws IOException { private void processSingleExcelFile(File excelFile,ExcelTemplateEntity ExcelTemplate) throws IOException {
String json = ExcelTemplate.getJson(); String json = ExcelTemplate.getJson();
List<TableHeaderVO> tableHeaderList = JSON.parseArray(json, TableHeaderVO.class); List<TableHeaderVO> tableHeaderList = JSON.parseArray(json, TableHeaderVO.class);
...@@ -251,28 +195,6 @@ public class AnalysisServiceImpl implements AnalysisService { ...@@ -251,28 +195,6 @@ public class AnalysisServiceImpl implements AnalysisService {
excelData.add(excelDataEntity); excelData.add(excelDataEntity);
} }
// 读取Excel文件内容
private String readExcelFile(File excelFile) throws IOException {
StringBuilder content = new StringBuilder();
try (Workbook workbook = getWorkbook(excelFile)) {
for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
Sheet sheet = workbook.getSheetAt(i);
content.append("工作表: ").append(sheet.getSheetName()).append("\n");
for (Row row : sheet) {
for (Cell cell : row) {
content.append(getCellValueAsString(cell)).append("\t");
}
content.append("\n");
}
content.append("\n");
}
}
return content.toString();
}
// 根据文件类型创建Workbook // 根据文件类型创建Workbook
private Workbook getWorkbook(File excelFile) throws IOException { private Workbook getWorkbook(File excelFile) throws IOException {
String fileName = excelFile.getName().toLowerCase(); String fileName = excelFile.getName().toLowerCase();
...@@ -312,10 +234,6 @@ public class AnalysisServiceImpl implements AnalysisService { ...@@ -312,10 +234,6 @@ public class AnalysisServiceImpl implements AnalysisService {
} }
} }
// 创建临时目录
private Path createTempDirectory() throws IOException {
return Files.createTempDirectory("excel-upload-");
}
// 将MultipartFile转换为File // 将MultipartFile转换为File
private File convertMultipartFileToFile(MultipartFile file) throws IOException { private File convertMultipartFileToFile(MultipartFile file) throws IOException {
......
...@@ -2,6 +2,7 @@ package com.platform.service.impl; ...@@ -2,6 +2,7 @@ package com.platform.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.platform.exception.BusinessException; import com.platform.exception.BusinessException;
import com.platform.mapper.FileUploadMapper; import com.platform.mapper.FileUploadMapper;
import com.platform.model.entity.FileUploadEntity; import com.platform.model.entity.FileUploadEntity;
...@@ -9,6 +10,7 @@ import com.platform.model.vo.FileUploadVO; ...@@ -9,6 +10,7 @@ import com.platform.model.vo.FileUploadVO;
import com.platform.service.FileUploadService; import com.platform.service.FileUploadService;
import com.platform.utils.SecurityUtils; import com.platform.utils.SecurityUtils;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
...@@ -17,7 +19,9 @@ import java.io.IOException; ...@@ -17,7 +19,9 @@ import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List;
/** /**
* @author wujipeng * @author wujipeng
......
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