大型电商网站建设公司,商贸公司商标logo设计,邮箱注册过的网站查询,怎样做一个简单的网站java根据模板导出word
日常开发中#xff0c;常常会遇到各种各样的表格进行导出#xff0c;比较好的办法就是提前弄好word模版#xff0c;再通过遍历的方式进行导出文档
1、制作word模版
模版编写 内容替换
目标下面模版进行多页展示 将word转换成xml 将xml格式化 再将x…java根据模板导出word
日常开发中常常会遇到各种各样的表格进行导出比较好的办法就是提前弄好word模版再通过遍历的方式进行导出文档
1、制作word模版
模版编写 内容替换
目标下面模版进行多页展示 将word转换成xml 将xml格式化 再将xml改成ftl
xml格式化地址 list找到起始位置和结束位置
起始位置插入#list list as l 结束位置插入/#list 2、java代码 dependencygroupIdorg.freemarker/groupIdartifactIdfreemarker/artifactIdversion2.3.28/version/dependencyExportWordFtl.java
package com.liuhm.utils;import freemarker.template.Configuration;
import freemarker.template.Template;
import org.junit.platform.commons.util.StringUtils;import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;/*** * ClassNameExportWordFtl* Description: TODO* Author: liuhaomin* Date: 2023/7/21 16:02*/
public class ExportWordFtl {/*** 生成word文件* param dataMap word中需要展示的动态数据用map集合来保存* param templateName word模板名称例如test.ftl* param filePath 文件生成的目标路径例如D:/wordFile/* param fileName 生成的文件名称例如test.doc*/SuppressWarnings(unchecked)public static File createWord(Map dataMap, String templateName, String filePath, String fileName){try {//创建配置实例Configuration configuration new Configuration();//设置编码configuration.setDefaultEncoding(UTF-8);//ftl模板文件configuration.setClassForTemplateLoading(ExportWordFtl.class,/);//获取模板Template template configuration.getTemplate(templateName);//输出文件File outFile ;if(StringUtils.isBlank(filePath)){outFile new File(fileName);}else {outFile new File(filePath File.separatorfileName);//如果输出目标文件夹不存在则创建if (!outFile.getParentFile().exists()){outFile.getParentFile().mkdirs();}}//将模板和数据模型合并生成文件Writer out new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile),UTF-8));//生成文件template.process(dataMap, out);//关闭流out.flush();out.close();return outFile;} catch (Exception e) {e.printStackTrace();}return null;}public static void main(String[] args) {ListMapString, Object list new ArrayList();for (int i 0; i 10 ; i) {MapString, Object map new HashMap();map.put(datasubmit_updated_time,2023-07-02 12:12:12);Date date new Date();map.put(nyliq,date);map.put(yhorq,单位名称i);map.put(xiikv,隐患类型i);map.put(kuzjg,隐患整改通知书类型i);map.put(hnvld,隐患详情i);map.put(gnbcv,date);map.put(malio,date);map.put(skpfm,整改情况告知书编号i);map.put(vgnje,整改状态i);map.put(vutap,备注i);list.add(map);}MapString,Object dataMap new HashMapString, Object();dataMap.put(list, list);try {String path C:\\Users\\Administrator\\Desktop\\;
// path URLDecoder.decode(Test.class.getClassLoader().getResource().getPath(), UTF8);File file ExportWordFtl.createWord(dataMap, template/demo.ftl, path, demo-t.doc);System.out.println(文档生成成功路径为 file.getAbsolutePath() );} catch (Exception e) {e.printStackTrace();}}
}测试
执行main方法
生成文件 所有的文件格式正确
博客地址
代码下载
下面的export-template-word