




免费预览已结束,剩余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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025广东广州市天河区银河小学招聘编外聘用制专任教师1人笔试参考题库附答案解析
- 2025年松江区教育卫生下属事业单位编外用工人员公开招聘笔试模拟试题及答案解析
- 2025贵州交投中石化能源发展有限责任公司招聘笔试模拟试题及答案解析
- 2025江苏中吴环保产业发展有限公司电镀产业园运营总监岗招聘2人笔试参考题库附答案解析
- 2025浙江嘉兴市海盐县教育局下属公办幼儿园招聘劳动合同制教职工(教师、卫生保健员)17人考试参考题库附答案解析
- 2025重庆公共运输职业学院招聘21人考试参考题库附答案解析
- 2026届海南省定安中学化学高一上期中学业水平测试试题含解析
- 吉林省吉林市示范初中2026届化学高二上期末质量检测试题含答案
- 2026届江苏省扬州、泰州、淮安、南通、徐州、宿迁、连云港市化学高二第一学期期末考试模拟试题含答案
- 江西省抚州市临川第二中学2026届高一化学第一学期期中质量跟踪监视试题含解析
- 卫浴设备安装技能的培训与认证
- 废气处理工程协议
- DZ∕T 0214-2020 矿产地质勘查规范 铜、铅、锌、银、镍、钼(正式版)
- 应急管理信息化系统建设方案
- 学校幼儿园消防安全风险自查检查指南
- 政府利用短视频平台宣传政策的成功案例分析
- 非煤矿山危险和有害因素之中毒和窒息
- 船员劳动合同
- 2024年中国人寿:养老险总公司招聘笔试参考题库含答案解析
- 知识产权风险预警项目分析报告
- 南城一中高三年级工作计划
评论
0/150
提交评论