天天看點

利用itext導出word表格,處理圖檔

    在實際的項目開發中我們需要将背景大量資料導出為word或者是excel友善使用者操作,當然能完成這一功能的有freemarker,itext,poi等技術,本文講述以itext導出word。

   首先我們需要明白的是無論是freemarker,itext,poi都是先做好模闆或者是先畫出模闆然後在其中填充内容,那麼有了這種思想,就能做好這一功能。

    開發必須的三個架包:

<a href="http://s3.51cto.com/wyfs02/M02/11/AE/wKiom1LY2vvANViGAACZHD5WK4M639.jpg" target="_blank"></a>

下面我們直接上代碼:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

<code>package</code> <code>com.csg.action;</code>

<code>import</code> <code>java.awt.Color;</code>

<code>import</code> <code>java.io.FileOutputStream;</code>

<code>import</code> <code>java.io.IOException;</code>

<code>import</code> <code>com.lowagie.text.Cell;</code>

<code>import</code> <code>com.lowagie.text.Document;</code>

<code>import</code> <code>com.lowagie.text.DocumentException;</code>

<code>import</code> <code>com.lowagie.text.Element;</code>

<code>import</code> <code>com.lowagie.text.Font;</code>

<code>import</code> <code>com.lowagie.text.Image;</code>

<code>import</code> <code>com.lowagie.text.PageSize;</code>

<code>import</code> <code>com.lowagie.text.Paragraph;</code>

<code>import</code> <code>com.lowagie.text.Phrase;</code>

<code>import</code> <code>com.lowagie.text.Table;</code>

<code>import</code> <code>com.lowagie.text.pdf.BaseFont;</code>

<code>import</code> <code>com.lowagie.text.rtf.RtfWriter2;</code>

<code>import</code> <code>com.lowagie.text.rtf.style.RtfFont;</code>

