1. 程式人生 > >使用freemarker匯出複雜的excel表格、word文件

使用freemarker匯出複雜的excel表格、word文件

        最近因為專案需要,需要匯出複雜的excel表格。由於需要合併單元格以及樣式處理,用poi匯出顯得比較困難。因為之前用freemarker匯出過pdf/word檔案,就想著能不能用其匯出excel。經過一番實驗,大功告成,真是比poi好用N多倍呀!廢話少說,直接進入正題。

        excel表格用編輯器開啟後,可以發現它也是一個可編輯的標記性的格式,如同html頁面裡面的標籤首尾相連。那就好辦了。以下是具體步驟,部分邏輯不贅述直接上程式碼了。有邏輯或程式碼簡陋望大神不要嘲笑並不吝指教。

        一、首先根據我們最終需要匯出的excel表格的格式,用假資料填充後生成一個excel模板。如下圖(假資料填充,作為模板):


       模板生成後,將當前模板另存,選擇另存為XML格式 ,如下圖:


另存後的檔案開啟後如下圖:

分析該檔案,我們會發現如同poi匯出一樣,該檔案已經自動包含了需要的樣式Styles、Worksheet、Table、以及一行行的資料Row。我們只需要關注Table裡面的東西就好,分析資料後根據自己的具體業務邏輯,將之前渲染的假資料通過freemarker的標籤替換成動態的生成形式。至此,前期準備完畢。如圖:


二、前後臺的最終實現(其實整個過程中最複雜部分就在將轉成xml的檔案中的資料替換成動態的)

前臺部分:

頁面點選匯出按鈕後觸發事件,fileFrame是頁面iframe隱藏域的id:


後臺部分:

直接上程式碼,很少幾行而已。


至此,通過freemarker匯出複雜excel表格完成。相較於poi,本人更喜歡這種方式。由於擔心一上來整的資料樣子看起來太複雜大家看不明白,因此在末尾又碼了一個簡單模型,希望對大家有幫助。哈哈!


補充:有朋友問我freemarker匯出word,在這裡也簡單說一下,跟excel匯出基本流程是一樣的。

首先就是模板製作:將下圖word模板另存為xml格式

將轉成的xml的w:tr (其實就是行)全部用freemarker動態標籤渲染上


