Commit 40bfdba5 authored by 韩桐桐's avatar 韩桐桐

fix(jg):告知模板

parent 296f7938
......@@ -20,9 +20,8 @@ import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.sql.Array;
import java.util.*;
@Slf4j
public class WordTemplateUtils {
......@@ -50,7 +49,7 @@ public class WordTemplateUtils {
* @param template 模板
* @return doc文件
*/
public static File createDoc(String templatePath, Map<String, ?> dataMap, Template template) throws TemplateException, IOException {
public static File createDoc(String templatePath, List<Map<String, ?>> dataMap, Template template) throws TemplateException, IOException {
// templatePath在后缀之前加上UUID是为了防止并发时多个线程使用同一个模板文件而导致生成的Word文档内容不一致
int i = templatePath.lastIndexOf(".");
templatePath = UUID.randomUUID() + templatePath.substring(i);
......@@ -63,12 +62,16 @@ public class WordTemplateUtils {
// 这个地方不能使用FileWriter因为需要指定编码类型否则生成的Word文档会因为有无法识别的编码而无法打开
Writer writer = new OutputStreamWriter(Files.newOutputStream(docFile.toPath()), StandardCharsets.UTF_8)
) {
template.process(escapeSpecialCharacters(dataMap), writer);
ArrayList<Map<String, Object>> arrayList = new ArrayList<>();
for (Map<String, ?> stringMap : dataMap) {
arrayList.add(escapeSpecialCharacters(stringMap));
}
template.process(arrayList, writer);
}
return docFile;
}
public File fillAndConvertDocFile(String templatePath, String targetFileName, Map<String, ?> map, int saveFormat) throws Exception {
public File fillAndConvertDocFile(String templatePath, String targetFileName, List<Map<String, ?>> map, int saveFormat) throws Exception {
// 指定模板所在包路径
configuration.setClassForTemplateLoading(this.getClass(), BASE_PACKAGE_PATH);
......@@ -154,7 +157,7 @@ public class WordTemplateUtils {
return url;
}
public static void templateToPdfDownload(String pdfName, String wordPath, Map<String, Object> placeholders, HttpServletResponse response) {
public static void templateToPdfDownload(String pdfName, String wordPath, List<Map<String, Object>> placeholders, HttpServletResponse response) {
// word转pdf
File pdfFile;
try {
......@@ -219,7 +222,7 @@ public class WordTemplateUtils {
*
* @param wordPath word文件路径
*/
private static File wordToPdf(String pdfName, String wordPath, Map<String, Object> placeholders) throws Exception {
private static File wordToPdf(String pdfName, String wordPath, List<Map<String, Object>> placeholders) throws Exception {
Assert.hasText(wordPath, "word文件路径不能为空");
WordTemplateUtils instance = WordTemplateUtils.getInstance();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment