认证-上报库-自定义导出-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
@@ -1,5 +1,6 @@
package com.jero.modules.project.util;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
@@ -40,6 +41,7 @@ import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
@Slf4j
public class ExcelUtil {
@@ -70,6 +72,33 @@ public class ExcelUtil {
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) {
SheetData sheetData = worksheetPart.getJaxbElement().getSheetData();
@@ -370,8 +399,8 @@ public class ExcelUtil {
// This seems to be about 5% faster than the Scanner approach
wmlTemplateString = IOUtils.toString(is, "UTF-8");
List<String> placeholderList = new ArrayList<>();
getAllPlaceholderElementFromObject(wmlTemplateString, 0, placeholderList);
// List<String> placeholderList = new ArrayList<>();
// getAllPlaceholderElementFromObject(wmlTemplateString, 0, placeholderList);
}
} catch (Exception 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);
if (startKey == -1)
return null;
return;
else {
int keyEnd = wmlTemplateString.indexOf('}', startKey);
String placeHolder = wmlTemplateString.substring(startKey, keyEnd + 1);
if (isPlaceholder(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;
import cn.hutool.core.collection.CollectionUtil;
import org.docx4j.Docx4J;
import org.docx4j.TraversalUtil;
import org.docx4j.XmlUtils;
@@ -28,6 +29,7 @@ import java.io.OutputStream;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
public class WordUtil {
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文档包含占位符的文本节点
* 未解决问题:两个连着的相同占位符会识别成一个。如 原文:${key1}${key1}