動態渲染後的xml程式碼(比較長):

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<?mso-application progid="Word.Document"?>
<w:wordDocument xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml"
xmlns:v="urn:schemas-microsoft-com:vml" xmlns:w10="urn:schemas-microsoft-com:office:word"
xmlns:sl="http://schemas.microsoft.com/schemaLibrary/2003/core"
xmlns:aml="http://schemas.microsoft.com/aml/2001/core" xmlns:wx="http://schemas.microsoft.com/office/word/2003/auxHint"
xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882"
w:macrosPresent="no" w:embeddedObjPresent="no" w:ocxPresent="no"
xml:space="preserve"><o:DocumentProperties><o:Title>出國、赴港澳任務批件</o:Title><o:Author>飛總保衛室文書</o:Author><o:LastAuthor>Administrator</o:LastAuthor><o:Revision>6</o:Revision><o:Created>2016-10-11T07:11:00Z</o:Created><o:LastSaved>2018-07-03T07:56:02Z</o:LastSaved><o:TotalTime>4320</o:TotalTime><o:Pages>4</o:Pages><o:Words>408</o:Words><o:Characters>2327</o:Characters><o:Company>center</o:Company><o:Lines>19</o:Lines><o:Paragraphs>5</o:Paragraphs><o:CharactersWithSpaces>2730</o:CharactersWithSpaces></o:DocumentProperties><o:CustomDocumentProperties><o:KSOProductBuildVer
dt:dt="string">2052-10.1.0.7400</o:KSOProductBuildVer></o:CustomDocumentProperties><w:fonts><w:defaultFonts
w:ascii="Times New Roman" w:fareast="宋體" w:h-ansi="Times New Roman"
w:cs="Times New Roman" /><w:font w:name="Times New Roman"><w:panose-1
w:val="02020603050405020304" /><w:charset w:val="00" /><w:family
w:val="Auto" /><w:pitch w:val="Default" /><w:sig w:usb-0="E0002EFF"
w:usb-1="C0007843" w:usb-2="00000009" w:usb-3="00000000" w:csb-0="400001FF"
w:csb-1="FFFF0000" /></w:font><w:font w:name="宋體"><w:panose-1
w:val="02010600030101010101" /><w:charset w:val="86" /><w:family
w:val="Auto" /><w:pitch w:val="Default" /><w:sig w:usb-0="00000003"
w:usb-1="288F0000" w:usb-2="00000006" w:usb-3="00000000" w:csb-0="00040001"
w:csb-1="00000000" /></w:font><w:font w:name="Wingdings"><w:panose-1
w:val="05000000000000000000" /><w:charset w:val="02" /><w:family
w:val="Auto" /><w:pitch w:val="Default" /><w:sig w:usb-0="00000000"
w:usb-1="00000000" w:usb-2="00000000" w:usb-3="00000000" w:csb-0="80000000"
w:csb-1="00000000" /></w:font><w:font w:name="Arial"><w:panose-1
w:val="020B0604020202020204" /><w:charset w:val="01" /><w:family
w:val="SWiss" /><w:pitch w:val="Default" /><w:sig
w:usb-0="E0002AFF" w:usb-1="C0007843" w:usb-2="00000009" w:usb-3="00000000"
w:csb-0="400001FF" w:csb-1="FFFF0000" /></w:font><w:font w:name="黑體"><w:panose-1
w:val="02010609060101010101" /><w:charset w:val="86" /><w:family
w:val="Auto" /><w:pitch w:val="Default" /><w:sig w:usb-0="800002BF"
w:usb-1="38CF7CFA" w:usb-2="00000016" w:usb-3="00000000" w:csb-0="00040001"
w:csb-1="00000000" /></w:font><w:font w:name="Courier New"><w:panose-1
w:val="02070309020205020404" /><w:charset w:val="01" /><w:family
w:val="Modern" /><w:pitch w:val="Default" /><w:sig
w:usb-0="E0002AFF" w:usb-1="C0007843" w:usb-2="00000009" w:usb-3="00000000"
w:csb-0="400001FF" w:csb-1="FFFF0000" /></w:font><w:font w:name="Symbol"><w:panose-1
w:val="05050102010706020507" /><w:charset w:val="02" /><w:family
w:val="Roman" /><w:pitch w:val="Default" /><w:sig
w:usb-0="00000000" w:usb-1="00000000" w:usb-2="00000000" w:usb-3="00000000"
w:csb-0="80000000" w:csb-1="00000000" /></w:font><w:font w:name="Calibri"><w:panose-1
w:val="020F0502020204030204" /><w:charset w:val="00" /><w:family
w:val="SWiss" /><w:pitch w:val="Default" /><w:sig
w:usb-0="E00002FF" w:usb-1="4000ACFF" w:usb-2="00000001" w:usb-3="00000000"
w:csb-0="2000019F" w:csb-1="00000000" /></w:font></w:fonts><w:styles><w:latentStyles
w:defLockedState="off" w:latentStyleCount="260"><w:lsdException
w:name="Normal" /><w:lsdException w:name="heading 1" /><w:lsdException
w:name="heading 2" /><w:lsdException w:name="heading 3" /><w:lsdException
w:name="heading 4" /><w:lsdException w:name="heading 5" /><w:lsdException
w:name="heading 6" /><w:lsdException w:name="heading 7" /><w:lsdException
w:name="heading 8" /><w:lsdException w:name="heading 9" /><w:lsdException
w:name="index 1" /><w:lsdException w:name="index 2" /><w:lsdException
w:name="index 3" /><w:lsdException w:name="index 4" /><w:lsdException
w:name="index 5" /><w:lsdException w:name="index 6" /><w:lsdException
w:name="index 7" /><w:lsdException w:name="index 8" /><w:lsdException
w:name="index 9" /><w:lsdException w:name="toc 1" /><w:lsdException
w:name="toc 2" /><w:lsdException w:name="toc 3" /><w:lsdException
w:name="toc 4" /><w:lsdException w:name="toc 5" /><w:lsdException
w:name="toc 6" /><w:lsdException w:name="toc 7" /><w:lsdException
w:name="toc 8" /><w:lsdException w:name="toc 9" /><w:lsdException
w:name="Normal Indent" /><w:lsdException w:name="footnote text" /><w:lsdException
w:name="annotation text" /><w:lsdException w:name="header" /><w:lsdException
w:name="footer" /><w:lsdException w:name="index heading" /><w:lsdException
w:name="caption" /><w:lsdException w:name="table of figures" /><w:lsdException
w:name="envelope address" /><w:lsdException w:name="envelope return" /><w:lsdException
w:name="footnote reference" /><w:lsdException w:name="annotation reference" /><w:lsdException
w:name="line number" /><w:lsdException w:name="page number" /><w:lsdException
w:name="endnote reference" /><w:lsdException w:name="endnote text" /><w:lsdException
w:name="table of authorities" /><w:lsdException w:name="macro" /><w:lsdException
w:name="toa heading" /><w:lsdException w:name="List" /><w:lsdException
w:name="List Bullet" /><w:lsdException w:name="List Number" /><w:lsdException
w:name="List 2" /><w:lsdException w:name="List 3" /><w:lsdException
w:name="List 4" /><w:lsdException w:name="List 5" /><w:lsdException
w:name="List Bullet 2" /><w:lsdException w:name="List Bullet 3" /><w:lsdException
w:name="List Bullet 4" /><w:lsdException w:name="List Bullet 5" /><w:lsdException
w:name="List Number 2" /><w:lsdException w:name="List Number 3" /><w:lsdException
w:name="List Number 4" /><w:lsdException w:name="List Number 5" /><w:lsdException
w:name="Title" /><w:lsdException w:name="Closing" /><w:lsdException
w:name="Signature" /><w:lsdException w:name="Default Paragraph Font" /><w:lsdException
w:name="Body Text" /><w:lsdException w:name="Body Text Indent" /><w:lsdException
w:name="List Continue" /><w:lsdException w:name="List Continue 2" /><w:lsdException
w:name="List Continue 3" /><w:lsdException w:name="List Continue 4" /><w:lsdException
w:name="List Continue 5" /><w:lsdException w:name="Message Header" /><w:lsdException
w:name="Subtitle" /><w:lsdException w:name="Salutation" /><w:lsdException
w:name="Date" /><w:lsdException w:name="Body Text First Indent" /><w:lsdException
w:name="Body Text First Indent 2" /><w:lsdException
w:name="Note Heading" /><w:lsdException w:name="Body Text 2" /><w:lsdException
w:name="Body Text 3" /><w:lsdException w:name="Body Text Indent 2" /><w:lsdException
w:name="Body Text Indent 3" /><w:lsdException w:name="Block Text" /><w:lsdException
w:name="Hyperlink" /><w:lsdException w:name="FollowedHyperlink" /><w:lsdException
w:name="Strong" /><w:lsdException w:name="Emphasis" /><w:lsdException
w:name="Document Map" /><w:lsdException w:name="Plain Text" /><w:lsdException
w:name="E-mail Signature" /><w:lsdException w:name="Normal (Web)" /><w:lsdException
w:name="HTML Acronym" /><w:lsdException w:name="HTML Address" /><w:lsdException
w:name="HTML Cite" /><w:lsdException w:name="HTML Code" /><w:lsdException
w:name="HTML Definition" /><w:lsdException w:name="HTML Keyboard" /><w:lsdException
w:name="HTML Preformatted" /><w:lsdException w:name="HTML Sample" /><w:lsdException
w:name="HTML Typewriter" /><w:lsdException w:name="HTML Variable" /><w:lsdException
w:name="Normal Table" /><w:lsdException w:name="annotation subject" /><w:lsdException
w:name="Table Simple 1" /><w:lsdException w:name="Table Simple 2" /><w:lsdException
w:name="Table Simple 3" /><w:lsdException w:name="Table Classic 1" /><w:lsdException
w:name="Table Classic 2" /><w:lsdException w:name="Table Classic 3" /><w:lsdException
w:name="Table Classic 4" /><w:lsdException w:name="Table Colorful 1" /><w:lsdException
w:name="Table Colorful 2" /><w:lsdException w:name="Table Colorful 3" /><w:lsdException
w:name="Table Columns 1" /><w:lsdException w:name="Table Columns 2" /><w:lsdException
w:name="Table Columns 3" /><w:lsdException w:name="Table Columns 4" /><w:lsdException
w:name="Table Columns 5" /><w:lsdException w:name="Table Grid 1" /><w:lsdException
w:name="Table Grid 2" /><w:lsdException w:name="Table Grid 3" /><w:lsdException
w:name="Table Grid 4" /><w:lsdException w:name="Table Grid 5" /><w:lsdException
w:name="Table Grid 6" /><w:lsdException w:name="Table Grid 7" /><w:lsdException
w:name="Table Grid 8" /><w:lsdException w:name="Table List 1" /><w:lsdException
w:name="Table List 2" /><w:lsdException w:name="Table List 3" /><w:lsdException
w:name="Table List 4" /><w:lsdException w:name="Table List 5" /><w:lsdException
w:name="Table List 6" /><w:lsdException w:name="Table List 7" /><w:lsdException
w:name="Table List 8" /><w:lsdException w:name="Table 3D effects 1" /><w:lsdException
w:name="Table 3D effects 2" /><w:lsdException w:name="Table 3D effects 3" /><w:lsdException
w:name="Table Contemporary" /><w:lsdException w:name="Table Elegant" /><w:lsdException
w:name="Table Professional" /><w:lsdException w:name="Table Subtle 1" /><w:lsdException
w:name="Table Subtle 2" /><w:lsdException w:name="Table Web 1" /><w:lsdException
w:name="Table Web 2" /><w:lsdException w:name="Table Web 3" /><w:lsdException
w:name="Balloon Text" /><w:lsdException w:name="Table Grid" /><w:lsdException
w:name="Table Theme" /><w:lsdException w:name="Light Shading" /><w:lsdException
w:name="Light List" /><w:lsdException w:name="Light Grid" /><w:lsdException
w:name="Medium Shading 1" /><w:lsdException w:name="Medium Shading 2" /><w:lsdException
w:name="Medium List 1" /><w:lsdException w:name="Medium List 2" /><w:lsdException
w:name="Medium Grid 1" /><w:lsdException w:name="Medium Grid 2" /><w:lsdException
w:name="Medium Grid 3" /><w:lsdException w:name="Dark List" /><w:lsdException
w:name="Colorful Shading" /><w:lsdException w:name="Colorful List" /><w:lsdException
w:name="Colorful Grid" /><w:lsdException w:name="Light Shading Accent 1" /><w:lsdException
w:name="Light List Accent 1" /><w:lsdException w:name="Light Grid Accent 1" /><w:lsdException
w:name="Medium Shading 1 Accent 1" /><w:lsdException
w:name="Medium Shading 2 Accent 1" /><w:lsdException
w:name="Medium List 1 Accent 1" /><w:lsdException
w:name="Medium List 2 Accent 1" /><w:lsdException
w:name="Medium Grid 1 Accent 1" /><w:lsdException
w:name="Medium Grid 2 Accent 1" /><w:lsdException
w:name="Medium Grid 3 Accent 1" /><w:lsdException
w:name="Dark List Accent 1" /><w:lsdException
w:name="Colorful Shading Accent 1" /><w:lsdException
w:name="Colorful List Accent 1" /><w:lsdException
w:name="Colorful Grid Accent 1" /><w:lsdException
w:name="Light Shading Accent 2" /><w:lsdException
w:name="Light List Accent 2" /><w:lsdException w:name="Light Grid Accent 2" /><w:lsdException
w:name="Medium Shading 1 Accent 2" /><w:lsdException
w:name="Medium Shading 2 Accent 2" /><w:lsdException
w:name="Medium List 1 Accent 2" /><w:lsdException
w:name="Medium List 2 Accent 2" /><w:lsdException
w:name="Medium Grid 1 Accent 2" /><w:lsdException
w:name="Medium Grid 2 Accent 2" /><w:lsdException
w:name="Medium Grid 3 Accent 2" /><w:lsdException
w:name="Dark List Accent 2" /><w:lsdException
w:name="Colorful Shading Accent 2" /><w:lsdException
w:name="Colorful List Accent 2" /><w:lsdException
w:name="Colorful Grid Accent 2" /><w:lsdException
w:name="Light Shading Accent 3" /><w:lsdException
w:name="Light List Accent 3" /><w:lsdException w:name="Light Grid Accent 3" /><w:lsdException
w:name="Medium Shading 1 Accent 3" /><w:lsdException
w:name="Medium Shading 2 Accent 3" /><w:lsdException
w:name="Medium List 1 Accent 3" /><w:lsdException
w:name="Medium List 2 Accent 3" /><w:lsdException
w:name="Medium Grid 1 Accent 3" /><w:lsdException
w:name="Medium Grid 2 Accent 3" /><w:lsdException
w:name="Medium Grid 3 Accent 3" /><w:lsdException
w:name="Dark List Accent 3" /><w:lsdException
w:name="Colorful Shading Accent 3" /><w:lsdException
w:name="Colorful List Accent 3" /><w:lsdException
w:name="Colorful Grid Accent 3" /><w:lsdException
w:name="Light Shading Accent 4" /><w:lsdException
w:name="Light List Accent 4" /><w:lsdException w:name="Light Grid Accent 4" /><w:lsdException
w:name="Medium Shading 1 Accent 4" /><w:lsdException
w:name="Medium Shading 2 Accent 4" /><w:lsdException
w:name="Medium List 1 Accent 4" /><w:lsdException
w:name="Medium List 2 Accent 4" /><w:lsdException
w:name="Medium Grid 1 Accent 4" /><w:lsdException
w:name="Medium Grid 2 Accent 4" /><w:lsdException
w:name="Medium Grid 3 Accent 4" /><w:lsdException
w:name="Dark List Accent 4" /><w:lsdException
w:name="Colorful Shading Accent 4" /><w:lsdException
w:name="Colorful List Accent 4" /><w:lsdException
w:name="Colorful Grid Accent 4" /><w:lsdException
w:name="Light Shading Accent 5" /><w:lsdException
w:name="Light List Accent 5" /><w:lsdException w:name="Light Grid Accent 5" /><w:lsdException
w:name="Medium Shading 1 Accent 5" /><w:lsdException
w:name="Medium Shading 2 Accent 5" /><w:lsdException
w:name="Medium List 1 Accent 5" /><w:lsdException
w:name="Medium List 2 Accent 5" /><w:lsdException
w:name="Medium Grid 1 Accent 5" /><w:lsdException
w:name="Medium Grid 2 Accent 5" /><w:lsdException
w:name="Medium Grid 3 Accent 5" /><w:lsdException
w:name="Dark List Accent 5" /><w:lsdException
w:name="Colorful Shading Accent 5" /><w:lsdException
w:name="Colorful List Accent 5" /><w:lsdException
w:name="Colorful Grid Accent 5" /><w:lsdException
w:name="Light Shading Accent 6" /><w:lsdException
w:name="Light List Accent 6" /><w:lsdException w:name="Light Grid Accent 6" /><w:lsdException
w:name="Medium Shading 1 Accent 6" /><w:lsdException
w:name="Medium Shading 2 Accent 6" /><w:lsdException
w:name="Medium List 1 Accent 6" /><w:lsdException
w:name="Medium List 2 Accent 6" /><w:lsdException
w:name="Medium Grid 1 Accent 6" /><w:lsdException
w:name="Medium Grid 2 Accent 6" /><w:lsdException
w:name="Medium Grid 3 Accent 6" /><w:lsdException
w:name="Dark List Accent 6" /><w:lsdException
w:name="Colorful Shading Accent 6" /><w:lsdException
w:name="Colorful List Accent 6" /><w:lsdException
w:name="Colorful Grid Accent 6" /></w:latentStyles><w:style
w:type="paragraph" w:styleId="a1" w:default="on"><w:name w:val="Normal" /><w:pPr><w:widowControl
w:val="off" /><w:jc w:val="both" /></w:pPr><w:rPr><w:kern
w:val="2" /><w:sz w:val="21" /><w:lang w:val="EN-US"
w:fareast="ZH-CN" /></w:rPr></w:style><w:style w:type="character"
w:styleId="a6" w:default="on"><w:name w:val="Default Paragraph Font" /><w:semiHidden /></w:style><w:style
w:type="table" w:styleId="a8" w:default="on"><w:name w:val="Normal Table" /><w:semiHidden /><w:tblPr><w:tblCellMar><w:top
w:w="0" w:type="dxa" /><w:left w:w="108" w:type="dxa" /><w:bottom
w:w="0" w:type="dxa" /><w:right w:w="108" w:type="dxa" /></w:tblCellMar></w:tblPr></w:style><w:style
w:type="paragraph" w:styleId="a2"><w:name w:val="annotation subject" /><w:basedOn
w:val="a3" /><w:next w:val="a3" /><w:semiHidden /><w:pPr><w:pStyle
w:val="a3" /></w:pPr><w:rPr><w:b /><w:b-cs /></w:rPr></w:style><w:style
w:type="paragraph" w:styleId="a3"><w:name w:val="annotation text" /><w:basedOn
w:val="a1" /><w:semiHidden /><w:pPr><w:jc w:val="left" /></w:pPr></w:style><w:style
w:type="paragraph" w:styleId="a4"><w:name w:val="footer" /><w:basedOn
w:val="a1" /><w:link w:val="a9" /><w:pPr><w:tabs><w:tab
w:val="center" w:pos="4153" /><w:tab w:val="right" w:pos="8306" /></w:tabs><w:snapToGrid
w:val="off" /><w:jc w:val="left" /></w:pPr><w:rPr><w:sz
w:val="18" /><w:sz-cs w:val="18" /></w:rPr></w:style><w:style
w:type="paragraph" w:styleId="a5"><w:name w:val="header" /><w:basedOn
w:val="a1" /><w:link w:val="a10" /><w:pPr><w:pBdr><w:bottom
w:val="single" w:sz="6" wx:bdrwidth="15" w:space="1" w:color="auto" /></w:pBdr><w:tabs><w:tab
w:val="center" w:pos="4153" /><w:tab w:val="right" w:pos="8306" /></w:tabs><w:snapToGrid
w:val="off" /><w:jc w:val="center" /></w:pPr><w:rPr><w:sz
w:val="18" /><w:sz-cs w:val="18" /></w:rPr></w:style><w:style
w:type="character" w:styleId="a7"><w:name w:val="annotation reference" /><w:basedOn
w:val="a6" /><w:semiHidden /><w:rPr><w:sz w:val="21" /></w:rPr></w:style><w:style
w:type="character" w:styleId="a9"><w:name w:val="頁尾 Char" /><w:basedOn
w:val="a6" /><w:link w:val="a4" /><w:rPr><w:kern w:val="2" /><w:sz
w:val="18" /><w:sz-cs w:val="18" /></w:rPr></w:style><w:style
w:type="character" w:styleId="a10"><w:name w:val="頁首 Char" /><w:basedOn
w:val="a6" /><w:link w:val="a5" /><w:rPr><w:kern w:val="2" /><w:sz
w:val="18" /><w:sz-cs w:val="18" /></w:rPr></w:style></w:styles><w:bgPict><w:background /><v:background
id="_x0000_s1025"><v:fill on="f" focussize="0,0" /></v:background></w:bgPict><w:docPr><w:view
w:val="print" /><w:zoom w:percent="100" /><w:characterSpacingControl
w:val="CompressPunctuation" /><w:documentProtection
w:enforcement="off" /><w:punctuationKerning /><w:doNotEmbedSystemFonts /><w:bordersDontSurroundHeader /><w:bordersDontSurroundFooter /><w:defaultTabStop
w:val="425" /><w:drawingGridHorizontalSpacing
w:val="105" /><w:drawingGridVerticalSpacing
w:val="156" /><w:displayHorizontalDrawingGridEvery
w:val="0" /><w:displayVerticalDrawingGridEvery
w:val="2" /><w:compat><w:ulTrailSpace /><w:doNotUseHTMLParagraphAutoSpacing /><w:doNotExpandShiftReturn /><w:balanceSingleByteDoubleByteWidth /><w:spaceForUL /><w:alignTablesRowByRow /><w:useWord97LineBreakingRules /><w:shapeLayoutLikeWW8 /><w:useFELayout /></w:compat></w:docPr><w:body><wx:sect><w:p><w:pPr><w:jc
w:val="center" /><w:rPr><w:rFonts w:ascii="宋體" w:hint="fareast" /><w:b /><w:b-cs /><w:sz
w:val="32" /></w:rPr></w:pPr><w:r><w:rPr><w:rFonts w:hint="fareast" /><w:b /><w:b-cs /><w:sz
w:val="32" /></w:rPr><w:t>出 國 人 員 名 單</w:t></w:r></w:p><w:p><w:pPr><w:rPr><w:rFonts
w:ascii="宋體" w:hint="default" /><w:b /><w:b-cs /><w:sz w:val="24" /></w:rPr></w:pPr><w:r><w:rPr><w:rFonts
w:ascii="宋體" w:hint="fareast" /><w:sz w:val="28" /></w:rPr><w:t>                                                                         </w:t></w:r><w:r><w:rPr><w:rFonts
w:ascii="宋體" w:hint="fareast" /><w:b /><w:b-cs /><w:sz w:val="24" /></w:rPr><w:t>本單位人數:${total!''}</w:t></w:r></w:p><w:tbl><w:tblPr><w:tblW
w:w="12915" w:type="dxa" /><w:tblInd w:w="948" w:type="dxa" /><w:tblBorders><w:top
w:val="single" w:sz="4" wx:bdrwidth="10" w:space="0" w:color="auto" /><w:left
w:val="single" w:sz="4" wx:bdrwidth="10" w:space="0" w:color="auto" /><w:bottom
w:val="single" w:sz="4" wx:bdrwidth="10" w:space="0" w:color="auto" /><w:right
w:val="single" w:sz="4" wx:bdrwidth="10" w:space="0" w:color="auto" /><w:insideH
w:val="single" w:sz="4" wx:bdrwidth="10" w:space="0" w:color="auto" /><w:insideV
w:val="single" w:sz="4" wx:bdrwidth="10" w:space="0" w:color="auto" /></w:tblBorders><w:tblLayout
w:type="Fixed" /><w:tblCellMar><w:top w:w="0" w:type="dxa" /><w:left
w:w="108" w:type="dxa" /><w:bottom w:w="0" w:type="dxa" /><w:right
w:w="108" w:type="dxa" /></w:tblCellMar></w:tblPr><w:tblGrid><w:gridCol
w:w="1155" /><w:gridCol w:w="1155" /><w:gridCol
w:w="840" /><w:gridCol w:w="1365" /><w:gridCol w:w="1365" /><w:gridCol
w:w="2625" /><w:gridCol w:w="1575" /><w:gridCol
w:w="2835" /></w:tblGrid><w:tr><w:tblPrEx><w:tblBorders><w:top
w:val="single" w:sz="4" wx:bdrwidth="10" w:space="0" w:color="auto" /><w:left
w:val="single" w:sz="4" wx:bdrwidth="10" w:space="0" w:color="auto" /><w:bottom
w:val="single" w:sz="4" wx:bdrwidth="10" w:space="0" w:color="auto" /><w:right
w:val="single" w:sz="4" wx:bdrwidth="10" w:space="0" w:color="auto" /><w:insideH
w:val="single" w:sz="4" wx:bdrwidth="10" w:space="0" w:color="auto" /><w:insideV
w:val="single" w:sz="4" wx:bdrwidth="10" w:space="0" w:color="auto" /></w:tblBorders><w:tblCellMar><w:top
w:w="0" w:type="dxa" /><w:left w:w="108" w:type="dxa" /><w:bottom
w:w="0" w:type="dxa" /><w:right w:w="108" w:type="dxa" /></w:tblCellMar></w:tblPrEx><w:trPr><w:cantSplit
w:val="on" /><w:trHeight w:val="465" w:h-rule="atLeast" /></w:trPr><w:tc><w:tcPr><w:tcW
w:w="1155" w:type="dxa" /><w:shd w:val="clear" w:color="auto"
w:fill="auto" /><w:vAlign w:val="center" /></w:tcPr><w:p><w:pPr><w:jc
w:val="center" /><w:rPr><w:rFonts w:hint="fareast" /><w:b /><w:sz
w:val="24" /></w:rPr></w:pPr><w:r><w:rPr><w:rFonts w:hint="fareast" /><w:b /><w:sz
w:val="24" /></w:rPr><w:t>序號</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW
w:w="1155" w:type="dxa" /><w:shd w:val="clear" w:color="auto"
w:fill="auto" /><w:vAlign w:val="center" /></w:tcPr><w:p><w:pPr><w:jc
w:val="center" /><w:rPr><w:rFonts w:hint="fareast" /><w:b /><w:sz
w:val="24" /></w:rPr></w:pPr><w:r><w:rPr><w:rFonts w:hint="fareast" /><w:b /><w:sz
w:val="24" /></w:rPr><w:t>姓   名</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW
w:w="840" w:type="dxa" /><w:shd w:val="clear" w:color="auto"
w:fill="auto" /><w:vAlign w:val="center" /></w:tcPr><w:p><w:pPr><w:jc
w:val="center" /><w:rPr><w:rFonts w:hint="fareast" /><w:b /><w:sz
w:val="24" /></w:rPr></w:pPr><w:r><w:rPr><w:rFonts w:hint="fareast" /><w:b /><w:sz
w:val="24" /></w:rPr><w:t>性別</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW
w:w="1365" w:type="dxa" /><w:shd w:val="clear" w:color="auto"
w:fill="auto" /><w:vAlign w:val="center" /></w:tcPr><w:p><w:pPr><w:jc
w:val="center" /><w:rPr><w:rFonts w:hint="fareast" /><w:b /><w:sz
w:val="24" /></w:rPr></w:pPr><w:r><w:rPr><w:rFonts w:hint="fareast" /><w:b /><w:sz
w:val="24" /></w:rPr><w:t>出生日期</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW
w:w="1365" w:type="dxa" /><w:shd w:val="clear" w:color="auto"
w:fill="auto" /><w:vAlign w:val="center" /></w:tcPr><w:p><w:pPr><w:jc
w:val="center" /><w:rPr><w:rFonts w:hint="fareast" /><w:b /><w:sz
w:val="24" /></w:rPr></w:pPr><w:r><w:rPr><w:rFonts w:hint="fareast" /><w:b /><w:sz
w:val="24" /></w:rPr><w:t>出生地</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW
w:w="2625" w:type="dxa" /><w:shd w:val="clear" w:color="auto"
w:fill="auto" /><w:vAlign w:val="center" /></w:tcPr><w:p><w:pPr><w:jc
w:val="center" /><w:rPr><w:rFonts w:hint="fareast" /><w:b /><w:sz
w:val="24" /></w:rPr></w:pPr><w:r><w:rPr><w:rFonts w:hint="fareast" /><w:b /><w:sz
w:val="24" /></w:rPr><w:t>身份證號碼</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW
w:w="1575" w:type="dxa" /><w:shd w:val="clear" w:color="auto"
w:fill="auto" /><w:vAlign w:val="center" /></w:tcPr><w:p><w:pPr><w:jc
w:val="center" /><w:rPr><w:b /><w:sz w:val="24" /></w:rPr></w:pPr><w:r><w:rPr><w:rFonts
w:hint="fareast" /><w:b /><w:sz w:val="24" /></w:rPr><w:t>工作單位</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW
w:w="2835" w:type="dxa" /><w:shd w:val="clear" w:color="auto"
w:fill="auto" /><w:vAlign w:val="center" /></w:tcPr><w:p><w:pPr><w:jc
w:val="center" /><w:rPr><w:rFonts w:hint="fareast" /><w:b /><w:sz
w:val="24" /></w:rPr></w:pPr><w:r><w:rPr><w:rFonts w:hint="fareast" /><w:b /><w:sz
w:val="24" /></w:rPr><w:t>職務</w:t></w:r></w:p></w:tc></w:tr>
<!-- 資料行START -->

