|
|
@@ -1,6 +1,8 @@
|
|
|
package com.github.jfcloud.gene.util;
|
|
|
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
+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;
|
|
|
@@ -14,17 +16,31 @@ import java.util.Map;
|
|
|
@Slf4j
|
|
|
public class WordUtil {
|
|
|
|
|
|
+ public static final String[][] XML_ESCAPE = {
|
|
|
+ {"&", "&"}, // & - ampersand
|
|
|
+ {"<", "<"}, // < - less-than
|
|
|
+ {">", ">"}, // > - greater-than
|
|
|
+ };
|
|
|
+
|
|
|
/**
|
|
|
* @param modelName 模板的名称 比如"basic.ftl"
|
|
|
* @param dataMap 传入的数据(key=ftl 中的站位的名称同时 要是String )
|
|
|
*/
|
|
|
- public static ByteArrayOutputStream exportWordOut(String modelName, Map<String, Object> dataMap) {
|
|
|
+ public static ByteArrayOutputStream exportWordOut(String modelName, JSONObject dataMap) {
|
|
|
+ Object modal = dataMap;
|
|
|
if (dataMap != null) {
|
|
|
+ //null 值转为 空
|
|
|
for (Map.Entry<String, Object> entry : dataMap.entrySet()) {
|
|
|
if (entry.getValue() == null) {
|
|
|
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);
|
|
|
}
|
|
|
|
|
|
// freemaker配置
|
|
|
@@ -37,7 +53,7 @@ public class WordUtil {
|
|
|
|
|
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
|
|
try (Writer out = new BufferedWriter(new OutputStreamWriter(outputStream, StandardCharsets.UTF_8))){
|
|
|
- configuration.getTemplate(modelName).process(dataMap, out);
|
|
|
+ configuration.getTemplate(modelName).process(modal, out);
|
|
|
return outputStream;
|
|
|
} catch (Exception e) {
|
|
|
log.error("导出word异常", e);
|
|
|
@@ -49,7 +65,7 @@ public class WordUtil {
|
|
|
* @param modelName 模板的名称 比如"basic.ftl"
|
|
|
* @param dataMap 传入的数据(key=ftl 中的站位的名称同时 要是String )
|
|
|
*/
|
|
|
- public static InputStream exportWord(String modelName, Map<String, Object> dataMap) {
|
|
|
+ public static InputStream exportWord(String modelName, JSONObject dataMap) {
|
|
|
try (ByteArrayOutputStream outputStream = exportWordOut(modelName, dataMap)){
|
|
|
if (outputStream != null) {
|
|
|
return new ByteArrayInputStream(outputStream.toByteArray());
|