返回所选择的sheets的表头数组
This commit is contained in:
parent
54c9a887a2
commit
c422864e43
|
@ -0,0 +1,39 @@
|
|||
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.ListHeadArray.ListHeadArrayData;
|
||||
import com.wxjw.dal.pojo.entity.ExcelInfoEntity;
|
||||
import com.wxjw.service.ListHeadArrayService;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* Controller 表头数组控制
|
||||
*
|
||||
* @author 筱锋xiao_lfeng
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/openapi/header")
|
||||
public class ListHeadArrayController {
|
||||
@Resource
|
||||
private ExcelInfoMapper excelInfoMapper;
|
||||
|
||||
@PostMapping("/get")
|
||||
public ResponseEntity<BaseResponse<Object>> getListHeadArray(@RequestBody @NotNull ListHeadArrayData requestBody) {
|
||||
if ("getfileIds".equals(requestBody.getAction())) {
|
||||
ListHeadArrayService service = new ListHeadArrayService(excelInfoMapper);
|
||||
service.getListHeadArray(requestBody);
|
||||
return service.getReturnResult();
|
||||
} else {
|
||||
return ResultUtil.error(ErrorCode.PARAMETER_ERROR);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -41,6 +41,13 @@ public interface ExcelInfoMapper {
|
|||
@Select("SELECT * FROM excel_file_handling.excel_info WHERE id = #{id}")
|
||||
ExcelInfoEntity getExcelForId(int id);
|
||||
|
||||
/**
|
||||
* @param ids String类型多个id
|
||||
* @return 返回类型内容
|
||||
*/
|
||||
@Select("SELECT * FROM excel_file_handling.excel_info WHERE id IN (${ids})")
|
||||
List<ExcelInfoEntity> getExcelForIds(String ids);
|
||||
|
||||
/**
|
||||
* @param id 序号
|
||||
* @return 是否删除成功
|
||||
|
@ -48,6 +55,10 @@ public interface ExcelInfoMapper {
|
|||
@Delete("DELETE FROM excel_file_handling.excel_info WHERE id = #{id}")
|
||||
boolean deleteExcelForId(int id);
|
||||
|
||||
/**
|
||||
* @param excelInfoEntity Excel自定义实体类
|
||||
* @return 是否更新成功
|
||||
*/
|
||||
@Update("UPDATE excel_file_handling.excel_info SET file_name = #{fileName}, sheet_name = #{sheetName}, table_name = #{tableName}, type = #{type}, create_by = #{createBy}, create_time = #{createTime}, update_by = #{updateBy}, update_time = #{updateTime} WHERE id = #{id}")
|
||||
boolean updateExcelInfo(ExcelInfoEntity excelInfoEntity);
|
||||
|
||||
|
|
|
@ -16,6 +16,8 @@ public enum ErrorCode {
|
|||
DATA_DUPLICATION("DataDuplication", 40012, "数据重复", HttpCode.BAD_REQUEST),
|
||||
DATA_WRITE_FAILURE("DataWriteFailure", 40013, "数据写入失败", HttpCode.BAD_REQUEST),
|
||||
DATA_DELETE_FAILURE("DataDeleteFailure", 40014, "数据删除失败", HttpCode.BAD_REQUEST),
|
||||
DATA_UPDATE_FAILURE("DataUpdateFailure", 40015, "数据更新失败", HttpCode.BAD_REQUEST),
|
||||
DATA_RETRIEVE_FAILURE("DataRetrieveFailure", 40016, "数据检索失败", HttpCode.BAD_REQUEST),
|
||||
FILE_TYPE_IS_INCORRECT("FileTypeIncorrect", 40014, "文件类型错误", HttpCode.BAD_REQUEST),
|
||||
FILE_CREATION_FAILED("FileCreationFailed", 40015, "文件创建失败", HttpCode.BAD_REQUEST),
|
||||
FILE_ALREADY_EXISTS("FileAlreadyExists", 40016, "文件已经存在", HttpCode.BAD_REQUEST),
|
||||
|
|
|
@ -0,0 +1,16 @@
|
|||
package com.wxjw.dal.pojo.data.ListHeadArray;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
import java.util.stream.Collector;
|
||||
|
||||
/**
|
||||
* Data 获取前端数据
|
||||
*
|
||||
* @author 筱锋xiao_lfeng
|
||||
*/
|
||||
@Getter
|
||||
public class ListHeadArrayData {
|
||||
private String action;
|
||||
private int[] nodeId;
|
||||
}
|
93
src/main/java/com/wxjw/service/ListHeadArrayService.java
Normal file
93
src/main/java/com/wxjw/service/ListHeadArrayService.java
Normal file
|
@ -0,0 +1,93 @@
|
|||
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.ListHeadArray.ListHeadArrayData;
|
||||
import com.wxjw.dal.pojo.entity.ExcelInfoEntity;
|
||||
import lombok.Getter;
|
||||
import org.apache.poi.ss.usermodel.Sheet;
|
||||
import org.apache.poi.ss.usermodel.Workbook;
|
||||
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.lang.reflect.Array;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* Service 列表获取控制器
|
||||
*
|
||||
* @author 筱锋xiao_lfeng
|
||||
*/
|
||||
@Service
|
||||
public class ListHeadArrayService {
|
||||
|
||||
private final ExcelInfoMapper excelInfoMapper;
|
||||
@Getter
|
||||
private ResponseEntity<BaseResponse<Object>> returnResult;
|
||||
|
||||
public ListHeadArrayService(ExcelInfoMapper excelInfoMapper) {
|
||||
this.excelInfoMapper = excelInfoMapper;
|
||||
}
|
||||
|
||||
public void getListHeadArray(@NotNull ListHeadArrayData requestBody) {
|
||||
// 检查数据是否存在
|
||||
String number = Arrays.stream(requestBody.getNodeId())
|
||||
.mapToObj(String::valueOf)
|
||||
.collect(Collectors.joining(","));
|
||||
ArrayList<ExcelInfoEntity> excelInfo = (ArrayList<ExcelInfoEntity>) excelInfoMapper.getExcelForIds(number);
|
||||
ArrayList<Object> excelFileIds = new ArrayList<>();
|
||||
if (excelInfo != null && !excelInfo.isEmpty()) {
|
||||
for (ExcelInfoEntity excelInfoEntity : excelInfo) {
|
||||
// 获取文件信息
|
||||
String fileName = excelInfoEntity.getFileName();
|
||||
String sheetName = excelInfoEntity.getSheetName();
|
||||
// 获取文件路径
|
||||
String filePathForXlsx = "./src/main/resources/excel/" + fileName + "/" + sheetName + ".xlsx";
|
||||
String filePathForXls = "./src/main/resources/excel/" + fileName + "/" + sheetName + ".xls";
|
||||
// 读取文件
|
||||
Workbook workbook;
|
||||
try {
|
||||
FileInputStream fileInputStream;
|
||||
if (Files.exists(Path.of(filePathForXlsx))) {
|
||||
fileInputStream = new FileInputStream(filePathForXlsx);
|
||||
} else {
|
||||
fileInputStream = new FileInputStream(filePathForXls);
|
||||
}
|
||||
// 获取文件内容
|
||||
workbook = new XSSFWorkbook(fileInputStream);
|
||||
// 关闭流
|
||||
fileInputStream.close();
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
// 打开文件获取内容
|
||||
Sheet sheet = workbook.getSheetAt(0);
|
||||
// 获取标题抬头
|
||||
ArrayList<Object> data = new ArrayList<>();
|
||||
for (int i = 0; i < sheet.getRow(1).getPhysicalNumberOfCells(); i++) {
|
||||
switch (sheet.getRow(1).getCell(i).getCellType()) {
|
||||
case NUMERIC -> data.add(sheet.getRow(1).getCell(i).getNumericCellValue());
|
||||
case BOOLEAN -> data.add(sheet.getRow(1).getCell(i).getBooleanCellValue());
|
||||
case FORMULA -> data.add(sheet.getRow(1).getCell(i).getCellFormula());
|
||||
default -> data.add(sheet.getRow(1).getCell(i).getStringCellValue());
|
||||
}
|
||||
}
|
||||
excelFileIds.add(data);
|
||||
}
|
||||
// 处理完成返回结果集
|
||||
returnResult = ResultUtil.success(excelFileIds,"输出成功");
|
||||
} else {
|
||||
returnResult = ResultUtil.error(ErrorCode.DATA_IS_EMPTY);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user