认证-上报库-word自定义导出: 解决表格不识别问题

This commit is contained in:
liyawei
2022-09-15 09:03:08 +08:00
parent 00ceb5b2cd
commit 3190e8ad5a
3 changed files with 74 additions and 3 deletions
@@ -100,6 +100,70 @@ public final class Docx4jUtils {
return true;
}
/**
* cleanDocumentPart
*
* @param documentPart
*/
public static boolean cleanDocumentPartDouble(MainDocumentPart documentPart) throws Exception {
if (documentPart == null) {
return false;
}
Document document = documentPart.getContents();
String wmlTemplate =
XmlUtils.marshaltoString(document, true, false, Context.jc);
wmlTemplate = cleanXmlString(wmlTemplate);
document = (Document) XmlUtils.unwrap(DocxVariableClearUtils.doCleanDocumentPart(wmlTemplate, Context.jc));
// document = (Document) DocumentHelper.parseText(wmlTemplate);
documentPart.setContents(document);
return true;
}
public static String cleanXmlString(String string) {
StringBuilder appender = new StringBuilder();
//判断每一行数据中不包含'$'的数据先添加进新文件
if (!string.contains("$")) {
appender.append(string);
return appender.toString();
}
//如果一行数据中包含'$'变量符将替换为'#$'
string = string.replaceAll("\\$", "#\\$");
//然后以'#'切割成每一行(数组),这样一来'$'都将在每一行的开头
String[] ss = string.split("#");
//遍历每一行(数组ss
for (int i = 0; i < ss.length; i++) {
// 同一行的数据写到同一行,文件追加自动换行了(最后的完整数据)
StringBuilder sb = new StringBuilder();
//暂存数据
String s1 = ss[i];
//将不是以'$'开头的行数据放进StringBuilder
if (!s1.startsWith("$")) {
if (i > 0) {
sb.append("#");
}
sb.append(s1);
appender.append(sb.toString());
continue;
}
//被分离的数据一般都是'${'这样被分开
//匹配以'$'开头的变量找到'}' 得到索引位置
int i1 = s1.indexOf("}");
//先切割得到这个完整体
String substr = s1.substring(0, i1 + 1);
//把变量追加到StringBuilder
sb.append(substr.replaceAll("\n","").replaceAll("<[^>]+>", ""));
//再将标签数据追加到StringBuilder包裹变量
sb.append(s1.substring(i1 + 1));
appender.append(sb.toString());
}
return appender.toString();
}
/**
* 清扫 docx4j 模板变量字符,通常以${variable}形式
* <p>
@@ -59,6 +59,11 @@ public class WordUtil {
documentPart.variableReplace((HashMap<String, String>) map);
}
public static void replaceVariableWithOutClear(WordprocessingMLPackage wordMLPackage, Map<String, String> map) throws Exception {
MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
documentPart.variableReplace((HashMap<String, String>) map);
}
public static void replaceTable(String templatePath, int tableNum, int rowNum, List<Map<String, Object>> dataList, String outPath) throws Exception {
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new File(templatePath));
MainDocumentPart mainDocumentPart = wordMLPackage.getMainDocumentPart();
@@ -203,7 +208,6 @@ public class WordUtil {
public static void replacePictureBatch(WordprocessingMLPackage wordMLPackage, List<Map<String, Object>> picList) throws Exception {
try {
MainDocumentPart mainDocumentPart = wordMLPackage.getMainDocumentPart();
Docx4jUtils.cleanDocumentPart(mainDocumentPart);
Document wmlDoc = (Document)mainDocumentPart.getJaxbElement();
Body body = wmlDoc.getBody();
List<Text> textList = getAllPlaceholderElementFromObject(body); // 获取文档中所有占位符
@@ -241,7 +245,6 @@ public class WordUtil {
*/
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); // 获取文档中所有占位符