1. 程式人生 > >java實現匯出word功能(包含圖片)一

java實現匯出word功能(包含圖片)一

一、目的

        這個功能已經完成很久了,一直沒有想起來去整理成文,今天就把它整理出來,供大家參考和批評。這個匯出功能也研究了好幾天,同時也看了很多人寫的部落格,真的是千篇一律,好多都不是我想要的功能,為什麼要做匯出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單一檔案模式。下一章貼程式碼。。。