<#if pageList?exists && (pageList?size>0)>
<#list pageList as person>
<w:tr><w:tblPrEx><w:tblBorders><w:top
w:val="single" w:sz="4" wx:bdrwidth="10" w:space="0" w:color="auto" /><w:left
w:val="single" w:sz="4" wx:bdrwidth="10" w:space="0" w:color="auto" /><w:bottom
w:val="single" w:sz="4" wx:bdrwidth="10" w:space="0" w:color="auto" /><w:right
w:val="single" w:sz="4" wx:bdrwidth="10" w:space="0" w:color="auto" /><w:insideH
w:val="single" w:sz="4" wx:bdrwidth="10" w:space="0" w:color="auto" /><w:insideV
w:val="single" w:sz="4" wx:bdrwidth="10" w:space="0" w:color="auto" /></w:tblBorders><w:tblCellMar><w:top
w:w="0" w:type="dxa" /><w:left w:w="108" w:type="dxa" /><w:bottom
w:w="0" w:type="dxa" /><w:right w:w="108" w:type="dxa" /></w:tblCellMar></w:tblPrEx><w:trPr><w:cantSplit
w:val="on" /><w:trHeight w:val="465" w:h-rule="atLeast" /></w:trPr><w:tc><w:tcPr><w:tcW
w:w="1155" w:type="dxa" /><w:shd w:val="clear" w:color="auto"
w:fill="auto" /><w:vAlign w:val="center" /></w:tcPr><w:p><w:pPr><w:jc
w:val="center" /><w:rPr><w:sz w:val="24" /></w:rPr></w:pPr><w:r><w:rPr><w:sz
w:val="24" /></w:rPr><w:t>${person_index + 1}</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW
w:w="1155" w:type="dxa" /><w:shd w:val="clear" w:color="auto"
w:fill="auto" /><w:vAlign w:val="center" /></w:tcPr><w:p><w:pPr><w:jc
w:val="center" /><w:rPr><w:rFonts w:hint="fareast" /><w:sz
w:val="24" /></w:rPr></w:pPr><w:r><w:rPr><w:rFonts w:hint="fareast" /><w:sz
w:val="24" /></w:rPr><w:t>${person.userName!''}</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW
w:w="840" w:type="dxa" /><w:shd w:val="clear" w:color="auto"
w:fill="auto" /><w:vAlign w:val="center" /></w:tcPr><w:p><w:pPr><w:jc
w:val="center" /><w:rPr><w:rFonts w:hint="fareast" /><w:sz
w:val="24" /></w:rPr></w:pPr><w:r><w:rPr><w:rFonts w:hint="fareast" /><w:sz
w:val="24" /></w:rPr><w:t>${person.sex!''}</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW
w:w="1365" w:type="dxa" /><w:shd w:val="clear" w:color="auto"
w:fill="auto" /><w:vAlign w:val="center" /></w:tcPr><w:p><w:pPr><w:jc
w:val="center" /><w:rPr><w:rFonts w:hint="fareast" /><w:sz
w:val="24" /></w:rPr></w:pPr><w:r><w:rPr><w:sz w:val="24" /></w:rPr><w:t>${person.birthDate!''}</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW
w:w="1365" w:type="dxa" /><w:shd w:val="clear" w:color="auto"
w:fill="auto" /><w:vAlign w:val="center" /></w:tcPr><w:p><w:pPr><w:jc
w:val="center" /><w:rPr><w:rFonts w:hint="fareast" /><w:sz
w:val="24" /></w:rPr></w:pPr><w:r><w:rPr><w:rFonts w:hint="fareast" /><w:sz
w:val="24" /></w:rPr><w:t>${person.bornIn!''}</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW
w:w="2625" w:type="dxa" /><w:shd w:val="clear" w:color="auto"
w:fill="auto" /><w:vAlign w:val="center" /></w:tcPr><w:p><w:pPr><w:jc
w:val="center" /><w:rPr><w:rFonts w:hint="fareast" /><w:sz
w:val="24" /></w:rPr></w:pPr><w:r><w:rPr><w:sz w:val="24" /></w:rPr><w:t>${person.idNo!''}</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW
w:w="1575" w:type="dxa" /><w:shd w:val="clear" w:color="auto"
w:fill="auto" /><w:vAlign w:val="center" /></w:tcPr><w:p><w:pPr><w:jc
w:val="center" /><w:rPr><w:rFonts w:hint="fareast" /><w:sz
w:val="24" /></w:rPr></w:pPr><w:r><w:rPr><w:rFonts w:hint="fareast" /><w:sz
w:val="24" /></w:rPr><w:t>${person.depDes!''}</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW
w:w="2835" w:type="dxa" /><w:shd w:val="clear" w:color="auto"
w:fill="auto" /><w:vAlign w:val="center" /></w:tcPr><w:p><w:pPr><w:jc
w:val="center" /><w:rPr><w:rFonts w:hint="fareast" /><w:sz
w:val="24" /></w:rPr></w:pPr><w:r><w:rPr><w:rFonts w:hint="fareast" /><w:sz
w:val="24" /></w:rPr><w:t>${person.postDuty!''}</w:t></w:r></w:p></w:tc></w:tr>
</#list>
</#if>

