diff --git a/example/pom.xml b/example/pom.xml index cf71cc49..8bf45b08 100644 --- a/example/pom.xml +++ b/example/pom.xml @@ -43,7 +43,7 @@ com.github.liaochong myexcel - 2.3.1 + 2.3.4 org.freemarker diff --git a/pom.xml b/pom.xml index d26b34df..596018a8 100644 --- a/pom.xml +++ b/pom.xml @@ -10,7 +10,7 @@ com.github.liaochong myexcel - 2.3.3 + 2.3.4 jar myexcel diff --git a/src/main/java/com/github/liaochong/myexcel/core/ThymeleafExcelBuilder.java b/src/main/java/com/github/liaochong/myexcel/core/ThymeleafExcelBuilder.java index fda08798..62adceaf 100644 --- a/src/main/java/com/github/liaochong/myexcel/core/ThymeleafExcelBuilder.java +++ b/src/main/java/com/github/liaochong/myexcel/core/ThymeleafExcelBuilder.java @@ -16,17 +16,13 @@ import com.github.liaochong.myexcel.core.io.TempFileOperator; import com.github.liaochong.myexcel.core.strategy.AutoWidthStrategy; -import com.github.liaochong.myexcel.exception.ExcelBuildException; import lombok.extern.slf4j.Slf4j; import org.thymeleaf.TemplateEngine; import org.thymeleaf.context.Context; -import org.thymeleaf.templateresolver.FileTemplateResolver; +import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver; import java.io.Writer; -import java.net.URISyntaxException; -import java.net.URL; import java.nio.charset.StandardCharsets; -import java.nio.file.Paths; import java.util.Map; import java.util.Objects; @@ -41,15 +37,14 @@ public class ThymeleafExcelBuilder extends AbstractExcelBuilder { private static final TemplateEngine TEMPLATE_ENGINE; - private String realPath; + private String filePath; static { TEMPLATE_ENGINE = new TemplateEngine(); - FileTemplateResolver fileTemplateResolver = new FileTemplateResolver(); - fileTemplateResolver.setCharacterEncoding(StandardCharsets.UTF_8.name()); - fileTemplateResolver.setTemplateMode("HTML5"); - fileTemplateResolver.setCacheable(true); - TEMPLATE_ENGINE.setTemplateResolver(fileTemplateResolver); + ClassLoaderTemplateResolver classLoaderTemplateResolver = new ClassLoaderTemplateResolver(); + classLoaderTemplateResolver.setCharacterEncoding(StandardCharsets.UTF_8.name()); + classLoaderTemplateResolver.setCacheable(true); + TEMPLATE_ENGINE.setTemplateResolver(classLoaderTemplateResolver); } public ThymeleafExcelBuilder() { @@ -65,18 +60,7 @@ public ExcelBuilder template(String path) { if (path.startsWith("/")) { path = path.substring(1); } - String filePath = path; - try { - URL url = Thread.currentThread().getContextClassLoader().getResource(filePath); - if (Objects.isNull(url)) { - throw new IllegalAccessException("File path " + filePath + " is not accessible"); - } - realPath = Paths.get(url.toURI()).toAbsolutePath().toString(); - log.info("Template file:" + realPath); - } catch (IllegalAccessException | URISyntaxException e) { - throw ExcelBuildException.of("Failed to build ExcelBuilder", e); - } - + filePath = path; return this; } @@ -84,7 +68,7 @@ public ExcelBuilder template(String path) { protected void render(Map renderData, Writer out) throws Exception { Context context = new Context(); context.setVariables(renderData); - TEMPLATE_ENGINE.process(realPath, context, out); + TEMPLATE_ENGINE.process(filePath, context, out); } }