freemarker導出word文檔,word文檔中的xml解析

段落字體:

<w:t>:字符串,文字內容,

如: <w:t>這是文字</w:t>

如: <w:t xml:space="preserve">       </w:t>   <#-- xml:space="preserve"從字面上理解是保持空格 -->

<w:r>:表示一個樣式串,指明它包括的文本的顯示樣式,如文本屬性加粗、下劃線、傾斜的分割,內含w:rsidRPr屬性

<w:rpr>:是<w:r>標籤內的標籤,對文本屬性進行修飾,與<w:ppr>類似

<w:p>:表示段落和html中的<p>標籤類似

如:<#-- 下面是一段粗體 -->

<w:r>

<w:rPr> 

<w:b w:val="on"/>

</w:rPr> 

<w:t> 這是粗體</w:t>

</w:r>

看完這段代碼,應該大概懂點結構了,其中<w:b w:val=”on”> 表示該格式串種的文本爲粗體,而<w:p>一般是<w:r>的外層。

再舉一例:

<w:p>

<w:pPr>

<w:rPr>

<w:jc w:val="center"/>                 <#-- 這句話表示段落對齊方式 -->

<w:spacing  w:line="600" w:lineRule="auto"/>                         <#-- 設置行距,要進行運算,要用數字除以240,如此處爲600/240=2.5倍行距 -->

</w:rPr>

</w:pPr>

<w:r w:rsidRPr="00F2468A">

<w:rPr>

<w:b w:val="on"/>                       <#-- 粗體 -->

<w:sz w:val="40"/><w:szCs w:val="40"/>                   <#-- 字體尺寸爲是40除2等於20 -->

<w:rFronts w:ascii="宋體" w:hAnsi="宋體" w:hint="esatAsia"/>

</w:rPr>

<w:t>${wordtext1}</w:t>                         <#-- ${wordtext1}是freemarker變量,一般是字符串,用於輸出,這個可以參考freemarker文檔 -->

</w:r>

</w:p>

段屬性包含在<w:pPr></w:pPr>中,文本格式包含在<w:rPr></w:rPr>中,這兒的Pr是property的意思

   頁面設置:

如:

<#-- 設置了頁的寬,高,和頁的各邊距。各項的值均是英寸乘1440得出 -->

<w:body> <w:sectPr>
<w:pgSz w:w="12240" w:h="15840"/>
<w:pgMar w:top="1440" w:right="1800" w:bottom="1440" w:left="1800" w:header="720" w:footer="720" w:gutter="0"/>
</w:sectPr>

</w:body>

再如:

<#--此處設置頁眉頁腳-->

<w:sectPr wsp:rsidR="002C452C">

<w:hdr w:type="odd" >

<w:p>

<w:pPr>

<w:pStyle w:val="Header"/>

</w:pPr>

<w:r>

<w:t>這是頁眉</w:t>

</w:r>

</w:p>

</w:hdr>

<w:ftr w:type="odd">

<w:p>

<w:pPr>

<w:pStyle w:val="Footer"/>

</w:pPr>

<w:r>

<w:t>這是頁腳</w:t>

</w:r>

</w:p>

</w:ftr>

</w:sectPr>

再再如:

</w:body>

<#-- 文檔的視圖 -->

<w:docPr>

<w:view w:val="print"/><w:zoom w:percent="100"/>

</w:docPr>

</w:wordDocument>

docPr,就是document property的意思,表示文檔的視圖是“print”,視圖比例100%

   表格:

直接看例子吧:

<w:tbl>

<w:tblPr><w:tblW w:w="0" w:type="auto"/>

<w:tblInd w:w="737" w:type="dxa"/>

<w:tblBorders>

<w:top w:val="single" w:sz="4" w:space="0" w:color="auto"/><w:left w:val="single" w:sz="4" w:space="0" w:color="auto"/>

<w:bottom w:val="single" w:sz="4" w:space="0" w:color="auto"/>

<w:right w:val="single" w:sz="4" w:space="0" w:color="auto"/>

<w:insideH w:val="single" w:sz="4" w:space="0" w:color="auto"/>

