认证-上报库-word自定义导出: 解决表格不识别问题
This commit is contained in:
+5
-1
@@ -30,6 +30,7 @@ import com.jero.modules.cert.template.enums.ParamsIsMustEnum;
|
|||||||
import com.jero.modules.ocr.util.LineHumpUtil;
|
import com.jero.modules.ocr.util.LineHumpUtil;
|
||||||
import com.jero.modules.oss.entity.OSSFile;
|
import com.jero.modules.oss.entity.OSSFile;
|
||||||
import com.jero.modules.oss.service.IOSSFileService;
|
import com.jero.modules.oss.service.IOSSFileService;
|
||||||
|
import com.jero.modules.project.util.Docx4jUtils;
|
||||||
import com.jero.modules.project.util.ExcelUtil;
|
import com.jero.modules.project.util.ExcelUtil;
|
||||||
import com.jero.modules.project.util.WordUtil;
|
import com.jero.modules.project.util.WordUtil;
|
||||||
import com.jero.modules.split.common.ReadExcel;
|
import com.jero.modules.split.common.ReadExcel;
|
||||||
@@ -48,6 +49,7 @@ import org.docx4j.Docx4J;
|
|||||||
import org.docx4j.openpackaging.exceptions.Docx4JException;
|
import org.docx4j.openpackaging.exceptions.Docx4JException;
|
||||||
import org.docx4j.openpackaging.packages.SpreadsheetMLPackage;
|
import org.docx4j.openpackaging.packages.SpreadsheetMLPackage;
|
||||||
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
|
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
|
||||||
|
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.mock.web.MockMultipartFile;
|
import org.springframework.mock.web.MockMultipartFile;
|
||||||
@@ -785,6 +787,8 @@ 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);
|
||||||
|
MainDocumentPart mainDocumentPart = wordMLPackage.getMainDocumentPart();
|
||||||
|
Docx4jUtils.cleanDocumentPartDouble(mainDocumentPart);
|
||||||
|
|
||||||
//下载关联文件内容
|
//下载关联文件内容
|
||||||
List<Map<String, Object>> fileListAll = (List<Map<String, Object>>) allParamsInfoList.get(2).get("fileListAll");
|
List<Map<String, Object>> fileListAll = (List<Map<String, Object>>) allParamsInfoList.get(2).get("fileListAll");
|
||||||
@@ -838,7 +842,7 @@ public class ParamsReportDetailEOServiceImpl extends ServiceImpl<ParamsReportDet
|
|||||||
|
|
||||||
// 替换占位符内容
|
// 替换占位符内容
|
||||||
WordUtil.replacePictureBatch(wordMLPackage, imgListAll);
|
WordUtil.replacePictureBatch(wordMLPackage, imgListAll);
|
||||||
WordUtil.replaceVariable(wordMLPackage, params);
|
WordUtil.replaceVariableWithOutClear(wordMLPackage, params);
|
||||||
Docx4J.save(wordMLPackage, wordOS);
|
Docx4J.save(wordMLPackage, wordOS);
|
||||||
|
|
||||||
wordOS.flush();
|
wordOS.flush();
|
||||||
|
|||||||
+64
@@ -100,6 +100,70 @@ public final class Docx4jUtils {
|
|||||||
return true;
|
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}形式
|
* 清扫 docx4j 模板变量字符,通常以${variable}形式
|
||||||
* <p>
|
* <p>
|
||||||
|
|||||||
+5
-2
@@ -59,6 +59,11 @@ public class WordUtil {
|
|||||||
documentPart.variableReplace((HashMap<String, String>) map);
|
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 {
|
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));
|
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new File(templatePath));
|
||||||
MainDocumentPart mainDocumentPart = wordMLPackage.getMainDocumentPart();
|
MainDocumentPart mainDocumentPart = wordMLPackage.getMainDocumentPart();
|
||||||
@@ -203,7 +208,6 @@ public class WordUtil {
|
|||||||
public static void replacePictureBatch(WordprocessingMLPackage wordMLPackage, List<Map<String, Object>> picList) throws Exception {
|
public static void replacePictureBatch(WordprocessingMLPackage wordMLPackage, List<Map<String, Object>> picList) throws Exception {
|
||||||
try {
|
try {
|
||||||
MainDocumentPart mainDocumentPart = wordMLPackage.getMainDocumentPart();
|
MainDocumentPart mainDocumentPart = wordMLPackage.getMainDocumentPart();
|
||||||
Docx4jUtils.cleanDocumentPart(mainDocumentPart);
|
|
||||||
Document wmlDoc = (Document)mainDocumentPart.getJaxbElement();
|
Document wmlDoc = (Document)mainDocumentPart.getJaxbElement();
|
||||||
Body body = wmlDoc.getBody();
|
Body body = wmlDoc.getBody();
|
||||||
List<Text> textList = getAllPlaceholderElementFromObject(body); // 获取文档中所有占位符
|
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 {
|
public static Map<String, String> filterParams(WordprocessingMLPackage wordMLPackage, Map<String, String> params) throws Exception {
|
||||||
MainDocumentPart mainDocumentPart = wordMLPackage.getMainDocumentPart();
|
MainDocumentPart mainDocumentPart = wordMLPackage.getMainDocumentPart();
|
||||||
Docx4jUtils.cleanDocumentPart(mainDocumentPart);
|
|
||||||
Document wmlDoc = (Document)mainDocumentPart.getJaxbElement();
|
Document wmlDoc = (Document)mainDocumentPart.getJaxbElement();
|
||||||
Body body = wmlDoc.getBody();
|
Body body = wmlDoc.getBody();
|
||||||
List<Text> textList = getAllPlaceholderElementFromObject(body); // 获取文档中所有占位符
|
List<Text> textList = getAllPlaceholderElementFromObject(body); // 获取文档中所有占位符
|
||||||
|
|||||||
Reference in New Issue
Block a user