Commit 83bd73b9 authored by wujipeng's avatar wujipeng
Browse files

解析模板表头

parent d1c43361
package com.platform.controller;
import com.platform.model.resp.Result;
import com.platform.model.vo.TableHeaderVO;
import com.platform.service.AnalysisService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
......@@ -8,6 +9,7 @@ import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.util.List;
/**
* @author : wujipeng
......@@ -29,4 +31,9 @@ public class AnalysisController {
return Result.data(analysisService.canvas(file));
}
@ApiOperation("解析模板表头")
@PostMapping("/template")
public Result<List<TableHeaderVO>> template(@RequestParam(value = "file", required = false) MultipartFile file) {
return analysisService.template(file);
}
}
package com.platform.model.vo;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @author : wujipeng
* @date : 2025/7/30 14:42
* @Version: 1.0
* @Desc : 描述信息
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class TableHeaderVO {
private String name;
private Integer index;
}
package com.platform.service;
import com.platform.model.resp.Result;
import com.platform.model.vo.TableHeaderVO;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;
/**
* @author : wujipeng
* @date : 2025/7/30 11:47
......@@ -16,4 +20,11 @@ public interface AnalysisService {
* @return
*/
Object canvas(MultipartFile file);
/**
* 读取模板文件
* @param file
* @return
*/
Result<List<TableHeaderVO>> template(MultipartFile file);
}
package com.platform.service.impl;
import com.platform.model.resp.Result;
import com.platform.model.vo.TableHeaderVO;
import com.platform.service.AnalysisService;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
......@@ -70,8 +72,7 @@ public class AnalysisServiceImpl implements AnalysisService {
}
return processExcelFiles(excelFiles);
}
else {
} else {
return "不支持的文件类型,请上传Excel文件(.xlsx, .xls)、文件夹或包含Excel文件的ZIP文件夹";
}
} catch (Exception e) {
......@@ -80,6 +81,35 @@ public class AnalysisServiceImpl implements AnalysisService {
}
}
@Override
public Result<List<TableHeaderVO>> template(MultipartFile file) {
try {
if (file.isEmpty()) {
return Result.fail("上传失败");
} else if (isExcelFile(file.getOriginalFilename())) {
File tempFile = convertMultipartFileToFile(file);
return Result.data(parseTheHeader(tempFile));
} else {
return Result.fail("上传失败");
}
} catch (Exception e) {
return Result.fail("上传失败");
}
}
private List<TableHeaderVO> parseTheHeader(File tempFile) throws IOException {
//读取第一个sheet
Sheet sheet = getWorkbook(tempFile).getSheetAt(0);
//获取文件表头
Row headerRow = sheet.getRow(0);
List<TableHeaderVO> headers = new ArrayList<>();
int index = 0;
for (Cell cell : headerRow) {
headers.add(TableHeaderVO.builder().name(getCellValueAsString(cell)).index(index++).build());
}
return headers;
}
private boolean isLikelyDirectory(String filename) {
if (filename == null) return false;
String lowerCaseFilename = filename.toLowerCase();
......
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