<code>public</code> <code>class</code> <code>WordTest2 {</code>

<code>    </code><code>public</code> <code>void</code> <code>createDocContext(String file) </code><code>throws</code> <code>DocumentException,</code>

<code>            </code><code>IOException {</code>

<code>        </code><code>// 設定紙張大小</code>

<code>        </code><code>Document document = </code><code>new</code> <code>Document(PageSize.A4);</code>

<code>        </code><code>// 建立一個書寫器(Writer)與document對象關聯,通過書寫器(Writer)可以将文檔寫入到磁盤中</code>

<code>        </code><code>RtfWriter2.getInstance(document, </code><code>new</code> <code>FileOutputStream(file));</code>

<code>        </code><code>document.open();</code>

<code>        </code><code>// 設定中文字型</code>

<code>        </code><code>BaseFont bfChinese = BaseFont.createFont(</code><code>"STSongStd-Light"</code><code>,</code>

<code>                </code><code>"UniGB-UCS2-H"</code><code>, BaseFont.NOT_EMBEDDED);</code>

<code>        </code><code>// 标題字型風格</code>

<code>        </code><code>RtfFont titleFont = </code><code>new</code> <code>RtfFont(</code><code>"宋體"</code><code>, </code><code>21</code><code>, Font.BOLD, Color.BLACK);</code>

<code>        </code><code>/* Font titleFont = new Font(bfChinese, 21, Font.BOLD); */</code>

<code>        </code><code>// 正文字型風格</code>

<code>        </code><code>Font contextFont = new Font(bfChinese, 12, Font.NORMAL);</code>

<code>        </code><code>Paragraph title = new Paragraph("xx畫院藝術品收藏稽核表");</code>

<code>        </code><code>// 設定标題格式對齊方式</code>

<code>        </code><code>title.setAlignment(Element.ALIGN_CENTER);</code>

<code>        </code><code>title.setFont(titleFont);</code>

<code>        </code><code>document.add(title);</code>

<code>        </code><code>// 設定 Table 表格</code>

<code>        </code><code>Table aTable = new Table(2);// 設定表格為2列</code>

<code>        </code><code>int width[] = { 4, 96 };// 每列的占比例</code>

<code>        </code><code>aTable.setWidths(width);// 設定每列所占比例</code>

<code>        </code><code>aTable.setWidth(100); // 占頁面寬度 100%</code>

<code>        </code><code>aTable.setAlignment(Element.ALIGN_CENTER);// 居中顯示</code>

<code>        </code><code>aTable.setAlignment(Element.ALIGN_MIDDLE);// 縱向居中顯示</code>

<code>        </code><code>aTable.setAutoFillEmptyCells(true); // 自動填滿</code>

<code>        </code><code>aTable.setBorderWidth(1); // 邊框寬度</code>

<code>        </code><code>aTable.setBorderColor(Color.BLACK); // 邊框顔色</code>

<code>        </code><code>aTable.setPadding(0);// 襯距,看效果就知道什麼意思了</code>

<code>        </code><code>aTable.setSpacing(0);// 即單元格之間的間距</code>

<code>        </code><code>aTable.setBorder(1);// 邊框</code>

<code>        </code><code>Font fontChinese = new Font(bfChinese, 12, Font.NORMAL, Color.black);</code>

<code>        </code><code>Cell cell = new Cell(new Phrase("藝術品情況說明", fontChinese));</code>

<code>        </code><code>cell.setHorizontalAlignment(Element.ALIGN_CENTER);</code>

<code>        </code><code>cell.setBorderColor(Color.BLACK);</code>

<code>        </code><code>aTable.addCell(cell);</code>

<code>        </code><code>cell = new Cell(new Phrase("收藏xxx版畫作品14張合共6.5萬元,從本人手中直接購買。",</code>

<code>                </code><code>fontChinese));</code>

<code>        </code><code>cell.setVerticalAlignment(Element.ALIGN_MIDDLE);</code>

<code>        </code><code>cell = new Cell("#5");</code>

<code>        </code><code>cell = new Cell(new Phrase("填表人:_________ 日期:___年___月___日" + "   ",</code>

<code>        </code><code>cell.setHorizontalAlignment(Element.ALIGN_RIGHT);</code>

<code>        </code><code>document.add(aTable);</code>

<code>        </code><code>// 設定Table表格,建立一個10列的表格,背景循環資料</code>

<code>        </code><code>aTable = new Table(10);</code>

<code>        </code><code>aTable.setWidths(new int[] { 4, 10, 20, 10, 20, 10, 20, 10, 20, 6 });</code>

<code>        </code><code>aTable.setWidth(100);</code>

<code>        </code><code>aTable.addCell(new Cell("#1"));</code>

<code>        </code><code>aTable.addCell(new Cell("序号"));</code>

<code>        </code><code>aTable.addCell(new Cell("作品名稱"));</code>

<code>        </code><code>aTable.addCell(new Cell("作者"));</code>

<code>        </code><code>aTable.addCell(new Cell("年代"));</code>

<code>        </code><code>aTable.addCell(new Cell("品名"));</code>

<code>        </code><code>aTable.addCell(new Cell("尺寸"));</code>

<code>        </code><code>aTable.addCell(new Cell("件數"));</code>

<code>        </code><code>aTable.addCell(new Cell("收藏金額(不含稅)"));</code>

<code>        </code><code>aTable.addCell(new Cell("稅收"));</code>

<code>        </code><code>for (int i = 0; i &lt; 10; i++) {</code>

<code>            </code><code>aTable.addCell(new Cell(""));</code>

<code>            </code><code>aTable.addCell(new Cell("第" + (i + 1) + "件"));</code>

<code>            </code><code>aTable.addCell(new Cell("1234"));</code>

<code>            </code><code>aTable.addCell(new Cell("123"));</code>

<code>        </code><code>}</code>

<code>        </code><code>;</code>

<code>        </code><code>aTable = new Table(2);// 設定表格為2列</code>

<code>        </code><code>aTable.setWidths(new int[] { 5, 95 });// 設定每列所占比例</code>

<code>        </code><code>cell = new Cell(new Phrase("征集小組組員執行征集情況說明", fontChinese));</code>

<code>        </code><code>cell = new Cell(</code>

<code>                </code><code>new Phrase(</code>

<code>                        </code><code>"志強、學靈經手向xxx征集版畫作品14張,合共6.5萬元,具體明細如上。經綜合分析,這14件作品建議列入“藏品”收藏,發收藏證書。當否,請稽核",</code>

<code>                        </code><code>fontChinese));</code>

<code>        </code><code>cell = new Cell(new Phrase("征集小組組員簽名", fontChinese));</code>

<code>        </code><code>cell.setRowspan(8);// 合并行</code>

<code>        </code><code>cell = new Cell(new Phrase("", fontChinese));</code>

<code>        </code><code>cell.setRowspan(8);</code>

<code>        </code><code>cell = new Cell(new Phrase("征集小組副組長稽核", fontChinese));</code>

<code>        </code><code>cell.setRowspan(9);</code>

<code>        </code><code>cell = new Cell(new Phrase("征集小組組長稽核", fontChinese));</code>

<code>        </code><code>cell = new Cell(new Phrase("藝術委員會意見", fontChinese));</code>

<code>        </code><code>// 設定5個表格 循環5次</code>

<code>        </code><code>for (int i = 0; i &lt; 5; i++) {</code>

<code>            </code><code>// 列印圖像</code>

<code>            </code><code>aTable = new Table(2);// 設定表格為2列3行</code>

<code>            </code><code>aTable.setWidths(new int[] { 50, 50 });// 設定每列所占比例</code>

<code>            </code><code>aTable.setWidth(100); // 占頁面寬度 100%</code>

<code>            </code><code>// 添加圖像1</code>

<code>            </code><code>cell = new Cell();</code>

<code>            </code><code>cell.setBorderWidth(0);// 邊框線設為0</code>

<code>            </code><code>Image img = Image.getInstance("d:/201305.jpg");</code>

<code>            </code><code>img.setAbsolutePosition(0, 0);</code>

<code>            </code><code>img.scaleAbsolute(12, 35);// 直接設定顯示尺寸</code>

<code>            </code><code>img.scalePercent(50);// 表示顯示的大小為原尺寸的50%</code>

<code>            </code><code>img.scalePercent(25, 50);// 圖像高寬的顯示比例</code>

<code>            </code><code>img.setRotation(30);// 圖像旋轉一定角度</code>

<code>            </code><code>cell.add(img);</code>

<code>            </code><code>aTable.addCell(cell);</code>

<code>            </code><code>// 添加圖像2</code>

<code>            </code><code>cell.setBorderWidth(0);</code>

<code>            </code><code>cell.setVerticalAlignment(Element.ALIGN_RIGHT);</code>

<code>            </code><code>img.setAlignment(Image.RIGHT);// 設定圖檔顯示位置</code>

<code>            </code><code>img = Image.getInstance("d:/201307.jpg");</code>

<code>            </code><code>/*</code>

<code>             </code><code>* img.scaleAbsolute(12,50);//直接設定顯示尺寸</code>

<code>             </code><code>*/</code><code>img.scalePercent(</code><code>50</code><code>);</code><code>// 表示顯示的大小為原尺寸的50%</code>

<code>            </code><code>img.scalePercent(</code><code>25</code><code>, </code><code>50</code><code>);</code><code>// 圖像高寬的顯示比例</code>

<code>            </code><code>img.setRotation(</code><code>30</code><code>);</code><code>// 圖像旋轉一定角度</code>

<code>            </code><code>// 資訊</code>

<code>            </code><code>cell = </code><code>new</code> <code>Cell(</code><code>new</code> <code>Phrase(</code><code>"作品1:《文明符号迴響》"</code><code>, fontChinese));</code>

<code>            </code><code>cell.setBorderWidth(</code><code>0</code><code>);</code>

<code>            </code><code>cell = </code><code>new</code> <code>Cell(</code><code>new</code> <code>Phrase(</code><code>"作品2:《石語之一》"</code><code>, fontChinese));</code>

<code>            </code><code>cell = </code><code>new</code> <code>Cell(</code>

<code>                    </code><code>new</code> <code>Phrase(</code><code>"落款:23/25 文明符号迴響 xxx 2009"</code><code>, fontChinese));</code>

<code>            </code><code>cell = </code><code>new</code> <code>Cell(</code><code>new</code> <code>Phrase(</code><code>"落款:7/15石語之一 石版畫 xxx 2011"</code><code>, fontChinese));</code>

<code>            </code><code>document.add(aTable);</code>

<code>        </code><code>document.add(</code><code>new</code> <code>Paragraph(</code><code>"\n"</code> <code>+ </code><code>"\n"</code><code>));</code>

<code>        </code><code>title = </code><code>new</code> <code>Paragraph(</code><code>"作者簡介"</code><code>);</code>

<code>        </code><code>titleFont = </code><code>new</code> <code>RtfFont(</code><code>"宋體"</code><code>, </code><code>16</code><code>, Font.BOLD, Color.BLACK);</code>

<code>        </code><code>title.setSpacingAfter(</code><code>10</code><code>);</code>

<code>        </code><code>// 正文</code>

<code>        </code><code>String contextString = </code><code>"xxx1969-年生于北京南靖,籍貫廣東潮州。1996年畢業于廣州美術學院版畫系并留校工作, 現為廣州美術學院版畫系副教授,山西大學客座教授;中國美術家協會會員,中國美術家協會藏書票研究會常務副主席,廣東青年畫院畫家。"</code>

<code>                </code><code>+ </code><code>"多件作品入選全國美展、全國版畫展、國際版畫展等學術展。曾獲第九屆全國美術作品展優秀獎、當代中國青年作品展二等獎、國務院慶祝澳門回歸祖國宣傳招貼畫十佳作品獎、全軍廉政文化優秀作品展二等獎、廣東省美術作品展銅獎、廣東版畫獎等學術獎項。"</code>

<code>                </code><code>+ </code><code>" \n"</code><code>// 換行</code>

<code>                </code><code>+ </code><code>"作品、論文發表于《美術》、《美術觀察》、《中國版畫》、《美術學報》、《北方美術》等學術刊物。出版教材《石版畫》、《版畫》(合著)等 ;出版作品集《中銳xxx作品集》;入編《中國美術大事記》、《當代中國藝術》、《中國當代中青年版畫家石版畫精品集》等。"</code>

<code>                </code><code>+ </code><code>"作品被廣東美術館、神州版畫博物館、中華世紀壇、紹興魯迅紀念館、觀瀾美術館、汕頭博物館、廣州美術學院、伊斯坦布爾國際版畫館等學術機構及個人收藏。"</code><code>;</code>

<code>        </code><code>Paragraph context = </code><code>new</code> <code>Paragraph(contextString);</code>

<code>        </code><code>// 正文格式左對齊</code>

<code>        </code><code>context.setAlignment(Element.ALIGN_LEFT);</code>

<code>        </code><code>context.setFont(contextFont);</code>

<code>        </code><code>// 離上一段落(标題)空的行數</code>

<code>        </code><code>context.setSpacingBefore(</code><code>10</code><code>);</code>

<code>        </code><code>// 設定第一行空的列數</code>

<code>        </code><code>context.setFirstLineIndent(</code><code>20</code><code>);</code>

<code>        </code><code>document.add(context);</code>

<code>        </code><code>document.close();</code>

<code>    </code><code>}</code>

<code>    </code><code>public</code> <code>static</code> <code>void</code> <code>main(String[] args) {</code>

<code>        </code><code>WordTest2 b = </code><code>new</code> <code>WordTest2();</code>

<code>        </code><code>try</code> <code>{</code>

<code>            </code><code>b.createDocContext(</code><code>"d:/demo.doc"</code><code>);</code>

<code>            </code><code>System.out.println(</code><code>"導出成功"</code><code>);</code>

<code>        </code><code>} </code><code>catch</code> <code>(DocumentException e) {</code>

<code>            </code><code>e.printStackTrace();</code>

<code>        </code><code>} </code><code>catch</code> <code>(IOException e) {</code>

<code>}</code>

   本文示例圖檔請直接拷貝到 D:盤!

本文以測試為主,實戰本代碼隻做參考。匆忙之作,歡迎指正!

本文轉自 小夜的傳說 51CTO部落格,原文連結:http://blog.51cto.com/1936625305/1352558,如需轉載請自行聯系原作者

繼續閱讀