<!-- 資料行END -->

</w:tbl><w:p><w:pPr><w:ind
w:left="8820" w:left-chars="4200" /><w:rPr><w:rFonts w:hint="fareast" /></w:rPr></w:pPr></w:p><w:p><w:pPr><w:ind
w:left="8820" w:left-chars="4200" /><w:rPr><w:rFonts w:hint="fareast" /></w:rPr></w:pPr></w:p><w:p><w:pPr><w:ind
w:left="8820" w:left-chars="4200" /><w:rPr><w:rFonts w:hint="fareast" /><w:b /><w:b-cs /></w:rPr></w:pPr><w:r><w:rPr><w:rFonts
w:hint="fareast" /><w:b /><w:b-cs /></w:rPr><w:t>公章:</w:t></w:r></w:p><w:p><w:pPr><w:ind
w:left="8820" w:left-chars="4200" /><w:rPr><w:rFonts w:hint="fareast" /><w:b /><w:b-cs /></w:rPr></w:pPr></w:p><w:p><w:pPr><w:ind
w:left="9240" w:left-chars="4400" /><w:rPr><w:rFonts w:hint="fareast" /><w:b /><w:b-cs /></w:rPr></w:pPr><w:r><w:rPr><w:rFonts
w:hint="fareast" /><w:b /><w:b-cs /></w:rPr><w:t>${year!''}年${month!''}月${day!''}日</w:t></w:r></w:p><w:sectPr><w:pgSz
w:w="16838" w:h="11906" w:orient="landscape" /><w:pgMar w:top="1474"
w:right="1440" w:bottom="1701" w:left="1440" w:header="851" w:footer="992"
w:gutter="0" /><w:cols w:space="720" /><w:docGrid
w:type="lines-and-chars" w:line-pitch="312" /></w:sectPr></wx:sect></w:body></w:wordDocument>

