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

java匯出word功能(包含圖片)二

上一章寫了匯出html其實基本能滿足條件,開寫mht格式下的word

mhtblog.ftl

MIME-Version: 1.0
Content-Type: multipart/related; boundary="----=_NextPart_01CFAA79.4B969C00"

此文件為“單個檔案網頁”,也稱為“Web 檔案”檔案。如果您看到此訊息,但是您的瀏覽器或編輯器不支援“Web 檔案”檔案。請下載支援“Web 檔案”的瀏覽器,如 Microsoft Internet Explorer。

------=_NextPart_01CFAA79.4B969C00
Content-Location: file:///C:/9FD15EF4/file8676.htm
Content-Transfer-Encoding: quoted-printable
Content-Type: text/html; charset="utf-8"

<html xmlns:v=3D"urn:schemas-microsoft-com:vml"
xmlns:o=3D"urn:schemas-microsoft-com:office:office"
xmlns:w=3D"urn:schemas-microsoft-com:office:word"
xmlns=3D"http://www.w3.org/TR/REC-html40">

<head>
<meta http-equiv=3DContent-Type content=3D"text/html; charset=3Dutf-8">
<meta name=3DProgId content=3DWord.Document>
<meta name=3DGenerator content=3D"Microsoft Word 11">
<meta name=3DOriginator content=3D"Microsoft Word 11">
<link rel=3DFile-List href=3D"file8676.files/filelist.xml">
<link rel=3DEdit-Time-Data href=3D"file8676.files/editdata.mso">
<!--[if !mso]>
<style>
v\:* {behavior:url(#default#VML);}
o\:* {behavior:url(#default#VML);}
w\:* {behavior:url(#default#VML);}
.shape {behavior:url(#default#VML);}
</style>
<![endif]-->
<title>${title}</title>
<!--[if gte mso 9]><xml>
 <o:DocumentProperties>
  <o:Author>User</o:Author>
  <o:LastAuthor>User</o:LastAuthor>
  <o:Revision>3</o:Revision>
  <o:TotalTime>1</o:TotalTime>
  <o:Created>2014-07-28T07:16:00Z</o:Created>
  <o:LastSaved>2014-07-28T07:33:00Z</o:LastSaved>
  <o:Pages>1</o:Pages>
  <o:Words>11</o:Words>
  <o:Characters>63</o:Characters>
  <o:Company>&#24494;&#36719;&#20013;&#22269;</o:Company>
  <o:Lines>1</o:Lines>
  <o:Paragraphs>1</o:Paragraphs>
  <o:CharactersWithSpaces>73</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 &#30917;</w:DrawingGridVerticalSpacing>
  <w:DisplayHorizontalDrawingGridEvery>0</w:DisplayHorizontalDrawingGridEve=
ry>
  <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=3D"false" LatentStyleCount=3D"156">
 </w:LatentStyles>
</xml><![endif]-->
<style>
<!--
 /* Font Definitions */
 @font-face
    {font-family:SimSun;
    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:SimSun;
    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:SimSun;
    mso-font-kerning:1.0pt;}
span.GramE
    {mso-style-name:"";
    mso-gram-e:yes;}
 /* 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;}
-->
</style>
<!--[if gte mso 10]>
<style>
 /* Style Definitions */
 table.MsoNormalTable
    {mso-style-name:\666E\901A\8868\683C;
    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-fareast-font-family:"Times New Roman";
    mso-ansi-language:#0400;
    mso-fareast-language:#0400;
    mso-bidi-language:#0400;}
</style>
<![endif]--><!--[if gte mso 9]><xml>
 <o:shapedefaults v:ext=3D"edit" spidmax=3D"2050"/>
</xml><![endif]--><!--[if gte mso 9]><xml>
 <o:shapelayout v:ext=3D"edit">
  <o:idmap v:ext=3D"edit" data=3D"1"/>
 </o:shapelayout></xml><![endif]-->
</head>

<body lang=3DZH-CN style=3D'tab-interval:21.0pt;text-justify-trim:punctuati=
on'>

<div class=3DSection1 style=3D'layout-grid:15.6pt'>

<p class=3DMsoNormal align=3Dcenter style=3D'text-align:center;mso-outline-level:1'>
    <span style=3D'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=3D'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=3DMsoNormal align=3Dcenter style=3D'text-align:center;mso-outline-level:1'>
    <#if category??>
        <b style=3D'mso-bidi-font-weight:normal'>
            <span style=3D'font-family:宋體'>博文屬於:</span>
        </b>
        <span style=3D'font-family:宋體'>${category}</span>
    </#if>
    <#if classifyName??>
        <b style=3D'mso-bidi-font-weight:normal'>
            <span style=3D'font-family:宋體'>博文分類標籤:</span>
        </b>
            <span style=3D'font-family:宋體'>${classifyName}</span>
    </#if>
    <span lang=EN-US style=3D'font-size:12.0pt;mso-bidi-font-size:10.0pt;font-family:宋體'>
        <o:p></o:p>
    </span>
</p>

<p class=3DMsoNormal align=3Dcenter style=3D'text-align:center;mso-outline-level:1'>
    <#if allTagName??>
        <#list allTagName as tagName>
            <b style='mso-bidi-font-weight:normal'>
                <span style='font-family:宋體'>${tagName}</span>
            </b>
        </#list>
    </#if>
    <span lang=EN-US style=3D'font-size:12.0pt;mso-bidi-font-size:10.0pt;font-family:宋體'>
        <o:p></o:p>
    </span>
</p>

<p class=3DMsoNormal align=3Dleft style=3D'text-align:left;mso-outline-level:1'>
    <span style=3D'font-size:12.0pt;font-family:宋體;mso-ascii-font-family:"Times New Roman";mso-hansi-font-family:"Times New Roman"'>
        ${content}
    </span>
    <span lang=EN-US style=3D'font-size:12.0pt;font-family:宋體'>
        <o:p></o:p>
    </span>
</p>


</div>

</body>

</html>

<#list imageList as imageMap>
------=_NextPart_01CFAA79.4B969C00
Content-Location: file:///C:/9FD15EF4/file8676.files/${imageMap["imageName"]}
Content-Transfer-Encoding: base64
Content-Type: image/${imageMap["imageType"]}

${imageMap["imageBase"]}
</#list>

------=_NextPart_01CFAA79.4B969C00
Content-Location: file:///C:/9FD15EF4/file8676.files/filelist.xml
Content-Transfer-Encoding: quoted-printable
Content-Type: text/xml; charset="utf-8"

<xml xmlns:o=3D"urn:schemas-microsoft-com:office:office">
<o:MainFile HRef=3D"../file8676.htm"/>
<#list imageList as imageMap>
<o:File HRef=3D"${imageMap["imageName"]}"/>
</#list>
 <o:File HRef=3D"filelist.xml"/>
</xml>
------=_NextPart_01CFAA79.4B969C00--
這是轉mht的模板,比起轉html的模板來更加複雜,因為在此我們需要將圖片轉為二進位制格式,base64。可以看下由freemark生成的檔案流

BlogHandler.java

public class BlogHandler  extends AppBaseAction{
    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();
        }
    }
    
    //模板
    private String generateTemp(String str) {
        configuration.setClassForTemplateLoading(this.getClass(), "/com/yuqiaotech/pms/util");
        Template t = null;
        String htmlStr = "";
        try {
            t = configuration.getTemplate(str);
            StringWriter stringWriter = new StringWriter();
            BufferedWriter writer = new BufferedWriter(stringWriter);
            t.setEncoding("UTF-8");
            t.process(dataMap, writer);
            htmlStr = stringWriter.toString();
            writer.flush();
            writer.close();
        } catch (TemplateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return htmlStr;     
    }
    
    /*
     * <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;
    }
    
    /*
     * 去除fontfamily預設為宋體字
     *
     */
    public String setFamily(String content){
        String regex = "font-family.*?;";
        content = content.replaceAll(regex, "");
        return content;
    }
    
    /*
     * <img/>
     * <img/> ==>
     * <v:shape style=3D'width:352.5pt;height:318.75pt'>
            <v:imagedata src=3D"file8676.files/image002.jpg" o:title=3D"logo"/>
        </v:shape>
     */
    public String exImageToD(String content) throws FileNotFoundException, IOException {
        String regex = "<img[^>]+src\\s*=\\s*['\"]([^'\"]+)['\"][^>]*>";
        content = content.replaceAll("(style=)","$13D");
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(content);
        String src = "";
        String[] sp;
        File file = null;
        while (matcher.find()){
            src = matcher.group(1);
            sp = src.split("/");
            file = new File(getRequest().getRealPath("/")+src.replace(getRequest().getContextPath(), ""));
            if(isExitsImage(file)) {
                content = content.replaceFirst(regex, appendShap(getImageSize(file),sp[sp.length-1]));
            }
            
        }
        return content;
    }
    
    //根據內容獲取圖片連結
    public List<String> getImageSrc(String content) throws FileNotFoundException, IOException {
        String regex = "<img[^>]+src\\s*=\\s*['\"]([^'\"]+)['\"][^>]*>";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(content);
        String src = "";
        List<String> srcs = new ArrayList<String>();
        while (matcher.find()){
            src = matcher.group(1);
            srcs.add(src);
        }
        return srcs;
    }
    
    //獲得圖片的base64碼
    public String getImageBase(String src) {
        File file = new File(getRequest().getRealPath("/")+src.replace(getRequest().getContextPath(), ""));
        if(!isExitsImage(file)) {
            return "";
        }
        InputStream in = null;
        byte[] data = null;  
        try {
            in = new FileInputStream(file);
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        }
        try {  
            data = new byte[in.available()];  
            in.read(data);  
            in.close();  
        } catch (IOException e) {  
          e.printStackTrace();  
        }
        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(data);
    }
    
    public boolean isExitsImage(File file) {
        return file.exists();
    }

    private String appendShap(BufferedImage image,String str){
        String shape = "<v:shape type=3D\"#_x0000_t75\

" style=3D'width:" + image.getWidth() + ";height:" + image.getHeight() + "'>"
                + "<v:imagedata src=3D\"file8676.files/" + str + " \"o:title=3D" + str + "\"/>"
                + "</v:shape>";
        return shape;
    }
    
    private BufferedImage getImageSize(File image) throws FileNotFoundException, IOException {
        return ImageIO.read(new FileInputStream(image));
    }

這裡面要注意的一個問題是type=3D\"#_x0000_t75\ 這是一定要加上的不然在2003中能看到圖片,到了2007中就看不到了

action

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");// 設定為下載application/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.exImageToD(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);
            }
            List<String> srcs = handler.getImageSrc(article.getContent());
            Map<String, String> imageMap = null;
            List<Map<String, String>> list = new ArrayList<Map<String,String>>();
            String[] sp = null;
            for(String src : srcs){
                sp = src.split("/");
                imageMap = new HashMap<String, String>();
                imageMap.put("imageName", sp[sp.length-1]);
                imageMap.put("imageType", sp[sp.length-1].split("\\.")[1]);
                imageMap.put("imageBase", handler.getImageBase(src));
                list.add(imageMap);
            }
            handler.dataMap.put("imageList", list);
            handler.createDoc(output);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            output.flush();
            output.close();
        }
    }

應該程式碼很明確了,我再簡單說一下思路,首先從資料庫中獲取帶css樣式的content,並且content包含img,然後利用freemark生成預定的模板,將資料填充進去,而mht是攜帶base64格式圖片的,所以這裡必須進行一個轉碼,然後就可以生成word了,思路應該挺簡單的,就是需要對圖片連結進行一個處理就行了。下一章介紹匯出pdf