"src/main/git@gitlab.ruzhoukj.com:wujipeng/relation.git" did not exist on "fe9324cc18eba2b395c8a62c8c7c26e15857aeab"
Commit 8609f079 authored by wujipeng's avatar wujipeng
Browse files

解析excel数据并入库

parent 98321c30
...@@ -27,8 +27,9 @@ public class AnalysisController { ...@@ -27,8 +27,9 @@ public class AnalysisController {
@ApiOperation("解析画布边文件上传excel或文件夹") @ApiOperation("解析画布边文件上传excel或文件夹")
@PostMapping("/canvas") @PostMapping("/canvas")
public Result<?> canvas(@RequestParam(value = "file", required = false) MultipartFile file) { public Result<?> canvas(@RequestParam(value = "file") MultipartFile file,
return Result.data(analysisService.canvas(file)); @RequestParam(value = "templateId") Integer templateId) {
return analysisService.canvas(file,templateId);
} }
@ApiOperation("解析模板表头") @ApiOperation("解析模板表头")
......
package com.platform.model.vo; package com.platform.model.vo;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Builder; import lombok.Builder;
import lombok.Data; import lombok.Data;
...@@ -7,7 +8,7 @@ import lombok.NoArgsConstructor; ...@@ -7,7 +8,7 @@ import lombok.NoArgsConstructor;
/** /**
* @author : wujipeng * @author : wujipeng
* @date : 2025/7/30 14:42 * @date : 2025/7/30 15:45
* @Version: 1.0 * @Version: 1.0
* @Desc : 描述信息 * @Desc : 描述信息
*/ */
...@@ -17,8 +18,9 @@ import lombok.NoArgsConstructor; ...@@ -17,8 +18,9 @@ import lombok.NoArgsConstructor;
@Builder @Builder
public class TableHeaderVO { public class TableHeaderVO {
@ApiModelProperty("字段名")
private String name; private String name;
@ApiModelProperty("字段索引")
private Integer index; private Integer index;
} }
...@@ -19,7 +19,7 @@ public interface AnalysisService { ...@@ -19,7 +19,7 @@ public interface AnalysisService {
* @param file * @param file
* @return * @return
*/ */
Object canvas(MultipartFile file); Result<?> canvas(MultipartFile file,Integer templateId);
/** /**
* 读取模板文件 * 读取模板文件
......
package com.platform.service.impl; package com.platform.service.impl;
import com.alibaba.fastjson.JSON;
import com.platform.model.entity.ExcelDataEntity;
import com.platform.model.entity.ExcelTemplateEntity;
import com.platform.model.resp.Result; import com.platform.model.resp.Result;
import com.platform.model.vo.TableHeaderVO; import com.platform.model.vo.TableHeaderVO;
import com.platform.service.AnalysisService; import com.platform.service.AnalysisService;
import com.platform.service.ExcelDataService;
import com.platform.service.ExcelTemplateService;
import com.platform.utils.DateUtils;
import org.apache.commons.compress.utils.Lists;
import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*; import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException; 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.StandardCopyOption; import java.nio.file.StandardCopyOption;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
/** /**
* @author : wujipeng * @author : wujipeng
...@@ -30,18 +36,28 @@ import java.util.zip.ZipInputStream; ...@@ -30,18 +36,28 @@ import java.util.zip.ZipInputStream;
@Service @Service
public class AnalysisServiceImpl implements AnalysisService { public class AnalysisServiceImpl implements AnalysisService {
@Resource
private ExcelTemplateService excelTemplateService;
@Resource
private ExcelDataService excelDataService;
private static final String[] EXCEL_EXTENSIONS = {".xlsx", ".xls"}; private static final String[] EXCEL_EXTENSIONS = {".xlsx", ".xls"};
@Override @Override
public Object canvas(MultipartFile file) { public Result<?> canvas(MultipartFile file, Integer templateId) {
//查询模板
ExcelTemplateEntity ExcelTemplate = excelTemplateService.getById(templateId);
if(ObjectUtils.isEmpty(ExcelTemplate)){
return Result.fail("模板不存在");
}
try { try {
if (file.isEmpty()) { if (file.isEmpty()) {
return "上传的文件为空"; return Result.fail("上传的文件为空");
} }
// 检查是否是Excel文件 // 检查是否是Excel文件
else if (isExcelFile(file.getOriginalFilename())) { else if (isExcelFile(file.getOriginalFilename())) {
File tempFile = convertMultipartFileToFile(file); File tempFile = convertMultipartFileToFile(file);
return processSingleExcelFile(tempFile); return Result.data(processSingleExcelFile(tempFile,ExcelTemplate));
} }
// 检查是否是目录(通过检查是否没有扩展名) // 检查是否是目录(通过检查是否没有扩展名)
else if (isLikelyDirectory(file.getOriginalFilename())) { else if (isLikelyDirectory(file.getOriginalFilename())) {
...@@ -64,14 +80,12 @@ public class AnalysisServiceImpl implements AnalysisService { ...@@ -64,14 +80,12 @@ public class AnalysisServiceImpl implements AnalysisService {
excelFiles.add(uploadedFile.toFile()); excelFiles.add(uploadedFile.toFile());
} }
} }
return Result.data(processExcelFiles(excelFiles));
return processExcelFiles(excelFiles);
} else { } else {
return "不支持的文件类型,请上传Excel文件(.xlsx, .xls)、文件夹或包含Excel文件的ZIP文件夹"; return Result.fail("不支持的文件类型,请上传Excel文件(.xlsx, .xls)或包含Excel文件的ZIP文件夹");
} }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); return Result.fail("文件处理失败: " + e.getMessage());
return "文件处理失败: " + e.getMessage();
} }
} }
...@@ -91,15 +105,17 @@ public class AnalysisServiceImpl implements AnalysisService { ...@@ -91,15 +105,17 @@ public class AnalysisServiceImpl implements AnalysisService {
} }
} }
//返回Excel文件表头数组
private List<TableHeaderVO> parseTheHeader(File tempFile) throws IOException { private List<TableHeaderVO> parseTheHeader(File tempFile) throws IOException {
//读取第一个sheet //读取第一个sheet
Sheet sheet = getWorkbook(tempFile).getSheetAt(0); Sheet sheet = getWorkbook(tempFile).getSheetAt(0);
//获取文件表头 //获取文件表头
Row headerRow = sheet.getRow(0); Row headerRow = sheet.getRow(0);
List<TableHeaderVO> headers = new ArrayList<>(); List<TableHeaderVO> headers = Lists.newArrayList();
int index = 0; int index = 0;
for (Cell cell : headerRow) { for (Cell cell : headerRow) {
headers.add(TableHeaderVO.builder().name(getCellValueAsString(cell)).index(index++).build()); headers.add(TableHeaderVO.builder().name(getCellValueAsString(cell)).index(index).build());
index++;
} }
return headers; return headers;
} }
...@@ -151,22 +167,81 @@ public class AnalysisServiceImpl implements AnalysisService { ...@@ -151,22 +167,81 @@ public class AnalysisServiceImpl implements AnalysisService {
} }
// 处理单个Excel文件 // 处理单个Excel文件
private String processSingleExcelFile(File excelFile) throws IOException { private String processSingleExcelFile(File excelFile,ExcelTemplateEntity ExcelTemplate) throws IOException {
String json = ExcelTemplate.getJson();
List<TableHeaderVO> tableHeaderList = JSON.parseArray(json, TableHeaderVO.class);
//读取第一个sheet //读取第一个sheet
Sheet sheet = getWorkbook(excelFile).getSheetAt(0); Sheet sheet = getWorkbook(excelFile).getSheetAt(0);
//获取sheet长度
int sheetLength = sheet.getLastRowNum();
//获取文件表头 List<ExcelDataEntity> excelData = Lists.newArrayList();
Row headerRow = sheet.getRow(0);
List<String> headers = new ArrayList<>();
for (Cell cell : headerRow) {
headers.add(getCellValueAsString(cell));
}
String content = readExcelFile(excelFile); for (int i = 1; i < sheetLength; i++) {
if (!excelFile.delete()) { Row row = sheet.getRow(i);
excelFile.deleteOnExit(); //处理excel数据
handleExcelData(tableHeaderList, row, excelData);
} }
return "处理单个Excel文件:\n" + content; //保存excel数据
excelDataService.saveBatch(excelData);
return "处理单个Excel文件:\n";
}
private void handleExcelData(List<TableHeaderVO> tableHeaderList, Row row, List<ExcelDataEntity> excelData) {
ExcelDataEntity excelDataEntity = new ExcelDataEntity();
try {
excelDataEntity.setType(getCellValueAsString(row.getCell(tableHeaderList.get(0).getIndex())));
} catch (Exception ignored) {}
try {
excelDataEntity.setDirection(getCellValueAsString(row.getCell(tableHeaderList.get(1).getIndex())));
} catch (Exception ignored) {}
try {
excelDataEntity.setTime(DateUtils.parseCompactDateTime(getCellValueAsString(row.getCell(tableHeaderList.get(2).getIndex()))));
} catch (Exception ignored) {}
try {
excelDataEntity.setAmount(Double.parseDouble(getCellValueAsString(row.getCell(tableHeaderList.get(3).getIndex()))));
} catch (Exception ignored) {}
try {
excelDataEntity.setSerial(getCellValueAsString(row.getCell(tableHeaderList.get(4).getIndex())));
} catch (Exception ignored) {}
try {
excelDataEntity.setPayeeBankCard(getCellValueAsString(row.getCell(tableHeaderList.get(5).getIndex())));
} catch (Exception ignored) {}
try {
excelDataEntity.setPayeeAccount(getCellValueAsString(row.getCell(tableHeaderList.get(6).getIndex())));
} catch (Exception ignored) {}
try {
excelDataEntity.setPayeeMerchant(getCellValueAsString(row.getCell(tableHeaderList.get(7).getIndex())));
} catch (Exception ignored) {}
try {
excelDataEntity.setPayeeName(getCellValueAsString(row.getCell(tableHeaderList.get(8).getIndex())));
} catch (Exception ignored) {}
try {
excelDataEntity.setPaymentBankName(getCellValueAsString(row.getCell(tableHeaderList.get(9).getIndex())));
} catch (Exception ignored) {}
try {
excelDataEntity.setPaymentBankCard(getCellValueAsString(row.getCell(tableHeaderList.get(10).getIndex())));
} catch (Exception ignored) {}
try {
excelDataEntity.setPaymentAccount(getCellValueAsString(row.getCell(tableHeaderList.get(11).getIndex())));
} catch (Exception ignored) {}
try {
excelDataEntity.setIp(getCellValueAsString(row.getCell(tableHeaderList.get(12).getIndex())));
} catch (Exception ignored) {}
try {
excelDataEntity.setMac(getCellValueAsString(row.getCell(tableHeaderList.get(13).getIndex())));
} catch (Exception ignored) {}
try {
excelDataEntity.setExternalChannel(getCellValueAsString(row.getCell(tableHeaderList.get(14).getIndex())));
} catch (Exception ignored) {}
try {
excelDataEntity.setPayAccount(getCellValueAsString(row.getCell(tableHeaderList.get(15).getIndex())));
} catch (Exception ignored) {}
try {
excelDataEntity.setRemark(getCellValueAsString(row.getCell(tableHeaderList.get(16).getIndex())));
} catch (Exception ignored) {}
excelData.add(excelDataEntity);
} }
// 读取Excel文件内容 // 读取Excel文件内容
......
package com.platform.utils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateUtils {
/**
* 将格式为 yyyyMMddHHmmss 的字符串转换为 Date 对象
* @param dateStr 日期时间字符串,格式必须为 yyyyMMddHHmmss
* @return 对应的 Date 对象
* @throws ParseException 如果字符串格式不匹配或无效
*/
public static Date parseCompactDateTime(String dateStr) throws ParseException {
if (dateStr == null || dateStr.length() != 14) {
throw new ParseException("日期字符串格式必须为 yyyyMMddHHmmss,长度为14位", 0);
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
sdf.setLenient(false); // 严格模式,不允许自动转换无效日期
return sdf.parse(dateStr);
}
// 可选:添加一个重载方法,允许自定义格式
public static Date parseDateTime(String dateStr, String pattern) throws ParseException {
if (dateStr == null || pattern == null) {
throw new ParseException("日期字符串和格式模式不能为null", 0);
}
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
sdf.setLenient(false);
return sdf.parse(dateStr);
}
}
\ No newline at end of file
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