freemarker导出word带图片
导出word带图片
如果你需要在word中添加图片,那你就在第一步制作模板时,加入一张图片占位,然后打开xml文档,可以看到如下的一片base64编码后的代码:
<w:binData w:name="wordml://03000001.png" xml:space="preserve">iVBORw0…(很省略很省略)…CC</w:binData>
只要将
base64
的代码替换成例如:
${image}
,如下:
<w:binData w:name="wordml://03000001.png" xml:space="preserve">${image}</w:binData>
这里要注意“>${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; /**
* @description freemarker导出word带图片
* @author: YHZ
* @date 2013-7-9
*/
public class CreateDocWithImage { private Configuration configuration = null; public CreateDocWithImage() {
configuration = new Configuration();
configuration.setDefaultEncoding("utf-8");
} public void create() throws Exception {
Map<String, Object> map = new HashMap<String, Object>();
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)));
t.process(map, out);
} private String getImageStr() {
String imgFile = "d:/test.jpg";
InputStream in = null;
byte[] data = null;
try {
in = new FileInputStream(imgFile);
data = new byte[in.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(); }
}
地址:http://www.cnblogs.com/zhuyoufeng/archive/2011/09/01/2161558.html
freemarker导出word带图片的更多相关文章
- spring boot freemarker 导出word 带echarts图形报表
创建word文件内容如下 将word导出为xml格式 将文件后缀名改为 .ftl 在springboot项目中添加freemarker依赖 <!-- 导出word文档--> <dep ...
- java导出word(带图片)
public class CreateWordDemo { public void createDocContext(String file) throws DocumentException,IOE ...
- 使用Freemarker导出Word文档(包含图片)代码实现及总结
.personSunflowerP { background: rgba(51, 153, 0, 0.66); border-bottom: 1px solid rgba(0, 102, 0, 1); ...
- freemarker导出word文档
使用freemarker导出word文档的过程 **************************************************************************** ...
- freemarker导出word文档——WordXML格式解析
前不久,公司一个项目需要实现导出文档的功能,之前是一个同事在做,做了3个星期,终于完成了,但是在项目上线之后却发现导出的文档有问题,此时,这个同事已经离职,我自然成为接班者,要把导出功能实现,但是我看 ...
- freemarker导出word档
1.word另存为xml:2.xml文件后缀名改成ftl:3.编写完整json字符串备用:4.修改ftl中动态字段为json中对应字段名:5.编写java代码自动生成word文件:(注意:换行用< ...
- freemarker导出word的一些问题
首先,了解下freemarker导出word的流程: 参考https://www.cnblogs.com/llfy/p/9303208.html 异常一: freemarker.core.ParseE ...
- 关于使用freemarker导出word
java使用FreeMarker导出word 一. 先做一个word模板 二. 将该word文件另存为xml格式(注意是另存为,不是直接改扩展名) 三. 打开xml文件把要 ...
- SpringBoot 集成 FreeMarker 导出 Word 模板文件(底部附源码)
思路解说 word 模板文件(doc 或 docx 文件)另存为 xml 文件 将后缀 xml 改成 html:大部分文档会改成 ftl(FreeMarker 的后缀名),因为 word 文件另存为 ...
随机推荐
- Indy10.2.5的危险做法
为了排查一个Bug今天无意看了看Indy源码,结果吓了一跳.TIdIOHandler.ReadLongWord函数用于读取通讯数据并转换成LongWord类型返回,它做用了一种危险的做法可能会导致数据 ...
- GetCursorPos/WindowFromPoint/SendMessage
GetCursorPos/WindowFromPoint/SendMessage (用API函数向Edit框发送字符) GetCursorPos(mPoint); DTWND:=WindowFromP ...
- Windows的自带控件(比如TButton)大多数消息都由它自己处理,Delphi覆盖了那么多WM_函数优先级较低,一般用不上
在空窗体上放一个TButton,一个TPanel,然后把在TWinControl.WMEraseBkgnd里下断点: procedure TWinControl.WMEraseBkgnd(var Me ...
- MFC程序的消息处理顺序
MFC应用程序中处理消息的顺序 1.AfxWndProc() 该函数负责接收消息,找到消息所属的CWnd对象,然后调用AfxCallWndProc 2.AfxCallWndProc() 该 ...
- 【ASP.NET Web API教程】5.1 HTTP消息处理器
原文:[ASP.NET Web API教程]5.1 HTTP消息处理器 注:本文是[ASP.NET Web API系列教程]的一部分,如果您是第一次看本系列教程,请先看前面的内容. 5.1 HTTP ...
- python 循环中的else
众多语言中都有if else这对条件选择组合,但是在python中还有更多else使用的地方,比如说循环for,或者while都可以和else组合. 下面简单介绍一下for-else while-el ...
- Swift - 页控件(UIPageControl)的用法
使用页控件可以用来展示多个桌面.比如很多应用第一次登陆时,会在开始页面使用页控件来介绍功能,通过左右滑动来切换页. 通常我们使用UIPageControl和UIScrollView相互结合来实现多页切 ...
- Google 搜索的基本语法
★搜索引擎的选择 先简单说一下"搜索引擎的选择". 在咱们天朝,Google 屡屡被 GFW 骚扰,导致百度占了便宜,成为份额最高的搜索引擎.不过今天这篇教程,俺还是继续拿 Goo ...
- AWS(0) - Amazon Web Services
Computer EC2 – Virtual Servers in the Cloud EC2 Container Service – Run and Manage Docker Containers ...
- 成都Java培训机构太多,该如何选择呢?
Java培训的势头愈发火热.越来越多的人看到了Java培训的前途所在,可是最好的Java培训机构是哪家呢?如何推断一家Java培训机构的专业性呢?140610lscs" target=&qu ...