接下來就是後臺程式碼了,直接上圖:

後臺程式碼:

@RequestMapping("/exportWord.do")
public void exportWord(HttpServletRequest request,HttpServletResponse response,String certGroupId,String orderType){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String dateString = sdf.format(new Date());//2018-07-03
String basePath =request.getSession().getServletContext().getRealPath("/");//絕對路徑    
String basePath2=basePath.replaceAll("\\\\", "/");  
Configuration cfg = FreemarkerConfiguration.getConfiguation();
//封裝返回資料
Map<String,Object> returnMap = new HashMap<>();
try {
cfg.setDirectoryForTemplateLoading(new File(basePath2 + "/WEB-INF/pages/icert/group"));
List<HrCertGroupPersonExt> pageList = hrCertGroupPersonService.selectPersonListByCertGroupId(certGroupId,orderType);
returnMap.put("pageList", pageList);
returnMap.put("total", pageList.size());
returnMap.put("year",dateString.substring(0, 4));
returnMap.put("month",dateString.substring(5, 7));
returnMap.put("day",dateString.substring(8, 10));
request.setCharacterEncoding("UTF-8"); 
    response.setContentType("application/x-download;");  
    response.setHeader("Content-disposition", "attachment; filename="  
                + new String("出國人員名單.doc".getBytes("gb2312"), "ISO8859-1"));
    response.setCharacterEncoding("UTF-8");
    cfg.getTemplate("/groupWord.xml").process(returnMap,response.getWriter());
} catch (Exception e) {
e.printStackTrace();
}
}