<w:insideV w:val="single" w:sz="4" w:space="0" w:color="auto"/>

</w:tblBorders>

<w:tblLayout w:type="fixed"/><w:tblLook w:val="04A0"/>

</w:tblPr>

<w:tblGrid><w:gridCol w:w="2635"/><w:gridCol w:w="3372"/><w:gridCol w:w="2074"/></w:tblGrid>

<w:tr w:rsidR="00F2468A" w:rsidRPr="00F2468A" w:rsidTr="00D074CF">

<w:trPr><w:trHeight w:val="566"/></w:trPr>

<w:tc><w:tcPr><w:tcW w:w="2000" w:type="dxa"/></w:tcPr>

<w:p w:rsidR="00F2468A" w:rsidRPr="00F2468A" w:rsidRDefault="00F2468A" w:rsidP="00E9655B">

<w:pPr><w:jc w:val="center"/><w:rPr><w:rFonts w:ascii="宋體" w:hAnsi="宋體"/><w:szCs w:val="21"/></w:rPr></w:pPr>

<w:r w:rsidRPr="00F2468A"><w:rPr><w:rFonts w:ascii="宋體" w:hAnsi="宋體" w:hint="eastAsia"/><w:szCs w:val="21"/></w:rPr>

<w:t>第一列</w:t></w:r>

</w:p></w:tc>

<w:tc><w:tcPr><w:tcW w:w="1200" w:type="dxa"/></w:tcPr>

<w:p w:rsidR="00F2468A" w:rsidRPr="00F2468A" w:rsidRDefault="00F2468A" w:rsidP="00E9655B">

<w:pPr><w:jc w:val="center"/><w:rPr><w:rFonts w:ascii="宋體" w:hAnsi="宋體"/><w:szCs w:val="21"/></w:rPr></w:pPr>

<w:r w:rsidRPr="00F2468A"><w:rPr><w:rFonts w:ascii="宋體" w:hAnsi="宋體" w:hint="eastAsia"/><w:szCs w:val="21"/></w:rPr>

<w:t>第二列</w:t></w:r>

</w:p></w:tc>

<w:tc><w:tcPr><w:tcW w:w="3200" w:type="dxa"/></w:tcPr>

<w:p w:rsidR="00F2468A" w:rsidRPr="00F2468A" w:rsidRDefault="00F2468A" w:rsidP="00E9655B">

<w:pPr><w:jc w:val="center"/><w:rPr><w:rFonts w:ascii="宋體" w:hAnsi="宋體"/><w:szCs w:val="21"/></w:rPr></w:pPr>

<w:r w:rsidRPr="00F2468A"><w:rPr><w:rFonts w:ascii="宋體" w:hAnsi="宋體" w:hint="eastAsia"/><w:szCs w:val="21"/></w:rPr>

<w:t>第三列</w:t></w:r>

</w:p></w:tc>

</w:tr>

<#list table3 as item3>

<w:tr w:rsidR="00F2468A" w:rsidRPr="00F2468A" w:rsidTr="00D074CF">

<w:trPr><w:trHeight w:val="566"/></w:trPr>

<w:tc><w:tcPr><w:tcW w:w="2000" w:type="dxa"/></w:tcPr>

<w:p w:rsidR="00F2468A" w:rsidRPr="00F2468A" w:rsidRDefault="00F2468A" w:rsidP="00E9655B">

<w:pPr><w:jc w:val="center"/><w:rPr><w:rFonts w:ascii="宋體" w:hAnsi="宋體"/><w:szCs w:val="21"/></w:rPr></w:pPr>

<w:r w:rsidRPr="00F2468A"><w:rPr><w:rFonts w:ascii="宋體" w:hAnsi="宋體" w:hint="eastAsia"/><w:szCs w:val="21"/></w:rPr>

<w:t>${item3.tablecolumn1}</w:t></w:r>

</w:p></w:tc>

<w:tc><w:tcPr><w:tcW w:w="1200" w:type="dxa"/></w:tcPr>

<w:p w:rsidR="00F2468A" w:rsidRPr="00F2468A" w:rsidRDefault="00F2468A" w:rsidP="00E9655B">

