Compare commits
No commits in common. "7b15fad32e4bd65d38a1e56f034d9bb7fc1256e7" and "ecfbf71ca00ae47b8e1fe8788adc6ec84d2f16b6" have entirely different histories.
7b15fad32e
...
ecfbf71ca0
@ -1,38 +0,0 @@
|
|||||||
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.ExportSheet.ExportSheetData;
|
|
||||||
import com.wxjw.service.ExportSheetService;
|
|
||||||
import jakarta.annotation.Resource;
|
|
||||||
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;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 导出Excel表格
|
|
||||||
*
|
|
||||||
* @author 筱锋xiao_lfeng
|
|
||||||
*/
|
|
||||||
@RestController
|
|
||||||
@RequestMapping("/openapi/export")
|
|
||||||
public class ExportSheetController {
|
|
||||||
|
|
||||||
@Resource
|
|
||||||
private ExcelInfoMapper excelInfoMapper;
|
|
||||||
|
|
||||||
@PostMapping("/sheet")
|
|
||||||
public ResponseEntity<BaseResponse<Object>> exportSheet(@RequestBody ExportSheetData requestBody) {
|
|
||||||
if ("exportfile".equals(requestBody.getAction())) {
|
|
||||||
ExportSheetService exportSheetService = new ExportSheetService(excelInfoMapper);
|
|
||||||
exportSheetService.exportSheet(requestBody);
|
|
||||||
return exportSheetService.getReturnResult();
|
|
||||||
} else {
|
|
||||||
return ResultUtil.error(ErrorCode.PARAMETER_ERROR);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -5,7 +5,7 @@ import com.wxjw.common.BaseResponse;
|
|||||||
import com.wxjw.common.ResultUtil;
|
import com.wxjw.common.ResultUtil;
|
||||||
import com.wxjw.dal.dao.ExcelInfoMapper;
|
import com.wxjw.dal.dao.ExcelInfoMapper;
|
||||||
import com.wxjw.dal.pojo.ErrorCode;
|
import com.wxjw.dal.pojo.ErrorCode;
|
||||||
import com.wxjw.dal.pojo.data.RetrieveFiles.InsertTableData;
|
import com.wxjw.dal.pojo.data.insertTable.InsertTableData;
|
||||||
import com.wxjw.service.InsertTableService;
|
import com.wxjw.service.InsertTableService;
|
||||||
import jakarta.annotation.Resource;
|
import jakarta.annotation.Resource;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
@ -1,38 +0,0 @@
|
|||||||
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.RetrieveFilesData;
|
|
||||||
import com.wxjw.service.RetrieveFilesService;
|
|
||||||
import jakarta.annotation.Resource;
|
|
||||||
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/retrieve")
|
|
||||||
public class RetrieveFilesController {
|
|
||||||
|
|
||||||
@Resource
|
|
||||||
private ExcelInfoMapper excelInfoMapper;
|
|
||||||
|
|
||||||
@PostMapping("/files")
|
|
||||||
public ResponseEntity<BaseResponse<Object>> retrieveFiles(@RequestBody RetrieveFilesData requestBody) {
|
|
||||||
if ("search".equals(requestBody.getAction())) {
|
|
||||||
RetrieveFilesService retrieveFilesService = new RetrieveFilesService(excelInfoMapper);
|
|
||||||
retrieveFilesService.getSearchFile(requestBody);
|
|
||||||
return retrieveFilesService.getReturnResult();
|
|
||||||
} else {
|
|
||||||
return ResultUtil.error(ErrorCode.PARAMETER_ERROR);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -18,7 +18,6 @@ public enum ErrorCode {
|
|||||||
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),
|
||||||
RETRIEVE_EMPTY("RetrieveEmpty", 40017, "文件检索为空", HttpCode.BAD_REQUEST),
|
|
||||||
THERE_IS_NO_SUCH_RECORD("ThereIsNoSuchRecord", 40017, "记录不存在", HttpCode.BAD_REQUEST);
|
THERE_IS_NO_SUCH_RECORD("ThereIsNoSuchRecord", 40017, "记录不存在", HttpCode.BAD_REQUEST);
|
||||||
|
|
||||||
private final String output;
|
private final String output;
|
||||||
|
@ -1,14 +0,0 @@
|
|||||||
package com.wxjw.dal.pojo.data.ExportSheet;
|
|
||||||
|
|
||||||
import lombok.Getter;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取参数表数据
|
|
||||||
*
|
|
||||||
* @author 筱锋xiao_lfeng
|
|
||||||
*/
|
|
||||||
@Getter
|
|
||||||
public class ExportSheetData {
|
|
||||||
private String action;
|
|
||||||
private int nodeId;
|
|
||||||
}
|
|
@ -1,14 +0,0 @@
|
|||||||
package com.wxjw.dal.pojo.data;
|
|
||||||
|
|
||||||
import lombok.Getter;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取前端文件列表数据
|
|
||||||
* @author 筱锋xiao_lfeng
|
|
||||||
*/
|
|
||||||
@Getter
|
|
||||||
public class RetrieveFilesData {
|
|
||||||
private String action;
|
|
||||||
private String searchConent;
|
|
||||||
private int nodeId;
|
|
||||||
}
|
|
@ -1,4 +1,4 @@
|
|||||||
package com.wxjw.dal.pojo.data.RetrieveFiles;
|
package com.wxjw.dal.pojo.data.insertTable;
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
@ -1,68 +0,0 @@
|
|||||||
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.ExportSheet.ExportSheetData;
|
|
||||||
import com.wxjw.dal.pojo.entity.ExcelInfoEntity;
|
|
||||||
import lombok.Getter;
|
|
||||||
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.Base64;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Service 操作导出 Sheet
|
|
||||||
*
|
|
||||||
* @author 筱锋xiao_lfeng
|
|
||||||
*/
|
|
||||||
@Service
|
|
||||||
public class ExportSheetService {
|
|
||||||
|
|
||||||
private final ExcelInfoMapper excelInfoMapper;
|
|
||||||
@Getter
|
|
||||||
private ResponseEntity<BaseResponse<Object>> returnResult;
|
|
||||||
|
|
||||||
public ExportSheetService(ExcelInfoMapper excelInfoMapper) {
|
|
||||||
this.excelInfoMapper = excelInfoMapper;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void exportSheet(@NotNull ExportSheetData requestBody) {
|
|
||||||
// 检查此 id 是否在数据库中存在
|
|
||||||
ExcelInfoEntity getExcel = excelInfoMapper.getExcelForId(requestBody.getNodeId());
|
|
||||||
if (getExcel != null && getExcel.getId() != null) {
|
|
||||||
// 获取数据
|
|
||||||
String getFileName = getExcel.getFileName();
|
|
||||||
String getSheetName = getExcel.getSheetName();
|
|
||||||
// 构建文件夹位置
|
|
||||||
String filePathForXlsx = "./src/main/resources/excel/" + getFileName + "/" + getSheetName + ".xlsx";
|
|
||||||
String filePathForXls = "./src/main/resources/excel/" + getFileName + "/" + getSheetName + ".xls";
|
|
||||||
String getBase64;
|
|
||||||
try {
|
|
||||||
InputStream inputStream;
|
|
||||||
if (Files.exists(Paths.get(filePathForXlsx))) {
|
|
||||||
inputStream = new FileInputStream(filePathForXlsx);
|
|
||||||
} else {
|
|
||||||
inputStream = new FileInputStream(filePathForXls);
|
|
||||||
}
|
|
||||||
getBase64 = Base64.getEncoder().encodeToString(inputStream.readAllBytes());
|
|
||||||
// 关闭输入流
|
|
||||||
inputStream.close();
|
|
||||||
} catch (IOException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
// 输出结果
|
|
||||||
returnResult = ResultUtil.success(getBase64);
|
|
||||||
} else {
|
|
||||||
// 为空操作
|
|
||||||
returnResult = ResultUtil.error(ErrorCode.THERE_IS_NO_SUCH_RECORD);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -4,9 +4,10 @@ import com.wxjw.common.BaseResponse;
|
|||||||
import com.wxjw.common.ResultUtil;
|
import com.wxjw.common.ResultUtil;
|
||||||
import com.wxjw.dal.dao.ExcelInfoMapper;
|
import com.wxjw.dal.dao.ExcelInfoMapper;
|
||||||
import com.wxjw.dal.pojo.ErrorCode;
|
import com.wxjw.dal.pojo.ErrorCode;
|
||||||
import com.wxjw.dal.pojo.data.RetrieveFiles.InsertTableData;
|
import com.wxjw.dal.pojo.data.insertTable.InsertTableData;
|
||||||
import com.wxjw.dal.pojo.entity.ExcelInfoEntity;
|
import com.wxjw.dal.pojo.entity.ExcelInfoEntity;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
@ -1,139 +0,0 @@
|
|||||||
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.RetrieveFilesData;
|
|
||||||
import com.wxjw.dal.pojo.entity.ExcelInfoEntity;
|
|
||||||
import lombok.Getter;
|
|
||||||
import org.apache.poi.ss.usermodel.HorizontalAlignment;
|
|
||||||
import org.apache.poi.ss.usermodel.Sheet;
|
|
||||||
import org.apache.poi.ss.usermodel.Workbook;
|
|
||||||
import org.apache.poi.ss.util.CellRangeAddress;
|
|
||||||
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.File;
|
|
||||||
import java.io.FileInputStream;
|
|
||||||
import java.io.FileOutputStream;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.nio.file.Files;
|
|
||||||
import java.nio.file.Paths;
|
|
||||||
import java.text.SimpleDateFormat;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Date;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Service 文件查找
|
|
||||||
*
|
|
||||||
* @author 筱锋xiao_lfeng
|
|
||||||
*/
|
|
||||||
@Service
|
|
||||||
public class RetrieveFilesService {
|
|
||||||
|
|
||||||
private final ExcelInfoMapper excelInfoMapper;
|
|
||||||
@Getter
|
|
||||||
private ResponseEntity<BaseResponse<Object>> returnResult;
|
|
||||||
|
|
||||||
public RetrieveFilesService(ExcelInfoMapper excelInfoMapper) {
|
|
||||||
this.excelInfoMapper = excelInfoMapper;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void getSearchFile(@NotNull RetrieveFilesData requestBody) {
|
|
||||||
// 检查 nodeId 是否存在
|
|
||||||
ExcelInfoEntity getExcel = excelInfoMapper.getExcelForId(requestBody.getNodeId());
|
|
||||||
if (getExcel != null && getExcel.getId() != null) {
|
|
||||||
// 获取数据
|
|
||||||
String getFileName = getExcel.getFileName();
|
|
||||||
String getSheetName = getExcel.getSheetName();
|
|
||||||
// 构建文件夹位置
|
|
||||||
String filePathForXlsx = "./src/main/resources/excel/" + getFileName + "/" + getSheetName + ".xlsx";
|
|
||||||
String filePathForXls = "./src/main/resources/excel/" + getFileName + "/" + getSheetName + ".xls";
|
|
||||||
// 获取文件
|
|
||||||
Workbook workbook;
|
|
||||||
try {
|
|
||||||
FileInputStream inputStream;
|
|
||||||
if (Files.exists(Paths.get(filePathForXlsx))) {
|
|
||||||
inputStream = new FileInputStream(filePathForXlsx);
|
|
||||||
} else {
|
|
||||||
inputStream = new FileInputStream(filePathForXls);
|
|
||||||
}
|
|
||||||
// 流输入工作目录
|
|
||||||
workbook = new XSSFWorkbook(inputStream);
|
|
||||||
// 关闭流
|
|
||||||
inputStream.close();
|
|
||||||
} catch (IOException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
// 进入循环配置
|
|
||||||
// 获取单元行Row内容
|
|
||||||
Sheet sheet = workbook.getSheetAt(0);
|
|
||||||
ArrayList<Object[]> searchList = new ArrayList<>();
|
|
||||||
boolean isEmpty = true;
|
|
||||||
for (int i = 0; i < sheet.getPhysicalNumberOfRows(); i++) {
|
|
||||||
// 获取单元格内容
|
|
||||||
for (int j = 0; j < sheet.getRow(i).getPhysicalNumberOfCells(); j++) {
|
|
||||||
// 检索Cell
|
|
||||||
if (sheet.getRow(i).getCell(j).getStringCellValue().contains(requestBody.getSearchConent())) {
|
|
||||||
searchList.add(new Object[]{sheet.getRow(i).getCell(j).getStringCellValue(), i, j});
|
|
||||||
isEmpty = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (isEmpty) {
|
|
||||||
returnResult = ResultUtil.error(ErrorCode.RETRIEVE_EMPTY);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
// 生成新 表
|
|
||||||
Workbook newWorkbook = new XSSFWorkbook();
|
|
||||||
Sheet newSheet = newWorkbook.createSheet("searchSheetName");
|
|
||||||
newSheet.createRow(0).createCell(0).setCellValue("内容查找");
|
|
||||||
newSheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 4));
|
|
||||||
for (int i = 1; i <= searchList.size() + 1; i++) {
|
|
||||||
if (i > 1) {
|
|
||||||
newSheet.createRow(i).createCell(0).setCellValue(i - 1);
|
|
||||||
newSheet.getRow(i).createCell(1).setCellValue(requestBody.getSearchConent());
|
|
||||||
newSheet.getRow(i).createCell(2).setCellValue(searchList.get(i - 2)[0].toString());
|
|
||||||
newSheet.getRow(i).createCell(3).setCellValue((int)searchList.get(i - 2)[1]);
|
|
||||||
newSheet.getRow(i).createCell(4).setCellValue((int)searchList.get(i - 2)[2]);
|
|
||||||
} else {
|
|
||||||
newSheet.createRow(1).createCell(0).setCellValue("序号");
|
|
||||||
newSheet.getRow(1).createCell(1).setCellValue("查找");
|
|
||||||
newSheet.getRow(1).createCell(2).setCellValue("找到");
|
|
||||||
newSheet.getRow(1).createCell(3).setCellValue("Row");
|
|
||||||
newSheet.getRow(1).createCell(4).setCellValue("Column");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// 获取命名规则
|
|
||||||
long date = new Date().getTime();
|
|
||||||
String newSheetName = "sheet1";
|
|
||||||
File file = new File("./src/main/resources/excel/" + date);
|
|
||||||
if (file.mkdirs()) {
|
|
||||||
try (FileOutputStream outputStream = new FileOutputStream("./src/main/resources/excel/" + date + "/" + newSheetName + ".xlsx")) {
|
|
||||||
newWorkbook.write(outputStream);
|
|
||||||
// 插入数据库
|
|
||||||
ExcelInfoEntity excelInfoEntity = new ExcelInfoEntity();
|
|
||||||
excelInfoEntity
|
|
||||||
.setFileName(String.valueOf(date))
|
|
||||||
.setSheetName(newSheetName)
|
|
||||||
.setType(2)
|
|
||||||
.setParentId(getExcel.getId())
|
|
||||||
.setCreateBy(getExcel.getCreateBy())
|
|
||||||
.setCreateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
|
|
||||||
// 数据库写入
|
|
||||||
excelInfoMapper.insertExcelInfo(excelInfoEntity);
|
|
||||||
// 返回结果
|
|
||||||
returnResult = ResultUtil.success();
|
|
||||||
} catch (IOException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// 为空操作
|
|
||||||
returnResult = ResultUtil.error(ErrorCode.THERE_IS_NO_SUCH_RECORD);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
Reference in New Issue
Block a user