至此,用freemarker匯出word完成,流程與匯出excel一致。

碼字不易,轉載請註明出處!

相關推薦

使用freemarker匯出複雜excel表格word

        最近因為專案需要,需要匯出複雜的excel表格。由於需要合併單元格以及樣式處理,用poi匯出顯得比較困難。因為之前用freemarker匯出過pdf/word檔案,就想著能不能用其匯出excel。經過一番實驗,大功告成,真是比poi好用N多倍呀!廢話少說,直接

java利用Freemarker模板生成docx格式的word

之前寫過一篇利用Freemarker模板生成doc的部落格,不過那個部落格有點缺陷,生成的word佔用的空間很大,幾百頁的word有將近100M了。所以,後面需求必須是生成的docx文件,結果匯出後正常才幾M,昨天花了一天的時間實現。 具體思路 1.把docx文件修改為ZIP格式(修改

swagger2 匯出api為html和word

匯出為html https://cloud.tencent.com/developer/article/1332445 匯出為word https://www.cnblogs.com/jmcui/p/8298823.html https://github.com/JMCuixy/Sw

除錯經驗——使用VBA在Excel中開啟Word(Open Word file in Excel with VBA)

VBA的一個強項是可以跨應用操作,特別是在office各程式內部。以下程式碼實現了在Excel中開啟Word文件。程式碼:Sub openWord() '在Excel中開啟Word文件 ' '

Windows匯出mysql表結構到word

推薦一個實用的工具DBExportDoc V1.0 For MySQL,直接使用其中的Word文件 首先需要安裝mysql-connector-odbc,安裝完成後,在控制面板\系統和安全\管理工具\資料來源(ODBC)中配置資料庫資訊 然後根據文件提示一步步操作即可,如果匯出來的是中文亂碼

如何將excel轉換成word格式

有時想把編輯好的excel按照原格式放在word裡,可採用複製貼上的方法得到的結果根本不讓人滿意,複製過去之後檔案會很亂,沒有整體感,但要自己再重新調整格式的話,費時費力,其實這個時候我們可以考慮怎麼直接將excel轉換成word格式。 excel轉換成word格式很簡

Java中使用freemarker匯出word(詳細案例,含圖片和表格

Java中使用freemarker匯出word文件(自己真實專案案例) 最近做了一個專案,有一個需求是要將職員的一些資料按照指定的樣式匯出成一個word文件出來,由於自己是一個Java小白,不懂,於是便在網上找方法,網上介紹的有很多,因為原來用poi匯出過pp

使用Freemarker生成Word並在內新增Echarts圖形報表或迴圈新增表格圖片資料

一、製作.ftl字尾的word模板檔案 1、新建一個word文件模板          使用其他文字編輯器編寫表示式,如:Editplus    2、將word文件另存為xml並改名為.ftl字尾的檔案 另存完之後關閉word文件,將demo.xml的字尾修改

使用freemarker按模板匯出表格word

一.製作模板    1.將word文件製作成以下樣式,需要替換的內容放佔位符,並按照佔位符名字替換資料。表格裡,使用RR.XXX的形式的佔位符。RR表示當前行物件的名字,XXX相當於物件的屬性名。寫後臺的時候,根據這些名將資料存為map,即名字為鍵值。例子如下:    2.將

Java後端使用Freemarker匯出word的各種細節

1.前言 最近在專案中,因客戶要求,需要做一個匯出成word的功能(比如月度報表等),技術選型也考慮過幾種,比如easypoi,itext,但發現這兩種在實現起來有困難,所以最終還是選Freemarker模板進行匯出,靈活性比較好。 2.實現步驟 1.準備好標準文件的word

使用EasyPoi根據模板匯出Excelword

接著上篇文章 Java根據模板匯出Excel並生成多個Sheet 簡單介紹下EasyPoi的使用,直接上程式碼吧 首先當然是先引入jar包了,看下圖 其次,還是貼程式碼吧看例項,下面是根據模板匯出的工具類,包含Excel和word /**  * 匯

使用Freemarker匯出Word(包含圖片)程式碼實現及總結

本篇是關於利用FreeMarker匯出Word的實現步驟。採用FreeMarker非常的靈活,能夠按照自己指定的樣式設定並輸出內容,操作簡單方便,程式碼實現也容易。 下面是實現的效果圖: 下面是實現步驟: 1.新增FreeMarker需要的jar包(這裡用的是2.3.28版本,從網上的maven倉庫

如何將excel表格轉化為word(去掉表格

第一次寫這種技術的部落格,有點辛苦,中間有一次,CSDN抽風,導致我不得不重新寫。 這是我自己摸索的,之前參考其他的教程,但是還是有點不同,他們用的是老舊的excel 2010和word 2010。 工

將jsp頁面生成wordexcelpdf

      開發過程中,有時要求將顯示查詢結果的jsp頁面生成word、excel、pdf等型別的文件,甚至可能要求將那些格式的文件下載下來,因為這樣更符合客戶的需求,也更利於他們的辦公使用。        基於此,上網查閱了一些資料,現簡單整理一下,以供自己和大家學習。

利用freemarker 在模板裡面寫入動態資料,動態表格,圖片插入並生成word

利用freemarker 在模板裡面寫入動態資料,動態表格,圖片插入。 以下測試程式碼圖片(image.jpg)和模板(template.xml)是直接放到src目錄下面的,可以根據自己需求調整 廢話不多說,直接上程式碼: 程式碼塊 impor

JAVA利用FreeMarker生成(匯出Word

一、首先下載相對應的jar包:freemarker.jar; 而我的專案環境是:maven 在pom.xml中: <dependency> <groupId>freemarker</groupId> <artifactId>f

C#也能動態生成Word並填充資料, 匯出EXCEL 方法

        public string CreateWordFile(string CheckedInfo)         ...{             string message = "";             try             ...{                 Ob

使用FreeMarker生成Word中的表格

    一、首先還是在Word中建立模板,只需要寫好表頭。如圖1所示:   圖1     二、編寫模板,並設定插值。 <#list schoolSafetyNotifications as listKey> <w:tr w:rsidR="006332C

java中使用freemarker匯出word

公司專案要求要有一個能匯出成一份word文件的功能,於是就上網搜了一些教程,但我跟著教程使用的時候還是遇到了很多問題,這裡我自己總結一份我作為新手使用freemarker能看懂的文章。可能以後自己還會用到,至少到時候忘了還可以翻看一下。 首先要自己在網上下載一個freema

使用FreeMarker匯出複雜Word(具體實現)

(1)使用freemarker,首先在pom.xml裡新增meaven依賴,如下圖: (2)建立一個Word文件,在這裡我使用model.doc檔案,在需要替換的地方使用”${XXX}”,PS:需要注意的是:書寫”${XXX}”的時候,必須從左至右一個個書