添加功能 获取 sheet
This commit is contained in:
parent
325d078641
commit
ecfbf71ca0
4
pom.xml
4
pom.xml
|
@ -59,12 +59,12 @@
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.apache.poi</groupId>
|
<groupId>org.apache.poi</groupId>
|
||||||
<artifactId>poi</artifactId>
|
<artifactId>poi</artifactId>
|
||||||
<version>5.0.0</version>
|
<version>5.1.0</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.apache.poi</groupId>
|
<groupId>org.apache.poi</groupId>
|
||||||
<artifactId>poi-ooxml</artifactId>
|
<artifactId>poi-ooxml</artifactId>
|
||||||
<version>5.0.0</version>
|
<version>5.1.0</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<!--<dependency>
|
<!--<dependency>
|
||||||
<groupId>org.slf4j</groupId>
|
<groupId>org.slf4j</groupId>
|
||||||
|
|
|
@ -0,0 +1,36 @@
|
||||||
|
package com.wxjw.controller.openapi;
|
||||||
|
|
||||||
|
import com.wxjw.common.BaseResponse;
|
||||||
|
import com.wxjw.common.ResultUtil;
|
||||||
|
import com.wxjw.dal.dao.ExcelInfoMapper;
|
||||||
|
import com.wxjw.dal.pojo.ErrorCode;
|
||||||
|
import com.wxjw.dal.pojo.data.GetSheet.GetSheetResultBody;
|
||||||
|
import com.wxjw.service.GetSheetService;
|
||||||
|
import jakarta.annotation.Resource;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Controller 获取 sheet
|
||||||
|
*
|
||||||
|
* @author 筱锋xiao_lfeng
|
||||||
|
*/
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/openapi/get")
|
||||||
|
public class GetSheetController {
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private ExcelInfoMapper excelInfoMapper;
|
||||||
|
|
||||||
|
@PostMapping("/sheet")
|
||||||
|
public ResponseEntity<BaseResponse<Object>> getSheet(@RequestBody @NotNull GetSheetResultBody resultBody) {
|
||||||
|
if ("openflie".equals(resultBody.getAction())) {
|
||||||
|
GetSheetService getSheetService = new GetSheetService(excelInfoMapper);
|
||||||
|
getSheetService.getSheet(resultBody);
|
||||||
|
return getSheetService.getReturnResult();
|
||||||
|
} else {
|
||||||
|
return ResultUtil.error(ErrorCode.PARAMETER_ERROR);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -35,6 +35,14 @@ public interface ExcelInfoMapper {
|
||||||
@Select("SELECT * FROM excel_file_handling.excel_info WHERE file_name = #{fileName}")
|
@Select("SELECT * FROM excel_file_handling.excel_info WHERE file_name = #{fileName}")
|
||||||
List<ExcelInfoEntity> getAllExcelFilesNameNoRepetition(String fileName);
|
List<ExcelInfoEntity> getAllExcelFilesNameNoRepetition(String fileName);
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param id 序号
|
||||||
|
* @return 返回单条数据
|
||||||
|
*/
|
||||||
|
@Select("SELECT * FROM excel_file_handling.excel_info WHERE id = #{id}")
|
||||||
|
ExcelInfoEntity getExcelForId(int id);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param excelInfoEntity excel
|
* @param excelInfoEntity excel
|
||||||
* @return 是否插入成功
|
* @return 是否插入成功
|
||||||
|
|
|
@ -17,7 +17,8 @@ public enum ErrorCode {
|
||||||
DATA_WRITE_FAILURE("DataWriteFailure", 40013, "数据写入失败", HttpCode.BAD_REQUEST),
|
DATA_WRITE_FAILURE("DataWriteFailure", 40013, "数据写入失败", HttpCode.BAD_REQUEST),
|
||||||
FILE_TYPE_IS_INCORRECT("FileTypeIncorrect", 40014, "文件类型错误", HttpCode.BAD_REQUEST),
|
FILE_TYPE_IS_INCORRECT("FileTypeIncorrect", 40014, "文件类型错误", HttpCode.BAD_REQUEST),
|
||||||
FILE_CREATION_FAILED("FileCreationFailed", 40015, "文件创建失败", HttpCode.BAD_REQUEST),
|
FILE_CREATION_FAILED("FileCreationFailed", 40015, "文件创建失败", HttpCode.BAD_REQUEST),
|
||||||
FILE_ALREADY_EXISTS("FileAlreadyExists", 40016, "文件已经存在", HttpCode.BAD_REQUEST);
|
FILE_ALREADY_EXISTS("FileAlreadyExists", 40016, "文件已经存在", HttpCode.BAD_REQUEST),
|
||||||
|
THERE_IS_NO_SUCH_RECORD("ThereIsNoSuchRecord", 40017, "记录不存在", HttpCode.BAD_REQUEST);
|
||||||
|
|
||||||
private final String output;
|
private final String output;
|
||||||
private final int code;
|
private final int code;
|
||||||
|
|
|
@ -0,0 +1,18 @@
|
||||||
|
package com.wxjw.dal.pojo.data.GetSheet;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Data 获取
|
||||||
|
*
|
||||||
|
* @author 筱锋xiao_lfeng
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class GetSheetData<T> {
|
||||||
|
private T title;
|
||||||
|
private T tablehead;
|
||||||
|
private T colwidth;
|
||||||
|
private T tabledata;
|
||||||
|
}
|
|
@ -0,0 +1,14 @@
|
||||||
|
package com.wxjw.dal.pojo.data.GetSheet;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取前端表格数据的返回
|
||||||
|
*
|
||||||
|
* @author 筱锋xiao_lfeng
|
||||||
|
*/
|
||||||
|
@Getter
|
||||||
|
public class GetSheetResultBody {
|
||||||
|
private String action;
|
||||||
|
private int id;
|
||||||
|
}
|
117
src/main/java/com/wxjw/service/GetSheetService.java
Normal file
117
src/main/java/com/wxjw/service/GetSheetService.java
Normal file
|
@ -0,0 +1,117 @@
|
||||||
|
package com.wxjw.service;
|
||||||
|
|
||||||
|
import com.wxjw.common.BaseResponse;
|
||||||
|
import com.wxjw.common.ResultUtil;
|
||||||
|
import com.wxjw.dal.dao.ExcelInfoMapper;
|
||||||
|
import com.wxjw.dal.pojo.ErrorCode;
|
||||||
|
import com.wxjw.dal.pojo.data.GetSheet.GetSheetData;
|
||||||
|
import com.wxjw.dal.pojo.data.GetSheet.GetSheetResultBody;
|
||||||
|
import com.wxjw.dal.pojo.entity.ExcelInfoEntity;
|
||||||
|
import lombok.Getter;
|
||||||
|
import org.apache.poi.ss.usermodel.Cell;
|
||||||
|
import org.apache.poi.ss.usermodel.Row;
|
||||||
|
import org.apache.poi.ss.usermodel.Sheet;
|
||||||
|
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Iterator;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Service 获取 Sheet
|
||||||
|
*
|
||||||
|
* @author 筱锋xiao_lfeng
|
||||||
|
*/
|
||||||
|
@Service
|
||||||
|
public class GetSheetService {
|
||||||
|
|
||||||
|
private final ExcelInfoMapper excelInfoMapper;
|
||||||
|
@Getter
|
||||||
|
private ResponseEntity<BaseResponse<Object>> returnResult;
|
||||||
|
|
||||||
|
public GetSheetService(ExcelInfoMapper excelInfoMapper) {
|
||||||
|
this.excelInfoMapper = excelInfoMapper;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void getSheet(@NotNull GetSheetResultBody resultBody) {
|
||||||
|
ExcelInfoEntity getSheet = excelInfoMapper.getExcelForId(resultBody.getId());
|
||||||
|
if (getSheet != null) {
|
||||||
|
// 获取文件所在地址
|
||||||
|
String filePath = getSheet.getFileName();
|
||||||
|
String fileName = getSheet.getSheetName();
|
||||||
|
// 文件地址
|
||||||
|
String pathXlsx = "./src/main/resources/excel/" + filePath + "/" + fileName + ".xlsx";
|
||||||
|
String pathXls = "./src/main/resources/excel/" + filePath + "/" + fileName + ".xls";
|
||||||
|
// 文件输入解析
|
||||||
|
try {
|
||||||
|
InputStream inputStream;
|
||||||
|
if (Files.exists(Paths.get(pathXlsx))) {
|
||||||
|
inputStream = new FileInputStream(pathXlsx);
|
||||||
|
} else {
|
||||||
|
inputStream = new FileInputStream(pathXls);
|
||||||
|
}
|
||||||
|
Sheet getEntitySheet = new XSSFWorkbook(inputStream).getSheetAt(0);
|
||||||
|
inputStream.close();
|
||||||
|
// 获取 title
|
||||||
|
Row titleRow = getEntitySheet.getRow(0);
|
||||||
|
StringBuilder title = null;
|
||||||
|
Iterator<Cell> iterator = titleRow.cellIterator();
|
||||||
|
while (iterator.hasNext()) {
|
||||||
|
Cell cell = iterator.next();
|
||||||
|
title = (title == null || title.isEmpty() ? new StringBuilder() : title).append(cell.toString());
|
||||||
|
}
|
||||||
|
// 获取 Header
|
||||||
|
Iterator<Cell> headerRow = getEntitySheet.getRow(1).cellIterator();
|
||||||
|
// 获取总行数
|
||||||
|
ArrayList<ArrayList<Object>> line = new ArrayList<>();
|
||||||
|
String[] rowMax = new String[getEntitySheet.getRow(1).getPhysicalNumberOfCells()];
|
||||||
|
ArrayList<String> headerArray = null;
|
||||||
|
for (int i = 2; i < getEntitySheet.getPhysicalNumberOfRows(); i++) {
|
||||||
|
Row loopRow = getEntitySheet.getRow(i);
|
||||||
|
ArrayList<Object> temp = new ArrayList<>();
|
||||||
|
// 验证Cell并判断所有 Row 中数字最大的
|
||||||
|
headerArray = new ArrayList<>();
|
||||||
|
for (int j = 0; getEntitySheet.getRow(1).cellIterator().hasNext(); j++) {
|
||||||
|
if (j < getEntitySheet.getRow(1).getPhysicalNumberOfCells()) {
|
||||||
|
try {
|
||||||
|
if (rowMax[j] == null || rowMax[j].length() < loopRow.getCell(j).toString().length()) {
|
||||||
|
switch (loopRow.getCell(j).getCellType()) {
|
||||||
|
case NUMERIC -> rowMax[j] = String.valueOf(loopRow.getCell(j).getNumericCellValue());
|
||||||
|
case BOOLEAN -> rowMax[j] = String.valueOf(loopRow.getCell(j).getBooleanCellValue());
|
||||||
|
case BLANK -> {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
case ERROR -> rowMax[j] = String.valueOf(loopRow.getCell(j).getErrorCellValue());
|
||||||
|
default -> rowMax[j] = String.valueOf(loopRow.getCell(j).getStringCellValue());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (NullPointerException e) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
headerArray.add(String.valueOf(getEntitySheet.getRow(1).getCell(j).getStringCellValue()));
|
||||||
|
temp.add(String.valueOf(loopRow.getCell(j)));
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
line.add(temp);
|
||||||
|
}
|
||||||
|
// 整理数据
|
||||||
|
returnResult = ResultUtil.success(new GetSheetData<>(title, headerArray, rowMax, line));
|
||||||
|
// 获取
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// 为空操作
|
||||||
|
returnResult = ResultUtil.error(ErrorCode.THERE_IS_NO_SUCH_RECORD);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -27,8 +27,6 @@ public class GetUploadFileService {
|
||||||
private final ExcelInfoMapper excelInfoMapper;
|
private final ExcelInfoMapper excelInfoMapper;
|
||||||
@Getter
|
@Getter
|
||||||
private ResponseEntity<BaseResponse<Object>> returnResult;
|
private ResponseEntity<BaseResponse<Object>> returnResult;
|
||||||
@Getter
|
|
||||||
private boolean checkType;
|
|
||||||
|
|
||||||
public GetUploadFileService(ExcelInfoMapper excelInfoMapper) {
|
public GetUploadFileService(ExcelInfoMapper excelInfoMapper) {
|
||||||
this.excelInfoMapper = excelInfoMapper;
|
this.excelInfoMapper = excelInfoMapper;
|
||||||
|
|
Reference in New Issue
Block a user