|
|
@@ -1,8 +1,8 @@
|
|
|
package com.github.jfcloud.gene.util;
|
|
|
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
-import com.alibaba.fastjson.serializer.SerializerFeature;
|
|
|
import com.github.jfcloud.gene.handler.MyCheck;
|
|
|
import com.github.jfcloud.gene.handler.MyJsonFilter;
|
|
|
import freemarker.template.Configuration;
|
|
|
@@ -10,7 +10,9 @@ import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
|
import java.io.*;
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
+import java.util.Collection;
|
|
|
import java.util.Map;
|
|
|
+import java.util.Set;
|
|
|
|
|
|
@Slf4j
|
|
|
public class WordUtil {
|
|
|
@@ -21,12 +23,44 @@ public class WordUtil {
|
|
|
{">", ">"}, // > - greater-than
|
|
|
};
|
|
|
|
|
|
+ /**
|
|
|
+ * 转义XML特殊字符
|
|
|
+ */
|
|
|
+ private static void escapeXml(JSONObject obj) {
|
|
|
+ Set<String> keys = obj.keySet();
|
|
|
+ for (String key : keys) {
|
|
|
+ Object value = obj.get(key);
|
|
|
+ if (value instanceof String) {
|
|
|
+ String content = (String) value;
|
|
|
+ // 转义特殊字符,除了标签内容
|
|
|
+ if (StrUtil.isNotBlank(content) && !content.startsWith("<") && !content.endsWith(">")) {
|
|
|
+ for (String[] pair : XML_ESCAPE) {
|
|
|
+ content = content.replaceAll(pair[0], pair[1]);
|
|
|
+ }
|
|
|
+ obj.put(key, content);
|
|
|
+ }
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ //递归调用map
|
|
|
+ if (value instanceof Map) {
|
|
|
+ escapeXml(obj.getJSONObject(key));
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ //递归调用list
|
|
|
+ if (value instanceof Collection) {
|
|
|
+ JSONArray array = obj.getJSONArray(key);
|
|
|
+ for (int i = 0; i < array.size(); i++) {
|
|
|
+ escapeXml(array.getJSONObject(i));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* @param modelName 模板的名称 比如"basic.ftl"
|
|
|
* @param dataMap 传入的数据(key=ftl 中的站位的名称同时 要是String )
|
|
|
*/
|
|
|
public static ByteArrayOutputStream exportWordOut(String modelName, JSONObject dataMap) {
|
|
|
- Object modal = dataMap;
|
|
|
if (dataMap != null) {
|
|
|
//null 值转为 空
|
|
|
for (Map.Entry<String, Object> entry : dataMap.entrySet()) {
|
|
|
@@ -34,12 +68,8 @@ public class WordUtil {
|
|
|
entry.setValue("");
|
|
|
}
|
|
|
}
|
|
|
- String string = JSONObject.toJSONString(dataMap, SerializerFeature.WRITE_MAP_NULL_FEATURES);
|
|
|
//转义特殊符号
|
|
|
- for (String[] pair : XML_ESCAPE) {
|
|
|
- string = string.replaceAll(pair[0], pair[1]);
|
|
|
- }
|
|
|
- modal = JSONObject.parseObject(string, JSONObject.class);
|
|
|
+ escapeXml(dataMap);
|
|
|
}
|
|
|
|
|
|
// freemaker配置
|
|
|
@@ -52,7 +82,7 @@ public class WordUtil {
|
|
|
|
|
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
|
|
try (Writer out = new BufferedWriter(new OutputStreamWriter(outputStream, StandardCharsets.UTF_8))){
|
|
|
- configuration.getTemplate(modelName).process(modal, out);
|
|
|
+ configuration.getTemplate(modelName).process(dataMap, out);
|
|
|
return outputStream;
|
|
|
} catch (Exception e) {
|
|
|
log.error("导出word异常", e);
|