<w:pPr><w:jc w:val="center"/><w:rPr><w:rFonts w:ascii="宋體" w:hAnsi="宋體"/><w:szCs w:val="21"/></w:rPr></w:pPr>

<w:r w:rsidRPr="00F2468A"><w:rPr><w:rFonts w:ascii="宋體" w:hAnsi="宋體" w:hint="eastAsia"/><w:szCs w:val="21"/></w:rPr>

<w:t>${item3.tablecolumn2}</w:t></w:r>

</w:p></w:tc>

<w:tc><w:tcPr><w:tcW w:w="3200" w:type="dxa"/></w:tcPr>

<w:p w:rsidR="00F2468A" w:rsidRPr="00F2468A" w:rsidRDefault="00F2468A" w:rsidP="00E9655B">

<w:pPr><w:jc w:val="center"/><w:rPr><w:rFonts w:ascii="宋體" w:hAnsi="宋體"/><w:szCs w:val="21"/></w:rPr></w:pPr>

<w:r w:rsidRPr="00F2468A"><w:rPr><w:rFonts w:ascii="宋體" w:hAnsi="宋體" w:hint="eastAsia"/><w:szCs w:val="21"/></w:rPr>

<w:t>${item3.tablecolumn3}</w:t></w:r>

</w:p></w:tc>

</w:tr>

</#list>

</w:tbl>

最後,寫一個 完整的xml例子 吧:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<?mso-application progid="Word.Document"?>

<w:wordDocument xmlns:aml="http://schemas.microsoft.com/aml/2001/core"

xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882"

xmlns:o="urn:schemas-microsoft-com:office:office"

xmlns:v="urn:schemas-microsoft-com:vml"

xmlns:w10="urn:schemas-microsoft-com:office:word"

xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml"

xmlns:wx="http://schemas.microsoft.com/office/word/2003/auxHint"

xmlns:wsp="http://schemas.microsoft.com/office/word/2003/wordml/sp2"

xmlns:sl="http://schemas.microsoft.com/schemaLibrary/2003/core"

w:macrosPresent="no" w:embeddedObjPresent="no" w:ocxPresent="no"

xml:space="preserve">

<w:body>

<w:p>

<w:pPr>

<w:jc w:val="left"/>

<w:spacing  w:line="240" w:lineRule="auto"/>

</w:pPr>

<w:r>

<w:rPr> 

<w:sz w:val="24"/><w:szCs w:val="24"/>

<w:rFonts   w:ascii="Arial" w:eastAsia="Arial" w:hAnsi="Arial" />

</w:rPr> 

<w:t>Niu don't like Red or Blue! It seems that </w:t>

</w:r>

<w:r>

<w:rPr> 

<w:sz w:val="48"/><w:szCs w:val="48"/>

<w:rFonts   w:ascii="Arial" w:eastAsia="Arial" w:hAnsi="Arial" />

</w:rPr> 

<w:t>Hello world!</w:t>

</w:r>

</w:p>

<w:p>

<w:sectPr wsp:rsidR="002C452C">

<w:pgSz w:w="12240" w:h="15840"/>

<w:pgMar w:top="1526.4" w:right="3254.4" w:bottom="2966.4" w:left="1670.4" w:header="720" w:footer="720" w:gutter="0"/>

<w:hdr w:type="odd" >

<w:p>

<w:pPr>

<w:pStyle w:val="Header"/>

</w:pPr>

<w:r>

<w:t>Header</w:t>

</w:r>

</w:p>

</w:hdr>

<w:ftr w:type="odd">

<w:p>

<w:pPr>

<w:pStyle w:val="Footer"/>

</w:pPr>

<w:r>

<w:t>Footer</w:t>

</w:r>

</w:p>

</w:ftr>

</w:sectPr>

</w:body>

<w:docPr>

<w:view w:val="print"/><w:zoom w:percent="100"/>

</w:docPr>

</w:wordDocument>

一個應用級別的Word文檔絕對不僅這些內容,更詳細的內容需要參考MS Office SDK。

順便提一句,freemarker導出文檔用的是freemarker.template.Template這個類,具體可以參考相關文檔。。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章