认证-上报库-自定义导出-bug57359

This commit is contained in:
liyawei
2022-08-09 15:50:50 +08:00
parent ab6cbc0812
commit 34e3f6a3bf
4 changed files with 164 additions and 32 deletions
@@ -726,16 +726,6 @@ public class ParamsReportDetailEOServiceImpl extends ServiceImpl<ParamsReportDet
// 替换模板中的占位符 // 替换模板中的占位符
try { try {
//下载关联文件内容
List<OSSFile> allRelevFileList = (List<OSSFile>) allParamsInfoList.get(2).get("fileListAll");
if (allRelevFileList != null && !allRelevFileList.isEmpty()) {
allRelevFileList = allRelevFileList.stream().distinct().collect(Collectors.toList());
String exportFile = "导出文件";
if (CutEnum.EN.getValue().equals(paramsReportDetailVO.getCut())) {
exportFile = "Export file";
}
downLoadFileList(allRelevFileList,fileNowPath + File.separator + exportFile);
}
// 拿取模板 // 拿取模板
String repFileName = fileName.replaceAll("/","_"); String repFileName = fileName.replaceAll("/","_");
@@ -743,6 +733,35 @@ public class ParamsReportDetailEOServiceImpl extends ServiceImpl<ParamsReportDet
if (CosBootUtil.doesObjectExist(templateUrl)) { if (CosBootUtil.doesObjectExist(templateUrl)) {
InputStream wordTemplate = CosBootUtil.download(templateUrl); InputStream wordTemplate = CosBootUtil.download(templateUrl);
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(wordTemplate); WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(wordTemplate);
//下载关联文件内容
List<Map<String, Object>> fileListAll = (List<Map<String, Object>>) allParamsInfoList.get(2).get("fileListAll");
if (fileListAll != null && !fileListAll.isEmpty()) {
Map<String,String> paramsAfterFilter = WordUtil.filterParams(wordMLPackage, params);
if (CollectionUtil.isNotEmpty(paramsAfterFilter)) {
List<String> nioNumberList = paramsAfterFilter.keySet().stream().collect(Collectors.toList());
List<OSSFile> allRelevFileList = new ArrayList<>();
for (Map<String, Object> fileMap : fileListAll) {
for (Map.Entry map : fileMap.entrySet()) {
String key = (String) map.getKey();
List<OSSFile> fileList = (List<OSSFile>) map.getValue();
if (nioNumberList.contains(key)) {
allRelevFileList.addAll(fileList);
}
}
}
if (CollectionUtil.isNotEmpty(allRelevFileList)) {
String exportFile = "导出文件";
if (CutEnum.EN.getValue().equals(paramsReportDetailVO.getCut())) {
exportFile = "Export file";
}
downLoadFileList(allRelevFileList, fileNowPath + File.separator + exportFile);
}
}
}
// 替换占位符内容 // 替换占位符内容
WordUtil.replacePictureBatch(wordMLPackage, imgListAll); WordUtil.replacePictureBatch(wordMLPackage, imgListAll);
WordUtil.replaceVariable(wordMLPackage, params); WordUtil.replaceVariable(wordMLPackage, params);
@@ -837,29 +856,47 @@ public class ParamsReportDetailEOServiceImpl extends ServiceImpl<ParamsReportDet
// 替换模板中的占位符 // 替换模板中的占位符
try { try {
//下载关联文件内容
List<OSSFile> allRelevFileList = (List<OSSFile>) allParamsInfoList.get(2).get("fileListAll");
if (allRelevFileList != null && !allRelevFileList.isEmpty()) {
allRelevFileList = allRelevFileList.stream().distinct().collect(Collectors.toList());
String exportFile = "导出文件";
if (CutEnum.EN.getValue().equals(paramsReportDetailVO.getCut())) {
exportFile = "Export file";
}
downLoadFileList(allRelevFileList,fileNowPath + File.separator + exportFile);
}
// 拿取模板 // 拿取模板
String repFileName = fileName.replaceAll("/","_"); String repFileName = fileName.replaceAll("/","_");
wordOS = new FileOutputStream(fileNowPath + File.separator + repFileName); wordOS = new FileOutputStream(fileNowPath + File.separator + repFileName);
if (CosBootUtil.doesObjectExist(templateUrl)) { if (CosBootUtil.doesObjectExist(templateUrl)) {
InputStream wordTemplate = CosBootUtil.download(templateUrl); InputStream exceltemplateForDocx4j = CosBootUtil.download(templateUrl);
// com.qcloud.cos.utils.IOUtils.copy(wordTemplate, wordOS); // com.qcloud.cos.utils.IOUtils.copy(wordTemplate, wordOS);
InputStream excelTemplateForPoi = CosBootUtil.download(templateUrl);
SpreadsheetMLPackage pkg = SpreadsheetMLPackage.load(exceltemplateForDocx4j);
//下载关联文件内容
List<Map<String, Object>> fileListAll = (List<Map<String, Object>>) allParamsInfoList.get(2).get("fileListAll");
if (fileListAll != null && !fileListAll.isEmpty()) {
Map<String,String> paramsAfterFilter = ExcelUtil.filterParams(pkg, params);
if (CollectionUtil.isNotEmpty(paramsAfterFilter)) {
List<String> nioNumberList = paramsAfterFilter.keySet().stream().collect(Collectors.toList());
List<OSSFile> allRelevFileList = new ArrayList<>();
for (Map<String, Object> fileMap : fileListAll) {
for (Map.Entry map : fileMap.entrySet()) {
String key = (String) map.getKey();
List<OSSFile> fileList = (List<OSSFile>) map.getValue();
if (nioNumberList.contains(key)) {
allRelevFileList.addAll(fileList);
}
}
}
if (CollectionUtil.isNotEmpty(allRelevFileList)) {
String exportFile = "导出文件";
if (CutEnum.EN.getValue().equals(paramsReportDetailVO.getCut())) {
exportFile = "Export file";
}
downLoadFileList(allRelevFileList, fileNowPath + File.separator + exportFile);
}
}
}
// 替换占位符内容 // 替换占位符内容
// docx4j方式 // docx4j方式
InputStream excelTemplate = CosBootUtil.download(templateUrl); Workbook workbook = WorkbookFactory.create(excelTemplateForPoi);
SpreadsheetMLPackage pkg = SpreadsheetMLPackage.load(wordTemplate);
Workbook workbook = WorkbookFactory.create(excelTemplate);
ExcelUtil.replacePictureBatchPOI(workbook, pkg, imgListAll, params); ExcelUtil.replacePictureBatchPOI(workbook, pkg, imgListAll, params);
ExcelUtil.variableReplace(pkg, params); ExcelUtil.variableReplace(pkg, params);
pkg.save(wordOS); pkg.save(wordOS);
@@ -1092,7 +1129,7 @@ public class ParamsReportDetailEOServiceImpl extends ServiceImpl<ParamsReportDet
Map<String, Object> resultMap = new HashMap<>(); Map<String, Object> resultMap = new HashMap<>();
List<Map<String, Object>> imgListAll = new ArrayList<>(); // 存放图片信息 List<Map<String, Object>> imgListAll = new ArrayList<>(); // 存放图片信息
List<OSSFile> fileListAll = new ArrayList<>(); // 存放文件 List<Map<String, Object>> fileListAll = new ArrayList<>(); // 存放文件
String fileTypeStr = ".png,.PNG,.jfif,.JFIF,.pjpeg,.PJPEG,.jpeg,.JPEG,.pjp,.PJP,.jpg,.JPG"; String fileTypeStr = ".png,.PNG,.jfif,.JFIF,.pjpeg,.PJPEG,.jpeg,.JPEG,.pjp,.PJP,.jpg,.JPG";
// 查询配置列数据 // 查询配置列数据
for (Map<String, Object> record1 : dataList) { for (Map<String, Object> record1 : dataList) {
@@ -1125,13 +1162,16 @@ public class ParamsReportDetailEOServiceImpl extends ServiceImpl<ParamsReportDet
fileNameList.add(ossFileList.get(0).getFileName()); fileNameList.add(ossFileList.get(0).getFileName());
fileList.addAll(ossFileList); fileList.addAll(ossFileList);
Map<String, Object> fileMap = new HashMap<>();
fileMap.put(nioNumber, ossFileList);
fileListAll.add(fileMap);
} else { } else {
imgFileList.addAll(ossFileList); imgFileList.addAll(ossFileList);
} }
} }
} }
} }
fileListAll.addAll(fileList);
StringBuilder configDataBuilder = new StringBuilder(); // 重新组合配置数据 StringBuilder configDataBuilder = new StringBuilder(); // 重新组合配置数据
if ((ControlTypeEnum.FILE.getValue().equals(controlType) if ((ControlTypeEnum.FILE.getValue().equals(controlType)
@@ -246,6 +246,39 @@ public class ParamsInfoEOServiceImpl extends ServiceImpl<ParamsInfoEOMapper, Par
return updateById(paramsInfoEO); return updateById(paramsInfoEO);
} }
public void verifyForAddOrEdit (String paramsNumber, String paramsName, String cut) {
// 编号,参数名称校验
if (StringUtils.isNotBlank(paramsNumber)) {
String numberRegex = "^[A-Za-z0-9-/. ]+$";
if (!paramsNumber.matches(numberRegex)) {
if (CutEnum.CN.getValue().equals(cut)) {
throw new JeroBootException("编号只能输入字母,数字,横杠(-),小数点(.),左斜杠,空格");
} else {
throw new JeroBootException("Number enter only letters, numbers, dash (-),dot (.),left slash,spaces");
}
}
if (paramsNumber.length() > Long.parseLong("15")) {
if (CutEnum.CN.getValue().equals(cut)) {
throw new JeroBootException("编号不能超过15个字符");
} else {
throw new JeroBootException("Number can not exceed 15 chars");
}
}
}
if (StringUtils.isNotBlank(paramsName)) {
if (paramsName.length() > Long.parseLong("100")) {
if (CutEnum.CN.getValue().equals(cut)) {
throw new JeroBootException("参数名称不能超过100个字符");
} else {
throw new JeroBootException("Params Name can not exceed 100 chars");
}
}
}
}
/** /**
* 通过id删除 * 通过id删除
* *
@@ -1,5 +1,6 @@
package com.jero.modules.project.util; package com.jero.modules.project.util;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.ObjectUtil;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
@@ -40,6 +41,7 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import java.util.stream.Collectors;
@Slf4j @Slf4j
public class ExcelUtil { public class ExcelUtil {
@@ -70,6 +72,33 @@ public class ExcelUtil {
System.out.println("mmmmmmm"); System.out.println("mmmmmmm");
} }
/**
* 过滤出需要导出的
* @param pkg
* @param params
* @return
* @throws Exception
*/
public static Map<String, String> filterParams(SpreadsheetMLPackage pkg, Map<String, String> params) throws Exception {
JaxbSmlPart smlPart = (JaxbSmlPart)pkg.getParts().get(new PartName("/xl/sharedStrings.xml")); // 获取所有sheet中的文本
String wmlTemplateString = getWmlTemplateString(smlPart);
List<String> placeholderList = new ArrayList<>();
getAllPlaceholderElementFromObject(wmlTemplateString, 0, placeholderList); // 获取文档中所有占位符
Map<String, String> paramsAfterFilter = new HashMap<>();
List<String> textValueList = placeholderList.stream().distinct().collect(Collectors.toList());
if (CollectionUtil.isNotEmpty(textValueList)) {
for (Map.Entry<String, String> map : params.entrySet()) {
String value = "${" + map.getKey() + "}";
if (textValueList.contains(value)) {
paramsAfterFilter.put(map.getKey(), map.getValue());
}
}
}
return paramsAfterFilter;
}
public static void setRowHeight(WorksheetPart worksheetPart, int rIndex, double height, int cIndex, String text) { public static void setRowHeight(WorksheetPart worksheetPart, int rIndex, double height, int cIndex, String text) {
SheetData sheetData = worksheetPart.getJaxbElement().getSheetData(); SheetData sheetData = worksheetPart.getJaxbElement().getSheetData();
@@ -370,8 +399,8 @@ public class ExcelUtil {
// This seems to be about 5% faster than the Scanner approach // This seems to be about 5% faster than the Scanner approach
wmlTemplateString = IOUtils.toString(is, "UTF-8"); wmlTemplateString = IOUtils.toString(is, "UTF-8");
List<String> placeholderList = new ArrayList<>(); // List<String> placeholderList = new ArrayList<>();
getAllPlaceholderElementFromObject(wmlTemplateString, 0, placeholderList); // getAllPlaceholderElementFromObject(wmlTemplateString, 0, placeholderList);
} }
} catch (Exception e) { } catch (Exception e) {
log.error(e.getMessage(), e); log.error(e.getMessage(), e);
@@ -381,18 +410,18 @@ public class ExcelUtil {
} }
} }
private static List<String> getAllPlaceholderElementFromObject(String wmlTemplateString, int offset, List<String> placeholderList) { private static void getAllPlaceholderElementFromObject(String wmlTemplateString, int offset, List<String> placeholderList) {
int startKey = wmlTemplateString.indexOf("${", offset); int startKey = wmlTemplateString.indexOf("${", offset);
if (startKey == -1) if (startKey == -1)
return null; return;
else { else {
int keyEnd = wmlTemplateString.indexOf('}', startKey); int keyEnd = wmlTemplateString.indexOf('}', startKey);
String placeHolder = wmlTemplateString.substring(startKey, keyEnd + 1); String placeHolder = wmlTemplateString.substring(startKey, keyEnd + 1);
if (isPlaceholder(placeHolder)) { if (isPlaceholder(placeHolder)) {
placeholderList.add(placeHolder); placeholderList.add(placeHolder);
} }
return getAllPlaceholderElementFromObject(wmlTemplateString, keyEnd + 1, placeholderList); getAllPlaceholderElementFromObject(wmlTemplateString, keyEnd + 1, placeholderList);
} }
} }
@@ -1,5 +1,6 @@
package com.jero.modules.project.util; package com.jero.modules.project.util;
import cn.hutool.core.collection.CollectionUtil;
import org.docx4j.Docx4J; import org.docx4j.Docx4J;
import org.docx4j.TraversalUtil; import org.docx4j.TraversalUtil;
import org.docx4j.XmlUtils; import org.docx4j.XmlUtils;
@@ -28,6 +29,7 @@ import java.io.OutputStream;
import java.util.*; import java.util.*;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import java.util.stream.Collectors;
public class WordUtil { public class WordUtil {
private static final Logger log = LoggerFactory.getLogger(WordUtil.class); private static final Logger log = LoggerFactory.getLogger(WordUtil.class);
@@ -230,6 +232,34 @@ public class WordUtil {
} }
/**
* 过滤出需要导出的
* @param wordMLPackage
* @param params
* @return
* @throws Exception
*/
public static Map<String, String> filterParams(WordprocessingMLPackage wordMLPackage, Map<String, String> params) throws Exception {
MainDocumentPart mainDocumentPart = wordMLPackage.getMainDocumentPart();
Docx4jUtils.cleanDocumentPart(mainDocumentPart);
Document wmlDoc = (Document)mainDocumentPart.getJaxbElement();
Body body = wmlDoc.getBody();
List<Text> textList = getAllPlaceholderElementFromObject(body); // 获取文档中所有占位符
Map<String, String> paramsAfterFilter = new HashMap<>();
List<String> textValueList = textList.stream().map(Text::getValue).distinct().collect(Collectors.toList());
if (CollectionUtil.isNotEmpty(textValueList)) {
for (Map.Entry<String, String> map : params.entrySet()) {
String value = "${" + map.getKey() + "}";
if (textValueList.contains(value)) {
paramsAfterFilter.put(map.getKey(), map.getValue());
}
}
}
return paramsAfterFilter;
}
/** /**
* 发现docx文档包含占位符的文本节点 * 发现docx文档包含占位符的文本节点
* 未解决问题:两个连着的相同占位符会识别成一个。如 原文:${key1}${key1} * 未解决问题:两个连着的相同占位符会识别成一个。如 原文:${key1}${key1}