网站域名 评估作价,住房和城乡建设部网站主页,企业为什么审计,dedecms 网站模板freemarker导出word 第一步、将word转换为xml格式第二步、将转换后的xml文件修改后缀为ftl后复制到项目 resources 目录下#xff08;可以自己新建一个文件夹放在文件夹中#xff09;第三步、格式化xml代码#xff08;如果问价太大可能会无法格式化#xff09;这时候需要在… freemarker导出word 第一步、将word转换为xml格式第二步、将转换后的xml文件修改后缀为ftl后复制到项目 resources 目录下可以自己新建一个文件夹放在文件夹中第三步、格式化xml代码如果问价太大可能会无法格式化这时候需要在idea中进行设置第四步、ftl文件值的替换第五步、工具类POM 第一步、将word转换为xml格式
第二步、将转换后的xml文件修改后缀为ftl后复制到项目 resources 目录下可以自己新建一个文件夹放在文件夹中
第三步、格式化xml代码如果问价太大可能会无法格式化这时候需要在idea中进行设置 idea.max.intellisense.filesize10000 idea.max.content.load.filesize10000
第四步、ftl文件值的替换
${monthLowerALL!} 表示替换单个值遍历数组
#list userList as user编号${user.id} 姓名${user.username} ${user.age}br/
/#list
第五步、工具类
Slf4j
public class FreemarkerUtil {/*** 使用 Freemarker 生成 Word 文件** param templateName 模板文件路径名称* param fileName 生成的文件路径以及名称* param dataModel 填充的数据对象*/public static void exportWord(String templateName, String fileName, MapString, Object dataModel) {generateFile(templateName, fileName, dataModel);}/*** 使用 Freemarker 生成指定文件返回流** param templateName 模板文件路径名称* param fileName 生成的文件名称* param dataModel 填充的数据对象*/private static void generateFile(String templateName, String fileName, MapString, Object dataModel) {try {// 1、创建配置对象Configuration config new Configuration(Configuration.VERSION_2_3_30);config.setDefaultEncoding(utf-8);//这里 templates 为你项目中resources目录下templates文件夹相当于会拼接在你的 templateName参数前面config.setClassForTemplateLoading(FreemarkerUtil.class, /templates);// 2、获取模板文件Template template config.getTemplate(templateName, utf-8);HttpServletResponse response ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();response.setContentType(application/msword;charsetutf-8);response.setCharacterEncoding(utf-8);try {response.setHeader(Content-disposition, attachment;filename new String(fileName.getBytes(gb2312), ISO8859-1) .docx);} catch (UnsupportedEncodingException e) {e.printStackTrace();}template.process(dataModel, response.getWriter());} catch (Exception e) {e.printStackTrace();}}
}
###第六步、调用 MapString, Object dataMap Maps.newConcurrentMap();dataMap.put(totalIncomeAll, 123456789);dataMap.put(monthLowerALL, 1960);// 执行导出FreemarkerUtil.exportWord(templates路径下对应的模板名称.ftl, 导出的模板名称, dataMap);
POM
!-- 文档模板操作依赖 --dependencygroupIdfr.opensagres.xdocreport/groupIdartifactIdfr.opensagres.xdocreport.core/artifactIdversion2.0.2/version/dependencydependencygroupIdfr.opensagres.xdocreport/groupIdartifactIdfr.opensagres.xdocreport.document/artifactIdversion2.0.2/version/dependencydependencygroupIdfr.opensagres.xdocreport/groupIdartifactIdfr.opensagres.xdocreport.template/artifactIdversion2.0.2/version/dependencydependencygroupIdfr.opensagres.xdocreport/groupIdartifactIdfr.opensagres.xdocreport.document.docx/artifactIdversion2.0.2/version/dependencydependencygroupIdfr.opensagres.xdocreport/groupIdartifactIdfr.opensagres.xdocreport.template.freemarker/artifactIdversion2.0.2/version/dependency