一、目的
这个功能已经完成很久了,一直没有想起来去整理成文,今天就把它整理出来,供大家参考和批评。这个导出功能也研究了好几天,同时也看了很多人写的博客,真的是千篇一律,好多都不是我想要的功能,为什么要做导出word呢,肯定是工作需要了,是将公司的博文导出来,这个博文,因为是用的ueditor在线编辑器,肯定会将很多样式存到数据库中,这里面导出处理就会很麻烦。
二、准备工作
网上我看了好多关于导出word文档的方法,最常见的有poi(应该能实现,没去尝试)、java-jacob(jacob需要调用本地的dll,而且在linux上市不能用的,只能在windows平台下运行)、itext生成rtf(不清楚)、freemark(本文方法),最初我也只是尝试过java-jacob但是只能支持windows,现在一般的服务器都是linux,所以肯定pass。
所需jar包:freemark-2.3.15.jar
三、正文
用freemark我们知道它是根据ftl模板来生成对应的word文件的,可以是xml格式,可以是html、也可以是mht格式,但最终都能通过修改后缀名来形成word文档,而Microsoft Word 都能识别,只要你生成的几种格式没有语法错误。xml格式应该是word文件流标准版,而html是网页形式,mht单一文件形式(css、图片等都包含在内)。
用xml确实能实现,但是工作量非常巨大,因为字体格式只能通过<w:rFonts><wx:font>来描述,所以这就面临一个问题,我们的数据源是带有css样式的,我们不可能再单独把css样式拿出来描述一遍,工作量巨大,如果强制性把数据源放入<w:t></w:t>标签,可想而知肯定会报错。
所以实现标准版显然是不可能了,而且我们还要导出图片呢,尝试html
首先建立一个ftl文件,blog.ftl
<html xmlns:v="urn:schemas-microsoft-com:vml"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:w="urn:schemas-microsoft-com:office:word"
xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv=Content-Type content="text/html; charset=utf-8">
<meta name=ProgId content=Word.Document>
<meta name=Generator content="Microsoft Word 11">
<meta name=Originator content="Microsoft Word 11">
<link rel=File-List href="blog.files/filelist.xml">
<title>${title}</title>
<!--[if gte mso 9]><xml>
<o:DocumentProperties>
<o:Author>User</o:Author>
<o:LastAuthor>User</o:LastAuthor>
<o:Revision>2</o:Revision>
<o:TotalTime>0</o:TotalTime>
<o:Created>2014-07-28T01:43:00Z</o:Created>
<o:LastSaved>2014-07-28T01:43:00Z</o:LastSaved>
<o:Pages>1</o:Pages>
<o:Words>10</o:Words>
<o:Characters>59</o:Characters>
<o:Company>微软中国</o:Company>
<o:Lines>1</o:Lines>
<o:Paragraphs>1</o:Paragraphs>
<o:CharactersWithSpaces>68</o:CharactersWithSpaces>
<o:Version>11.9999</o:Version>
</o:DocumentProperties>
</xml><![endif]--><!--[if gte mso 9]><xml>
<w:WordDocument>
<w:SpellingState>Clean</w:SpellingState>
<w:GrammarState>Clean</w:GrammarState>
<w:PunctuationKerning/>
<w:DrawingGridVerticalSpacing>7.8 磅</w:DrawingGridVerticalSpacing>
<w:DisplayHorizontalDrawingGridEvery>0</w:DisplayHorizontalDrawingGridEvery>
<w:DisplayVerticalDrawingGridEvery>2</w:DisplayVerticalDrawingGridEvery>
<w:ValidateAgainstSchemas/>
<w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid>
<w:IgnoreMixedContent>false</w:IgnoreMixedContent>
<w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText>
<w:Compatibility>
<w:SpaceForUL/>
<w:BalanceSingleByteDoubleByteWidth/>
<w:DoNotLeaveBackslashAlone/>
<w:ULTrailSpace/>
<w:DoNotExpandShiftReturn/>
<w:AdjustLineHeightInTable/>
<w:BreakWrappedTables/>
<w:SnapToGridInCell/>
<w:WrapTextWithPunct/>
<w:UseAsianBreakRules/>
<w:DontGrowAutofit/>
<w:UseFELayout/>
</w:Compatibility>
<w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel>
</w:WordDocument>
</xml><![endif]--><!--[if gte mso 9]><xml>
<w:LatentStyles DefLockedState="false" LatentStyleCount="156">
</w:LatentStyles>
</xml><![endif]-->
<style>
<!--
/* Font Definitions */
@font-face
{font-family:宋体;
panose-1:2 1 6 0 3 1 1 1 1 1;
mso-font-alt:SimSun;
mso-font-charset:134;
mso-generic-font-family:auto;
mso-font-pitch:variable;
mso-font-signature:3 135135232 16 0 262145 0;}
@font-face
{font-family:"\@宋体";
panose-1:2 1 6 0 3 1 1 1 1 1;
mso-font-charset:134;
mso-generic-font-family:auto;
mso-font-pitch:variable;
mso-font-signature:3 135135232 16 0 262145 0;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{mso-style-parent:"";
margin:0cm;
margin-bottom:.0001pt;
text-align:justify;
text-justify:inter-ideograph;
mso-pagination:none;
font-size:10.5pt;
mso-bidi-font-size:10.0pt;
font-family:"Times New Roman";
mso-fareast-font-family:宋体;
mso-font-kerning:1.0pt;}
/* Page Definitions */
@page
{mso-page-border-surround-header:no;
mso-page-border-surround-footer:no;}
@page Section1
{size:595.3pt 841.9pt;
margin:72.0pt 90.0pt 72.0pt 90.0pt;
mso-header-margin:42.55pt;
mso-footer-margin:49.6pt;
mso-paper-source:0;
layout-grid:15.6pt;}
div.Section1
{page:Section1;}
.tag {
background-color: #aab5c3;
border-radius: 10px;
color: #fff;
display: inline-block;
margin: 0 5px 5px 0;
padding: 0 10px;
text-decoration: none;
}
-->
</style>
<!--[if gte mso 10]>
<style>
/* Style Definitions */
table.MsoNormalTable
{mso-style-name:普通表格;
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-parent:"";
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-para-margin:0cm;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:10.0pt;
font-family:"Times New Roman";
mso-ansi-language:#0400;
mso-fareast-language:#0400;
mso-bidi-language:#0400;}
</style>
<![endif]-->
</head>
<body lang=ZH-CN style='tab-interval:21.0pt;text-justify-trim:punctuation'>
<div class=Section1 style='layout-grid:15.6pt'>
<p class=MsoNormal align=center style='text-align:center;mso-outline-level:1'>
<span style='font-size:22.5pt;mso-bidi-font-size:10.0pt;font-family:宋体;mso-ascii-font-family:"Times New Roman";mso-hansi-font-family:"Times New Roman"'>${title}</span>
<span lang=EN-US style='font-size:22.5pt;mso-bidi-font-size:10.0pt;font-family:宋体; mso-hansi-font-family:"Times New Roman"'>
<o:p></o:p>
</span>
</p>
<p class=MsoNormal align=center style='text-align:center;mso-outline-level:1'>
<#if category??>
<b style='mso-bidi-font-weight:normal'>
<span style='font-family:宋体'>博文属于:</span>
</b>
<span style='font-family:宋体'>${category}</span>
</#if>
<#if classifyName??>
<b style='mso-bidi-font-weight:normal'>
<span style='font-family:宋体'>博文分类标签:</span>
</b>
<span style='font-family:宋体'>${classifyName}</span>
</#if>
<span lang=EN-US style='font-size:12.0pt;mso-bidi-font-size:10.0pt;font-family:宋体'>
<o:p></o:p>
</span>
</p>
<p class=MsoNormal align=center style='text-align:center;mso-outline-level:1'>
<#if allTagName??>
<#list allTagName as tagName>
<b style='mso-bidi-font-weight:normal'>
<span class='tag' style='font-family:宋体'>${tagName}</span>
</b>
</#list>
</#if>
<span lang=EN-US style='font-size:12.0pt;mso-bidi-font-size:10.0pt;font-family:宋体'>
<o:p></o:p>
</span>
</p>
<p class=MsoNormal align=left style='text-align:left;mso-outline-level:1'>
<span style='font-size:12.0pt;font-family:宋体;mso-ascii-font-family:"Times New Roman";mso-hansi-font-family:"Times New Roman"'>
${content}
</span>
</p>
</div>
</body>
</html>
${}代表我们想要填入得内容
BlogHandler.java
public class BlogHandler{
private Configuration configuration = null;
public Map<String,Object> dataMap = new HashMap<String,Object>();//填充的数据
public final static String TITLE = "title";
public final static String CATEGORY = "category";
public final static String CLASSIFYNAME = "classifyName";
public final static String CONTENT = "content";
public final static String ALLTAGNAME = "allTagName";
public BlogHandler() {
configuration = new Configuration();
configuration.setDefaultEncoding("utf-8");
}
public void createDoc(Writer out) {
//设置模本装置方法和路径,FreeMarker支持多种模板装载方法。可以重servlet,classpath,数据库装载,
//模板
configuration.setClassForTemplateLoading(this.getClass(), "/com/yuqiaotech/pms/util");
Template t=null;
try {
//test.ftl为要装载的模板
t = configuration.getTemplate("blog.ftl");
t.process(dataMap, out);
} catch (TemplateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
/*
* <img/>标签src替换为加上域名的链接
*/
public String exchangeImg(String content){
String regex = "<img[^>]+src\\s*=\\s*['\"]([^'\"]+)['\"][^>]*>";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(content);
String src = "";
String scheme = getRequest().getScheme() + "://"
+ getRequest().getServerName() + ":" + getRequest().getServerPort();
//避免重复的图片链接
HashSet<String> set = new HashSet<String>();
while (matcher.find()) {
src = matcher.group(1);
set.add(src);
}
for(String str : set) {
if(str.contains(scheme)){
continue;
}
content = content.replace(str, scheme+str);
}
return content;
}
}
}
BlogAction.java
public class BlogAction extends BaseAction {
public void exportBlog() throws UnsupportedEncodingException {
String title = article.getTitle();
getResponse().addHeader("Content-Disposition","attachment;filename=" + new String(title.getBytes("GBK"), "iso-8859-1") + ".doc");
getResponse().setContentType("application/x-download");//pplication/x-download
getResponse().setCharacterEncoding("utf-8");
PrintWriter output = null;
try {
output = getResponse().getWriter();
BlogHandler handler = new BlogHandler();
handler.dataMap.put(BlogHandler.TITLE, article.getTitle());
handler.dataMap.put(BlogHandler.CATEGORY, article.getCategory());
handler.dataMap.put(BlogHandler.CLASSIFYNAME, article.getClassifyName());
handler.dataMap.put(BlogHandler.CONTENT, handler.exchangeImg(article.getContent()));
String hql = "select tagName from TagMark where entityType='Blog' and relateEntityId = " + articleId;
List<String> alltagName = articleManager.find(hql);
if(alltagName.size() > 0){
handler.dataMap.put(BlogHandler.ALLTAGNAME, alltagName);
}
handler.createDoc(output);
} catch (IOException e) {
e.printStackTrace();
} finally{
output.flush();
output.close();
}
}
}
效果图
问题:这个word只能在有网的情况下才能查看,因为我存的是链接,如果要能在本地随时打开的话,用还需生成.file文件夹(存图片),这样不太方便,毕竟不是一体的,想复制还得把文件夹拷贝走,所以换成mht单一文件模式。下一章贴代码。。。