feat: 通用导出工具

This commit is contained in:
2024-07-18 17:08:11 +08:00
parent eb380a73d8
commit 48fa9bf18b
7 changed files with 290 additions and 102 deletions
@@ -31,6 +31,7 @@ import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@@ -171,4 +172,25 @@ public class ProcessStandardComplianceCheckController extends JeroController<Pro
return Result.OK("批量删除成功!");
}
/**
* 导出excel
*/
@AutoLog(value = "法规符合性排查流程-分发列表导出")
@ApiOperation(value="法规符合性排查流程-分发列表导出", notes="法规符合性排查流程-分发列表导出")
@GetMapping(value = "/exportXlsDistribute")
public void exportXlsDistribute(@RequestParam("taskId") String taskId, HttpServletResponse response) {
sccService.exportXlsDistribute(taskId, response);
}
/**
* 导出excel
*/
@AutoLog(value = "法规符合性排查流程-不分发列表导出")
@ApiOperation(value="法规符合性排查流程-不分发列表导出", notes="法规符合性排查流程-不分发列表导出")
@GetMapping(value = "/exportXlsNotDistribute")
public void exportXlsNotDistribute(@RequestParam("taskId") String taskId, HttpServletResponse response) {
sccService.exportXlsNotDistribute(taskId, response);
}
}
@@ -7,6 +7,7 @@ import com.jero.modules.activiti.process.standardComplianceCheck.entity.ProcessS
import com.jero.modules.activiti.process.standardComplianceCheck.entity.vo.ProcessSccVO;
import org.activiti.engine.delegate.DelegateTask;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
/**
@@ -27,4 +28,8 @@ public interface IProcessStandardComplianceCheckService extends IService<Process
void submit(ProcessInfoVO<ProcessSccVO> processInfoVO);
List<ProcessStandardComplianceCheckDetail> generate(ProcessStandardComplianceCheck scc) throws Exception;
void exportXlsDistribute(String taskId, HttpServletResponse response);
void exportXlsNotDistribute(String taskId, HttpServletResponse response);
}
@@ -41,6 +41,7 @@ import com.jero.modules.split.service.ISarFileSplitInfoService;
import com.jero.modules.sys.utils.UserUtils;
import com.jero.modules.system.entity.SysDepart;
import com.jero.modules.system.service.ISysDepartService;
import com.jero.modules.system.util.SysWaterMarkUtil;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.delegate.DelegateTask;
@@ -48,11 +49,14 @@ import org.activiti.engine.runtime.ProcessInstance;
import org.apache.http.entity.ContentType;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
@@ -73,35 +77,32 @@ public class ProcessStandardComplianceCheckServiceImpl extends ServiceImpl<Proce
@Resource
private IProcessHandleService processHandleService;
@Resource
private ActFlowCommonService actFlowCommonService;
@Resource
private ProcessNodeLinkService processNodeLinkService;
@Resource
private TaskService taskService;
@Resource
private ISysDepartService sysDepartService;
@Resource
private RuntimeService runtimeService;
@Resource
private IProcessStandardComplianceCheckDetailService sccDetailService;
@Resource
private IDocumentSplitService documentSplitService;
@Resource
private IOSSFileService ossFileService;
@Resource
private ISarFileSplitInfoService sarFileSplitInfoService;
@Resource
private DocumentSplitMapper documentSplitMapper;
@Resource
private SysWaterMarkUtil sysWaterMarkUtil;
@Value("${jero.path.exportExcelTempPath}")
private String exportExcelTempPath;
private static final String EXPORT_EXCEL_TEMP = "exportExcelTemp";
@Override
public void startProcess(ProcessInfoVO<ProcessSccVO> processInfoVO) {
@@ -420,6 +421,51 @@ public class ProcessStandardComplianceCheckServiceImpl extends ServiceImpl<Proce
return this.saveAllDetailData(modelExcelVOList, detailInfoList);
}
@Override
public void exportXlsDistribute(String taskId, HttpServletResponse response) {
ProcessInfoVO<ProcessSccVO> handleData = this.getHandleData(taskId);
List<ProcessStandardComplianceCheckDetail> detailInfoList = handleData.getBusinessInfoDTO().getDetailInfoList();
String fileName = "";
String url = exportExcelTempPath.replace(EXPORT_EXCEL_TEMP, fileName);
File file = new File(url);
// if (!file.exists()) {
// boolean mkdirs = file.mkdirs();
// log.info("创建临时目录:{}", mkdirs);
// }
// try (FileOutputStream fos = new FileOutputStream(url + File.separator + fileName + ".xls")){
// // 获取数据
//
//
// this.settleDocuments(url, records);
//
// // 生成excel
// ExportParams exportParams = new ExportParams();
// exportParams.setType(ExcelType.HSSF);
// // 复制一份数据
// List<LawsEvaluationNotMetLibrary> newRecordsExport = new ArrayList<>(records);
// Workbook workbook = ExcelExportUtil.exportExcel(exportParams, LawsEvaluationNotMetLibrary.class, records);
// // 生成超链接
// generateHyperlink(newRecordsExport, workbook);
// // 导出excel
// workbook.write(fos);
// // 生成并导出zip
// ZipUtil.toZip(url, response.getOutputStream(), response, true);
// } catch (Exception e) {
// log.error("导出excel异常", e);
// throw new JeroBootException("导出excel异常", e);
// } finally {
// // 删除文件
// if (new File(url).exists()) {
// FileUtils.deleteFolders(url);
// }
// }
}
@Override
public void exportXlsNotDistribute(String taskId, HttpServletResponse response) {
ProcessInfoVO<ProcessSccVO> handleData = this.getHandleData(taskId);
}
private List<ProcessStandardComplianceCheckDetail> getSplitDataByStandard(ProcessStandardComplianceCheck scc) {
String standardId = scc.getStandardId();
String fileType = scc.getSplitFileSource();
@@ -6,13 +6,16 @@ import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.jero.common.aspect.annotation.Dict;
import com.jero.modules.laws.utils.ExcelExportUtilPro;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.jeecgframework.poi.excel.annotation.Excel;
import org.jeecgframework.poi.excel.annotation.ExcelCollection;
import org.springframework.format.annotation.DateTimeFormat;
import java.io.Serializable;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;
@@ -23,6 +26,7 @@ import java.util.List;
*/
@TableName(value ="laws_evaluation_not_met_library")
@Data
@EqualsAndHashCode(callSuper = false)
public class LawsEvaluationNotMetLibrary implements Serializable {
@@ -218,7 +222,11 @@ public class LawsEvaluationNotMetLibrary implements Serializable {
@ApiModelProperty(value = "附件")
@ExcelCollection(name = "", orderNum = "13")
@TableField(exist = false)
private List<NotMetFile> fileList = Collections.emptyList();
private List<ExcelExportUtilPro.FileDetails> fileList = Collections.emptyList();
public List<String> obtainFileIds() {
return file != null ? Arrays.asList(file.split(",")) : Collections.emptyList();
}
/**
* 标准ID
@@ -13,9 +13,7 @@
</sql>
<select id="pageList" resultType="com.jero.modules.assessmentLibrary.entity.LawsEvaluationNotMetLibrary">
select nm.* ,
d.standard_name,
d.standard_number
select nm.*
from laws_evaluation_not_met_library nm
left join laws_domestic_standard as d on d.id = nm.standard_id
@@ -9,19 +9,15 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jero.common.exception.JeroBootException;
import com.jero.common.system.query.QueryGenerator;
import com.jero.common.util.FileUtils;
import com.jero.common.util.MinioUtil;
import com.jero.common.util.ZipUtil;
import com.jero.modules.assessmentLibrary.entity.LawsEvaluationNotMetLibrary;
import com.jero.modules.assessmentLibrary.entity.NotMetFile;
import com.jero.modules.assessmentLibrary.mapper.LawsEvaluationNotMetLibraryMapper;
import com.jero.modules.assessmentLibrary.service.ILawsEvaluationNotMetLibraryService;
import com.jero.modules.oss.entity.OSSFile;
import com.jero.modules.oss.service.IOSSFileService;
import com.jero.modules.laws.utils.ExcelExportUtilPro;
import com.jero.modules.projectLibrary.common.AssessCommon;
import com.jero.modules.system.util.SysWaterMarkUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.usermodel.Workbook;
import org.jeecgframework.poi.excel.ExcelExportUtil;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.enmus.ExcelType;
@@ -33,9 +29,9 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* @author ThinkBook
@@ -47,12 +43,10 @@ import java.util.*;
public class LawsEvaluationNotMetLibraryServiceImpl extends ServiceImpl<LawsEvaluationNotMetLibraryMapper, LawsEvaluationNotMetLibrary>
implements ILawsEvaluationNotMetLibraryService {
@Resource
private IOSSFileService ossFileService;
@Resource
private SysWaterMarkUtil sysWaterMarkUtil;
@Resource
private LawsEvaluationNotMetLibraryMapper notMetMapper;
@Resource
private ExcelExportUtilPro excelExportUtilPro;
@Value("${jero.path.exportExcelTempPath}")
private String exportExcelTempPath;
@@ -113,7 +107,7 @@ public class LawsEvaluationNotMetLibraryServiceImpl extends ServiceImpl<LawsEval
IPage<LawsEvaluationNotMetLibrary> lawsEvaluationNotMets = this.queryPage(lawsEvaluationNotMet, 1, Integer.MAX_VALUE, req);
List<LawsEvaluationNotMetLibrary> records = lawsEvaluationNotMets.getRecords();
this.settleDocuments(url, records);
excelExportUtilPro.settleDocuments(url, records, LawsEvaluationNotMetLibrary::obtainFileIds, LawsEvaluationNotMetLibrary::setFileList);
// 生成excel
ExportParams exportParams = new ExportParams();
@@ -122,7 +116,7 @@ public class LawsEvaluationNotMetLibraryServiceImpl extends ServiceImpl<LawsEval
List<LawsEvaluationNotMetLibrary> newRecordsExport = new ArrayList<>(records);
Workbook workbook = ExcelExportUtil.exportExcel(exportParams, LawsEvaluationNotMetLibrary.class, records);
// 生成超链接
generateHyperlink(newRecordsExport, workbook);
excelExportUtilPro.generateHyperlink(newRecordsExport, workbook, LawsEvaluationNotMetLibrary::getFileList, 12);
// 导出excel
workbook.write(fos);
// 生成并导出zip
@@ -140,80 +134,80 @@ public class LawsEvaluationNotMetLibraryServiceImpl extends ServiceImpl<LawsEval
}
private static void generateHyperlink(List<LawsEvaluationNotMetLibrary> vos, Workbook workbook) {
Sheet sheet = workbook.getSheetAt(0);
// 设置字体颜色、下划线
CellStyle cellStyle = workbook.createCellStyle();
Font font = workbook.createFont();
font.setColor(IndexedColors.BLUE.getIndex());
font.setUnderline(Font.U_SINGLE);
cellStyle.setFont(font);
cellStyle.setWrapText(true);
int i = 1;
for (LawsEvaluationNotMetLibrary vo : vos) {
if (!Objects.isNull(vo.getFile()) && !vo.getFileList().isEmpty()) {
for (NotMetFile notMetFile : vo.getFileList()) {
Row row = sheet.getRow(i);
Cell cell = row.getCell(12);
cell.setCellFormula("HYPERLINK(\"" + notMetFile.getFileName() + "\", \"" + notMetFile.getFileName() + "\")");
cell.setCellStyle(cellStyle);
i += 1;
}
} else {
i += 1;
}
}
}
private void settleDocuments(String url, List<LawsEvaluationNotMetLibrary> excelVos) throws
IOException {
for (LawsEvaluationNotMetLibrary vo : excelVos) {
// 查询文件
if (StringUtils.isNotBlank(vo.getFile())) {
List<String> fileIdList = Arrays.asList(vo.getFile().split(","));
List<OSSFile> ossFileList = ossFileService.listByIds(fileIdList);
if (!ossFileList.isEmpty()) {
// 赋值
List<NotMetFile> notMetFileList = com.jero.common.util.BeanCopyUtils.copyBeanList(ossFileList, NotMetFile.class);
vo.setFileList(notMetFileList);
// 写出文件
for (OSSFile ossFile : ossFileList) {
InputStream inputStream = MinioUtil.getMinioFile(MinioUtil.getBucketName(), ossFile.getUrl());
// pdf加水印
if (StringUtils.endsWithIgnoreCase(ossFile.getFileName(), ".pdf")) {
inputStream = sysWaterMarkUtil.addWatermarkToPdf(inputStream);
}
// 导出文件
String filePath = url + File.separator + ossFile.getFileName();
writeToLocal(filePath, inputStream);
}
}
}
}
}
/**
* 将InputStream写入本地文件
*
* @param filePath
* @param input
* @throws IOException
*/
public void writeToLocal(String filePath, InputStream input)
throws IOException {
int index;
byte[] bytes = new byte[1024];
try (FileOutputStream downloadFile = new FileOutputStream(filePath)) {
while ((index = input.read(bytes)) != -1) {
downloadFile.write(bytes, 0, index);
downloadFile.flush();
}
} catch (Exception e) {
log.error("将InputStream写入本地失败:{}", e.getMessage());
} finally {
input.close();
}
}
// private static void generateHyperlink(List<LawsEvaluationNotMetLibrary> vos, Workbook workbook) {
// Sheet sheet = workbook.getSheetAt(0);
// // 设置字体颜色、下划线
// CellStyle cellStyle = workbook.createCellStyle();
// Font font = workbook.createFont();
// font.setColor(IndexedColors.BLUE.getIndex());
// font.setUnderline(Font.U_SINGLE);
// cellStyle.setFont(font);
// cellStyle.setWrapText(true);
// int i = 1;
// for (LawsEvaluationNotMetLibrary vo : vos) {
// if (!Objects.isNull(vo.getFile()) && !vo.getFileList().isEmpty()) {
// for (NotMetFile notMetFile : vo.getFileList()) {
// Row row = sheet.getRow(i);
// Cell cell = row.getCell(12);
// cell.setCellFormula("HYPERLINK(\"" + notMetFile.getFileName() + "\", \"" + notMetFile.getFileName() + "\")");
// cell.setCellStyle(cellStyle);
// i += 1;
// }
// } else {
// i += 1;
// }
// }
// }
//
// private void settleDocuments(String url, List<LawsEvaluationNotMetLibrary> excelVos) throws
// IOException {
// for (LawsEvaluationNotMetLibrary vo : excelVos) {
// // 查询文件
// if (StringUtils.isNotBlank(vo.getFile())) {
// List<String> fileIdList = Arrays.asList(vo.getFile().split(","));
// List<OSSFile> ossFileList = ossFileService.listByIds(fileIdList);
// if (!ossFileList.isEmpty()) {
// // 赋值
// List<NotMetFile> notMetFileList = com.jero.common.util.BeanCopyUtils.copyBeanList(ossFileList, NotMetFile.class);
// vo.setFileList(notMetFileList);
// // 写出文件
// for (OSSFile ossFile : ossFileList) {
// InputStream inputStream = MinioUtil.getMinioFile(MinioUtil.getBucketName(), ossFile.getUrl());
// // pdf加水印
// if (StringUtils.endsWithIgnoreCase(ossFile.getFileName(), ".pdf")) {
// inputStream = sysWaterMarkUtil.addWatermarkToPdf(inputStream);
// }
// // 导出文件
// String filePath = url + File.separator + ossFile.getFileName();
// writeToLocal(filePath, inputStream);
// }
// }
// }
// }
// }
//
// /**
// * 将InputStream写入本地文件
// *
// * @param filePath
// * @param input
// * @throws IOException
// */
// public void writeToLocal(String filePath, InputStream input)
// throws IOException {
// int index;
// byte[] bytes = new byte[1024];
// try (FileOutputStream downloadFile = new FileOutputStream(filePath)) {
// while ((index = input.read(bytes)) != -1) {
// downloadFile.write(bytes, 0, index);
// downloadFile.flush();
// }
// } catch (Exception e) {
// log.error("将InputStream写入本地失败:{}", e.getMessage());
// } finally {
// input.close();
// }
// }
}
@@ -0,0 +1,115 @@
package com.jero.modules.laws.utils;
import com.jero.common.util.BeanCopyUtils;
import com.jero.common.util.MinioUtil;
import com.jero.modules.oss.entity.OSSFile;
import com.jero.modules.oss.service.IOSSFileService;
import com.jero.modules.system.util.SysWaterMarkUtil;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.ss.usermodel.*;
import org.jeecgframework.poi.excel.annotation.Excel;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.Function;
@Component
@Slf4j
public class ExcelExportUtilPro {
@Resource
private IOSSFileService ossFileService;
@Resource
private SysWaterMarkUtil sysWaterMarkUtil;
// 文件详细信息类
@Getter
public static class FileDetails {
@ApiModelProperty(value = "id")
private String id;
@Excel(name = "附件", width = 15)
@ApiModelProperty(value = "附件")
private String fileName;
}
public <T> void generateHyperlink(List<T> items, Workbook workbook,
Function<T, List<FileDetails>> fileDetailsProvider,
Integer cellNum) {
Sheet sheet = workbook.getSheetAt(0);
CellStyle cellStyle = createHyperlinkCellStyle(workbook);
int rowIndex = 1;
for (T item : items) {
List<FileDetails> fileDetailsList = fileDetailsProvider.apply(item);
if (fileDetailsList != null && !fileDetailsList.isEmpty()) {
for (FileDetails fileDetails : fileDetailsList) {
Row row = sheet.getRow(rowIndex);
Cell cell = row.getCell(cellNum);
cell.setCellFormula("HYPERLINK(\"" + fileDetails.getFileName() + "\", \"" + fileDetails.getFileName() + "\")");
cell.setCellStyle(cellStyle);
rowIndex++;
}
} else {
rowIndex++;
}
}
}
private static CellStyle createHyperlinkCellStyle(Workbook workbook) {
CellStyle cellStyle = workbook.createCellStyle();
Font font = workbook.createFont();
font.setColor(IndexedColors.BLUE.getIndex());
font.setUnderline(Font.U_SINGLE);
cellStyle.setFont(font);
cellStyle.setWrapText(true);
return cellStyle;
}
public <T> void settleDocuments(String url, List<T> items,
Function<T, List<String>> fileIdProvider,
BiConsumer<T, List<FileDetails>> fileDetailsConsumer) throws IOException {
for (T item : items) {
List<String> fileIdList = fileIdProvider.apply(item);
if (fileIdList != null && !fileIdList.isEmpty()) {
List<OSSFile> ossFileList = ossFileService.listByIds(fileIdList);
if (!ossFileList.isEmpty()) {
List<FileDetails> fileDetailsList = BeanCopyUtils.copyBeanList(ossFileList, FileDetails.class);
fileDetailsConsumer.accept(item, fileDetailsList);
for (OSSFile ossFile : ossFileList) {
InputStream inputStream = MinioUtil.getMinioFile(MinioUtil.getBucketName(), ossFile.getUrl());
if (StringUtils.endsWithIgnoreCase(ossFile.getFileName(), ".pdf")) {
inputStream = sysWaterMarkUtil.addWatermarkToPdf(inputStream);
}
String filePath = url + File.separator + ossFile.getFileName();
writeToLocal(filePath, inputStream);
}
}
}
}
}
public void writeToLocal(String filePath, InputStream input) throws IOException {
int index;
byte[] bytes = new byte[1024];
try (FileOutputStream downloadFile = new FileOutputStream(filePath)) {
while ((index = input.read(bytes)) != -1) {
downloadFile.write(bytes, 0, index);
downloadFile.flush();
}
} catch (Exception e) {
log.error("将InputStream写入本地失败:{}", e.getMessage());
} finally {
input.close();
}
}
}