




免费预览已结束,剩余1页可下载查看
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
用 Freemarker 生成 word 文档1.用word写一个需要导出的word模板,然后存为xml格式。2.将xml中需要动态修改内容的地方,换成freemarker的标识符,例如: $year 年度工作报告 3.用代码生成:import freemarker.template.Configuration; import freemarker.template.Template; import java.io.*; import java.util.HashMap; import java.util.Map; /* * Class name: CreateDoc * * Short description on the purpose of the program. * author : youfeng * modified : 8/29/11 */ public class CreateDoc private Configuration configuration = null; public CreateDoc() configuration = new Configuration(); configuration.setDefaultEncoding(utf-8); public void create() throws Exception Map map = new HashMap(); map.put(date, 2011); map.put(modifyDate, 2011/8/29); map.put(modifyUser, Zhu You Feng); configuration.setClassForTemplateLoading(this.getClass(), /docTemplate/); Template t = configuration.getTemplate(doc1.ftl); File outFile = new File(D:/outFile.doc); Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile); cess(map, out); public static void main(String args) throws Exception new CreateDoc().create(); 添加图片如果你需要在word中添加图片,那你就在第一步制作模板时,加入一张图片占位,然后打开xml文档,可以看到如下的一片base64编码后的代码:1iVBORw0(很省略很省略)CC只要将base64的代码替换成例如:$image,如下:1$image这里要注意“$image”这尖括号中间不能加任何其他的诸如空格,tab,换行等符号。然后用代码生成:import freemarker.template.Configuration; import freemarker.template.Template; import sun.misc.BASE64Encoder; import java.io.*; import java.util.HashMap; import java.util.Map; /* * Class name: CreateDoc * author: youfeng * modified: 8/29/11 */ public class CreateDocWithImage private Configuration configuration = null; public CreateDocWithImage() configuration = new Configuration(); configuration.setDefaultEncoding(utf-8); public void create() throws Exception Map map = new HashMap(); map.put(year, 2011); map.put(person, Zhu You Feng); map.put(image, getImageStr(); configuration.setClassForTemplateLoading(this.getClass(), /docTemplate/); Template t = configuration.getTemplate(doc2.ftl); File outFile = new File(D:/outFile.doc); Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile); cess(map, out); private String getImageStr() String imgFile = d:/test.jpg; InputStream in = null; byte data = null; try in = new FileInputStream(imgFile); data = new bytein.available(); in.read(data); in.close(); catch (IOException e) e.printStackTrace(); BASE64Encoder encoder = new BASE64Encoder(); return encoder.encode(data); public static void main(String args) throws Exception new CreateDocWithImage().create(); 自定义载入模板import freemarker.template.Configuration; import freemarker.template.Template; import freemarker.template.TemplateException; import sun.misc.BASE64Encoder; import java.io.*; import java.util.HashMap; import java.util.Map; public class DocumentGenerator private Configuration configuration = null; public static void main(String args) throws Exception Map map = new HashMap(); map.put(date, 2012); map.put(modifyDate, 2011/8/29); map.put(modifyUser, Zhu You Feng); new DocumentGenerator().createDoc(, D:/outFile2.doc, map); public DocumentGenerator() configuration = new Configuration(); configuration.setDefaultEncoding(utf-8); configuration.setClassicCompatible(true); configuration.setTemplateLoader(new ByteArrayStreamTemplateLoader(new ByteArrayInputStream( getBytesFromFile(new File(D:/ownProject/freemarkerToDoc/src/main/resources/docTemplate/doc1.ftl) ); /* * param fileName * param outFileName * param dataMap */ public void createDoc(String fileName, String outFileName, Map dataMap) Template t = null; try t = configuration.getTemplate(fileName); catch (IOException e) e.printStackTrace(); File outFile = new File(outFileName); Writer out = null; try out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile); catch (FileNotFoundException e1) e1.printStackTrace(); try cess(dataMap, out); catch (TemplateException e) e.printStackTrace(); catch (IOException e) e.printStackTrace(); public byte getBytesFromFile(File f) if (f = null) return null; try FileInputStream stream = new FileInputStream(f); ByteArrayOutputStream out = new ByteArrayOutputStream(1000); byte b = new byte1000; int n; while (n = stream.read(b) != -1) out.write(b, 0, n); stream.close(); out.close(); return out.toByteArray(); catch (IOException e) e.printStackTrace(); return null; class ByteArrayStreamTemplateLoader implements TemplateLoader InputStream in = null; public ByteArrayStreamTemplateLoader(ByteArrayInputStream inputStream) in = inputStream; public Object findTemplateSource(String name) throws IOException System.out.println(findTemplateSource); return in; public long getLastModified(Object templateSource) return 0; public Reader getReader(Object templateSource,
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- GB/T 21766-2025化学品生殖/发育毒性筛选试验方法
- 2025年中国邮政极速达项目经理招聘面试指南及常见问题解答
- 2025年深圳会考试卷及答案
- 2025年四平市事业单位专项招聘高校毕业生笔试模拟试卷及答案详解(名师系列)
- 2025年河南省上蔡第一高级中学招聘教师30人考前自测高频考点模拟试题附答案详解(考试直接用)
- 2025年新能源行业安全生产标准化建设中的技术创新策略研究报告
- 2025年影视工业化制作流程与质量控制产业政策分析报告
- 八年级劳动教育 焖饭与炒饭 教学设计
- 八年级政治上册 第四单元 第9课 心有他人天地宽说课稿 新人教版
- 2025年中国高温真空袋膜行业市场分析及投资价值评估前景预测报告
- 教师晋升答辩常见问题汇编
- 新加坡安全培训题库及答案解析
- (人教A版)选择性必修一数学高二上册 第一章 空间向量与立体几何(A卷·知识通关练+B卷提升练习)(原卷版)
- 2025煤矿安全规程解读
- 初级消防员培训课程教学大纲
- 2025-2026学年北师大版数学小学三年级上册(全册)教案设计及教学计划
- 2025年“学宪法讲宪法”主题活动知识竞赛题库附答案
- 2025年党纪法规知识测试题(含答案)
- 护理伦理与法律
- 网赌网贷专题教育
- 物业出纳培训课件内容
评论
0/150
提交评论