Java实现用Freemarker完美导出word文档(带图片)
前言
最近在项目中,因客户要求,将页面内容(如合同协议)导出成word,在网上翻了好多,感觉太乱了,不过最后还是较好解决了这个问题。
准备材料
1.word原件2.编辑器(推荐FirstobjectfreeXMLeditor)
实现步骤
1.用MicrosoftOfficeWord打开word原件;
2.把需要动态修改的内容替换成***,如果有图片,尽量选择较小的图片几十K左右,并调整好位置;
3.另存为,选择保存类型Word2003XML文档(*.xml)【这里说一下为什么用MicrosoftOfficeWord打开且要保存为Word2003XML,本人亲测,用WPS找不到Word2003XML选项,如果保存为WordXML,会有兼容问题,避免出现导出的word文档不能用Word2003打开的问题】;
4.用FirstobjectfreeXMLeditor打开文件,选择Tools下的Indent【或者按快捷键F8】格式化文件内容。左边是文档结构,右边是文档内容;
5.将文档内容中需要动态修改内容的地方,换成freemarker的标识。其实就是Map
6.在加入了图片占位的地方,会看到一片base64编码后的代码,把base64替换成${image},也就是Map
代码如:
注意:“>${image}<”这尖括号中间不能加任何其他的诸如空格,tab,换行等符号。
如果需要循环,则使用:<#listmapsasmap>#list> maps是Map
7.标识替换完之后,模板就弄完了,另存为.ftl后缀文件即可。注意:一定不要用word打开ftl模板文件,否则xml内容会发生变化,导致前面的工作白做了。
代码实现
工具类WordUtils.Java
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjava.io.InputStream;
importjava.io.OutputStreamWriter;
importjava.io.Writer;
importjava.net.URLEncoder;
importjava.util.Date;
importjava.util.Map;
importjavax.servlet.ServletOutputStream;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importfreemarker.template.Configuration;
importfreemarker.template.Template;
publicclassWordUtils{
//配置信息,代码本身写的还是很可读的,就不过多注解了
privatestaticConfigurationconfiguration=null;
//这里注意的是利用WordUtils的类加载器动态获得模板文件的位置
//privatestaticfinalStringtemplateFolder=WordUtils.class.getClassLoader().getResource("../../").getPath()+"WEB-INF/templetes/";
privatestaticfinalStringtemplateFolder="H:/我的项目/lm/lm/web/src/main/webapp/WEB-INF/templates";
static{
configuration=newConfiguration();
configuration.setDefaultEncoding("utf-8");
try{
configuration.setDirectoryForTemplateLoading(newFile(templateFolder));
}catch(IOExceptione){
e.printStackTrace();
}
}
privateWordUtils(){
thrownewAssertionError();
}
publicstaticvoidexportMillCertificateWord(HttpServletRequestrequest,HttpServletResponseresponse,Mapmap,Stringtitle,StringftlFile)throwsIOException{
TemplatefreemarkerTemplate=configuration.getTemplate(ftlFile);
Filefile=null;
InputStreamfin=null;
ServletOutputStreamout=null;
try{
//调用工具类的createDoc方法生成Word文档
file=createDoc(map,freemarkerTemplate);
fin=newFileInputStream(file);
response.setCharacterEncoding("utf-8");
response.setContentType("application/msword");
//设置浏览器以下载的方式处理该文件名
StringfileName=title+DateUtil.formatDateDetailTime(newDate())+".doc";
response.setHeader("Content-Disposition","attachment;filename="
.concat(String.valueOf(URLEncoder.encode(fileName,"UTF-8"))));
out=response.getOutputStream();
byte[]buffer=newbyte[512];//缓冲区
intbytesToRead=-1;
//通过循环将读入的Word文件的内容输出到浏览器中
while((bytesToRead=fin.read(buffer))!=-1){
out.write(buffer,0,bytesToRead);
}
}finally{
if(fin!=null)fin.close();
if(out!=null)out.close();
if(file!=null)file.delete();//删除临时文件
}
}
privatestaticFilecreateDoc(Map,?>dataMap,Templatetemplate){
Stringname="sellPlan.doc";
Filef=newFile(name);
Templatet=template;
try{
//这个地方不能使用FileWriter因为需要指定编码类型否则生成的Word文档会因为有无法识别的编码而无法打开
Writerw=newOutputStreamWriter(newFileOutputStream(f),"utf-8");
t.process(dataMap,w);
w.close();
}catch(Exceptionex){
ex.printStackTrace();
thrownewRuntimeException(ex);
}
returnf;
}
}
Action
@RequestMapping("/exportSellPlan")
public@ResponseBodyvoidexportSellPlan(Longid){
Calendarcalendar=Calendar.getInstance();//取当前日期。
if(id!=null){
SellPlanplan=sellService.getSellPlanInfo(id);
//获得数据
Mapmap=newHashMap();
map.put("bYear",plan.getBusinessYear()!=null?plan.getBusinessYear():"");
map.put("lYear",plan.getLiveYear()!=null?plan.getLiveYear():"");
map.put("leader",plan.getLeader()!=null?plan.getLeader():"");
map.put("phone",plan.getPhone()!=null?plan.getPhone():"");
map.put("curYear",calendar.get(Calendar.YEAR)+"");
map.put("image",getImageBase(plan.getPositionImage()));
try{
WordUtils.exportMillCertificateWord(getRequest(),getResponse(),map,"方案","sellPlan.ftl");
}catch(IOExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
}
Base64处理
//获得图片的base64码
@SuppressWarnings("deprecation")
publicStringgetImageBase(Stringsrc){
if(src==null||src==""){
return"";
}
Filefile=newFile(getRequest().getRealPath("/")+src.replace(getRequest().getContextPath(),""));
if(!file.exists()){
return"";
}
InputStreamin=null;
byte[]data=null;
try{
in=newFileInputStream(file);
}catch(FileNotFoundExceptione1){
e1.printStackTrace();
}
try{
data=newbyte[in.available()];
in.read(data);
in.close();
}catch(IOExceptione){
e.printStackTrace();
}
BASE64Encoderencoder=newBASE64Encoder();
returnencoder.encode(data);
}
Javascript
window.location.href="<%=path%>/exportSellPlan?id="rel="externalnofollow"+id;
结束语
如果对Freemarker标签不熟的,可以在网上先学习下,了解文档结构。
相关链接
FirstobjectfreeXMLeditor下载地址:http://www.firstobject.com/dn_editor.htm
freemarker官网:http://freemarker.org/
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。