直接运行就行:

    1. package myTest;
    2. import com.lowagie.text.*;
    3. import com.lowagie.text.rtf.style.RtfParagraphStyle;
    4. import dto.TempletResult;
    5. import dto.WordInfo;
    6. import java.awt.*;
    7. import java.io.IOException;
    8. import java.util.List;
    9. import java.util.List;
    10. public class WordDemo {
    11. public WordDemo() {
    12. }
    13. //原始的评估报告模板
    14. public static void excelInfo1()throws IOException, DocumentException {
    15. WordUtils wordUtils = new WordUtils();
    16. wordUtils.openDocument("D:\\机构自评估模板"+System.currentTimeMillis()+".doc");
    17. RtfParagraphStyle rtfGsBt1 = RtfParagraphStyle.STYLE_HEADING_1;
    18. rtfGsBt1.setAlignment(0);
    19. rtfGsBt1.setStyle(1);
    20. rtfGsBt1.setSize(15.0F);
    21. RtfParagraphStyle rtfGsBt2 = RtfParagraphStyle.STYLE_HEADING_2;
    22. rtfGsBt2.setAlignment(0);
    23. rtfGsBt2.setStyle(1);
    24. rtfGsBt2.setSize(13.0F);
    25. wordUtils.insertTitle("山东高速支付集团2020年风险评估报告", 13, 1, 1);
    26. wordUtils.insertTitle("评估周期:2020年1月1日-2020年12月31日", 8, 1, 1);
    27. wordUtils.insertTitlePattern("一、基本情况", rtfGsBt1);
    28. //wordUtils.insertTitlePatternSecond("1.1 第一小节内容", rtfGsBt2);
    29. wordUtils.insertContext("本次风险评估,风险指标156个,其中地域维度23个,客户维度45个,产品业务维度58个,渠道维度30个。控制措施指标67个,其中地域维度" +
    30. "10个,客户维度23个,产品业务维度20个,渠道维度14个", 12, 0, 1);
    31. wordUtils.insertTitlePattern("二、评估结果分析:", rtfGsBt1);
    32. wordUtils.insertTitlePatternSecond("(一)机构风险评估剩余风险", rtfGsBt2);
    33. wordUtils.getDocument().add(new Phrase(""));
    34. //调用插入表格
    35. wordUtils.getDocument().add((new WordDemo()).insertComplexTable("满意","高风险"));
    36. wordUtils.insertContext("\n",12,0,1);
    37. wordUtils.insertContext("本次风险评估:剩余风险为中高,固有风险评估结果为高风险、控制措施有效性评估结果为满意。\n", 12, 0, 1);
    38. wordUtils.insertContext("固有风险结果分析:地域维度风险较高的因素有风险因素1、风险因素2、风险" +
    39. "因素3;客户维度风险较高的因素有风险因素1、风险因素2、风险因素3;产品维度风险较高的因素有风险因素" +
    40. "1、风险因素2、风险因素3;渠道维度风险较高的因素有风险因素1、风险因素2、风险因素3;(备注:取固有" +
    41. "风险指标得分为最高分的风险因素)\n", 12, 0, 1);
    42. wordUtils.insertContext("控制措施有效性结果分析:地域维度控制措施有效性缺陷的措施有措施1、措施" +
    43. "2、措施3;客户维度控制措施有效性缺陷的措施有措施1、措施2、措施3;产品维度控制措施有效性缺陷的措施" +
    44. "有措施1、措施2、措施3;渠道维度控制措施有效性缺陷的措施有措施1、措施2、措施3;(备注:取控制措施" +
    45. "指标得分为最低分的控制措施)\n", 12, 0, 1);
    46. wordUtils.insertTitlePatternSecond("(二)各维度剩余风险较高的风险因素", rtfGsBt2);
    47. //调用插入表格
    48. wordUtils.getDocument().add((new WordDemo()).insertParameterTable());
    49. wordUtils.insertContext("\n",12,0,1);
    50. wordUtils.insertTitlePatternSecond("(三)地域维度", rtfGsBt2);
    51. wordUtils.insertContext("地域维度评估结果明细\n",12,0,1);
    52. wordUtils.insertTitlePatternSecond("(四)客户维度", rtfGsBt2);
    53. wordUtils.insertContext("\n",12,0,1);
    54. wordUtils.insertTitlePatternSecond("(五)产品维度", rtfGsBt2);
    55. wordUtils.insertContext("\n",12,0,1);
    56. wordUtils.insertTitlePatternSecond("(六)渠道维度", rtfGsBt2);
    57. wordUtils.insertContext("\n",12,0,1);
    58. wordUtils.insertTitlePatternSecond("(七)总结", rtfGsBt2);
    59. wordUtils.closeDocument();
    60. }
    61. public static void main(String[] args) throws IOException, DocumentException {
    62. excelInfo1();
    63. }
    64. public Table insertParameterTable() throws DocumentException {
    65. //初始化table
    66. Table table = new Table(3);
    67. int[] width = new int[]{20, 20, 20};
    68. table.setWidths(width);
    69. table.setWidth(100.0F);
    70. table.setAutoFillEmptyCells(true);
    71. table.setAlignment(1);
    72. table.setAlignment(5);
    73. table.setBorder(1000);
    74. table.setBorderWidth(1.0F);
    75. //第一行开始
    76. //第1个cell
    77. Cell cell = new Cell(new Phrase("维度"));
    78. cell.setVerticalAlignment(cell.ALIGN_MIDDLE);
    79. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
    80. table.addCell(cell);
    81. Cell cell2 = new Cell(new Phrase("高风险因素"));
    82. cell2.setVerticalAlignment(cell.ALIGN_MIDDLE);
    83. cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
    84. table.addCell(cell2);
    85. Cell cell3 = new Cell(new Phrase("控制措施缺陷点"));
    86. cell3.setVerticalAlignment(cell.ALIGN_MIDDLE);
    87. cell3.setHorizontalAlignment(Element.ALIGN_CENTER);
    88. table.addCell(cell3);
    89. Cell cell4 = new Cell(new Phrase("地域"));
    90. cell4.setVerticalAlignment(cell.ALIGN_MIDDLE);
    91. cell4.setHorizontalAlignment(Element.ALIGN_CENTER);
    92. table.addCell(cell4);
    93. Cell cell5 = new Cell(new Phrase("固有风险指标得分为最高分的风险因素"));
    94. cell5.setVerticalAlignment(cell.ALIGN_MIDDLE);
    95. cell5.setHorizontalAlignment(Element.ALIGN_CENTER);
    96. table.addCell(cell5);
    97. Cell cell6 = new Cell(new Phrase("控制措施指标得分为最低分的控制措施"));
    98. cell6.setVerticalAlignment(cell.ALIGN_MIDDLE);
    99. cell6.setHorizontalAlignment(Element.ALIGN_CENTER);
    100. table.addCell(cell6);
    101. Cell cell7 = new Cell(new Phrase("客户"));
    102. cell7.setVerticalAlignment(cell.ALIGN_MIDDLE);
    103. cell7.setHorizontalAlignment(Element.ALIGN_CENTER);
    104. table.addCell(cell7);
    105. Cell cell8 = new Cell(new Phrase(""));
    106. cell8.setVerticalAlignment(cell.ALIGN_MIDDLE);
    107. cell8.setHorizontalAlignment(Element.ALIGN_CENTER);
    108. table.addCell(cell8);
    109. Cell cell9 = new Cell(new Phrase(""));
    110. cell9.setVerticalAlignment(cell.ALIGN_MIDDLE);
    111. cell9.setHorizontalAlignment(Element.ALIGN_CENTER);
    112. table.addCell(cell9);
    113. Cell cell10 = new Cell(new Phrase("产品业务"));
    114. cell10.setVerticalAlignment(cell.ALIGN_MIDDLE);
    115. cell10.setHorizontalAlignment(Element.ALIGN_CENTER);
    116. table.addCell(cell10);
    117. Cell cell11 = new Cell(new Phrase(""));
    118. cell11.setVerticalAlignment(cell.ALIGN_MIDDLE);
    119. cell11.setHorizontalAlignment(Element.ALIGN_CENTER);
    120. table.addCell(cell11);
    121. Cell cell12 = new Cell(new Phrase(""));
    122. cell12.setVerticalAlignment(cell.ALIGN_MIDDLE);
    123. cell12.setHorizontalAlignment(Element.ALIGN_CENTER);
    124. table.addCell(cell12);
    125. Cell cell13 = new Cell(new Phrase("渠道"));
    126. cell13.setVerticalAlignment(cell.ALIGN_MIDDLE);
    127. cell13.setHorizontalAlignment(Element.ALIGN_CENTER);
    128. table.addCell(cell13);
    129. Cell cell14 = new Cell(new Phrase(""));
    130. cell14.setVerticalAlignment(cell.ALIGN_MIDDLE);
    131. cell14.setHorizontalAlignment(Element.ALIGN_CENTER);
    132. table.addCell(cell14);
    133. Cell cell15 = new Cell(new Phrase(""));
    134. cell15.setVerticalAlignment(cell.ALIGN_MIDDLE);
    135. cell15.setHorizontalAlignment(Element.ALIGN_CENTER);
    136. table.addCell(cell15);
    137. return table;
    138. }
    139. public Table insertComplexTable(String controlMeassure,String risk) throws DocumentException {
    140. //初始化table
    141. Table table = new Table(6);
    142. int[] width = new int[]{20, 20, 20, 20, 20,20};
    143. table.setWidths(width);
    144. table.setWidth(100.0F);
    145. table.setAutoFillEmptyCells(true);
    146. table.setAlignment(1);
    147. table.setAlignment(5);
    148. table.setBorder(1000);
    149. table.setBorderWidth(1.0F);
    150. //第一行开始
    151. //第1个cell
    152. Cell cell = new Cell(new Phrase("控制措施有效性/固有风险"));
    153. cell.setBackgroundColor(new Color(217, 241, 250));
    154. table.addCell(cell);
    155. //第2个cell
    156. Cell cell2 = new Cell(new Phrase("强健"));
    157. cell2.setVerticalAlignment(cell.ALIGN_MIDDLE);
    158. cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
    159. cell2.setBackgroundColor(new Color(217, 241, 250));
    160. table.addCell(cell2);
    161. //第3个cell
    162. Cell cell3 = new Cell(new Phrase("满意"));
    163. cell3.setVerticalAlignment(cell.ALIGN_MIDDLE);
    164. cell3.setHorizontalAlignment(Element.ALIGN_CENTER);
    165. cell3.setBackgroundColor(new Color(217, 241, 250));
    166. table.addCell(cell3);
    167. //第4个cell
    168. Cell cell4 = new Cell(new Phrase("一般"));
    169. cell4.setVerticalAlignment(cell.ALIGN_MIDDLE);
    170. cell4.setHorizontalAlignment(Element.ALIGN_CENTER);
    171. cell4.setBackgroundColor(new Color(217, 241, 250));
    172. table.addCell(cell4);
    173. //第5个cell
    174. Cell cell5 = new Cell(new Phrase("不充分"));
    175. cell5.setVerticalAlignment(cell.ALIGN_MIDDLE);
    176. cell5.setHorizontalAlignment(Element.ALIGN_CENTER);
    177. cell5.setBackgroundColor(new Color(217, 241, 250));
    178. table.addCell(cell5);
    179. //第6个cell
    180. Cell cell6 = new Cell(new Phrase("重大缺陷"));
    181. cell6.setVerticalAlignment(cell.ALIGN_MIDDLE);
    182. cell6.setHorizontalAlignment(Element.ALIGN_CENTER);
    183. cell6.setBackgroundColor(new Color(217, 241, 250));
    184. table.addCell(cell6);
    185. //第二行开始
    186. //第7个cell
    187. Cell cell7 = new Cell(new Phrase("高风险"));
    188. cell7.setVerticalAlignment(cell.ALIGN_MIDDLE);
    189. cell7.setHorizontalAlignment(Element.ALIGN_CENTER);
    190. cell7.setBackgroundColor(new Color(217, 241, 250));
    191. table.addCell(cell7);
    192. //第8个cell
    193. Cell cell8 = new Cell(new Phrase("中"));
    194. cell8.setVerticalAlignment(cell.ALIGN_MIDDLE);
    195. cell8.setHorizontalAlignment(Element.ALIGN_CENTER);
    196. if(controlMeassure.equals("强健")&&risk.equals("高风险")){
    197. cell8.setBackgroundColor(new Color(251, 221, 173));
    198. }
    199. table.addCell(cell8);
    200. //第9个cell
    201. Cell cell9 = new Cell(new Phrase("中高"));
    202. cell9.setVerticalAlignment(cell.ALIGN_MIDDLE);
    203. cell9.setHorizontalAlignment(Element.ALIGN_CENTER);
    204. if(controlMeassure.equals("满意")&&risk.equals("高风险")){
    205. cell9.setBackgroundColor(new Color(251, 221, 173));
    206. }
    207. table.addCell(cell9);
    208. //第10个cell
    209. Cell cell10 = new Cell(new Phrase("中高"));
    210. cell10.setVerticalAlignment(cell.ALIGN_MIDDLE);
    211. cell10.setHorizontalAlignment(Element.ALIGN_CENTER);
    212. if(controlMeassure.equals("一般")&&risk.equals("高风险")){
    213. cell10.setBackgroundColor(new Color(251, 221, 173));
    214. }
    215. table.addCell(cell10);
    216. //第11个cell
    217. Cell cell11 = new Cell(new Phrase("高"));
    218. cell11.setVerticalAlignment(cell.ALIGN_MIDDLE);
    219. cell11.setHorizontalAlignment(Element.ALIGN_CENTER);
    220. if(controlMeassure.equals("不充分")&&risk.equals("高风险")){
    221. cell11.setBackgroundColor(new Color(251, 221, 173));
    222. }
    223. table.addCell(cell11);
    224. //第12个cell
    225. Cell cell12 = new Cell(new Phrase("高"));
    226. cell12.setVerticalAlignment(cell.ALIGN_MIDDLE);
    227. cell12.setHorizontalAlignment(Element.ALIGN_CENTER);
    228. if(controlMeassure.equals("重大缺陷")&&risk.equals("高风险")){
    229. cell12.setBackgroundColor(new Color(251, 221, 173));
    230. }
    231. table.addCell(cell12);
    232. //第3行开始
    233. //第13个cell
    234. Cell cell13 = new Cell(new Phrase("较高风险"));
    235. cell13.setVerticalAlignment(cell.ALIGN_MIDDLE);
    236. cell13.setHorizontalAlignment(Element.ALIGN_CENTER);
    237. cell13.setBackgroundColor(new Color(217, 241, 250));
    238. table.addCell(cell13);
    239. //第14个cell
    240. Cell cell14 = new Cell(new Phrase("中"));
    241. cell14.setVerticalAlignment(cell.ALIGN_MIDDLE);
    242. cell14.setHorizontalAlignment(Element.ALIGN_CENTER);
    243. if(controlMeassure.equals("强健")&&risk.equals("较高风险")){
    244. cell14.setBackgroundColor(new Color(251, 221, 173));
    245. }
    246. table.addCell(cell14);
    247. //第15个cell
    248. Cell cell15 = new Cell(new Phrase("中"));
    249. cell15.setVerticalAlignment(cell.ALIGN_MIDDLE);
    250. cell15.setHorizontalAlignment(Element.ALIGN_CENTER);
    251. if(controlMeassure.equals("满意")&&risk.equals("较高风险")){
    252. cell15.setBackgroundColor(new Color(251, 221, 173));
    253. }
    254. table.addCell(cell15);
    255. //第16个cell
    256. Cell cell16 = new Cell(new Phrase("中高"));
    257. cell16.setVerticalAlignment(cell.ALIGN_MIDDLE);
    258. cell16.setHorizontalAlignment(Element.ALIGN_CENTER);
    259. if(controlMeassure.equals("一般")&&risk.equals("较高风险")){
    260. cell16.setBackgroundColor(new Color(251, 221, 173));
    261. }
    262. table.addCell(cell16);
    263. //第17个cell
    264. Cell cell17 = new Cell(new Phrase("中高"));
    265. cell17.setVerticalAlignment(cell.ALIGN_MIDDLE);
    266. cell17.setHorizontalAlignment(Element.ALIGN_CENTER);
    267. if(controlMeassure.equals("不充分")&&risk.equals("较高风险")){
    268. cell17.setBackgroundColor(new Color(251, 221, 173));
    269. }
    270. table.addCell(cell17);
    271. //第18个cell
    272. Cell cell18 = new Cell(new Phrase("高"));
    273. cell18.setVerticalAlignment(cell.ALIGN_MIDDLE);
    274. cell18.setHorizontalAlignment(Element.ALIGN_CENTER);
    275. if(controlMeassure.equals("重大缺陷")&&risk.equals("较高风险")){
    276. cell18.setBackgroundColor(new Color(251, 221, 173));
    277. }
    278. table.addCell(cell18);
    279. //第4行
    280. //第19个cell
    281. Cell cell19 = new Cell(new Phrase("中风险"));
    282. cell19.setVerticalAlignment(cell.ALIGN_MIDDLE);
    283. cell19.setHorizontalAlignment(Element.ALIGN_CENTER);
    284. cell19.setBackgroundColor(new Color(217, 241, 250));
    285. table.addCell(cell19);
    286. //第20个cell
    287. Cell cell20 = new Cell(new Phrase("中低"));
    288. cell20.setVerticalAlignment(cell.ALIGN_MIDDLE);
    289. cell20.setHorizontalAlignment(Element.ALIGN_CENTER);
    290. if(controlMeassure.equals("强健")&&risk.equals("中风险")){
    291. cell20.setBackgroundColor(new Color(251, 221, 173));
    292. }
    293. table.addCell(cell20);
    294. //第21个cell
    295. Cell cell21 = new Cell(new Phrase("中"));
    296. cell21.setVerticalAlignment(cell.ALIGN_MIDDLE);
    297. cell21.setHorizontalAlignment(Element.ALIGN_CENTER);
    298. if(controlMeassure.equals("满意")&&risk.equals("中风险")){
    299. cell21.setBackgroundColor(new Color(251, 221, 173));
    300. }
    301. table.addCell(cell21);
    302. //第22个cell
    303. Cell cell22 = new Cell(new Phrase("中"));
    304. cell22.setVerticalAlignment(cell.ALIGN_MIDDLE);
    305. cell22.setHorizontalAlignment(Element.ALIGN_CENTER);
    306. if(controlMeassure.equals("一般")&&risk.equals("中风险")){
    307. cell22.setBackgroundColor(new Color(251, 221, 173));
    308. }
    309. table.addCell(cell22);
    310. //第23个cell
    311. Cell cell23 = new Cell(new Phrase("中高"));
    312. cell23.setVerticalAlignment(cell.ALIGN_MIDDLE);
    313. cell23.setHorizontalAlignment(Element.ALIGN_CENTER);
    314. if(controlMeassure.equals("不充分")&&risk.equals("中风险")){
    315. cell23.setBackgroundColor(new Color(251, 221, 173));
    316. }
    317. table.addCell(cell23);
    318. //第24个cell
    319. Cell cell24 = new Cell(new Phrase("中高"));
    320. cell24.setVerticalAlignment(cell.ALIGN_MIDDLE);
    321. cell24.setHorizontalAlignment(Element.ALIGN_CENTER);
    322. if(controlMeassure.equals("重大缺陷")&&risk.equals("中风险")){
    323. cell24.setBackgroundColor(new Color(251, 221, 173));
    324. }
    325. table.addCell(cell24);
    326. //第5行
    327. //第25个cell
    328. Cell cell25 = new Cell(new Phrase("较低风险"));
    329. cell25.setVerticalAlignment(cell.ALIGN_MIDDLE);
    330. cell25.setHorizontalAlignment(Element.ALIGN_CENTER);
    331. cell25.setBackgroundColor(new Color(217, 241, 250));
    332. table.addCell(cell25);
    333. //第26个cell
    334. Cell cell26 = new Cell(new Phrase("中低"));
    335. cell26.setVerticalAlignment(cell.ALIGN_MIDDLE);
    336. cell26.setHorizontalAlignment(Element.ALIGN_CENTER);
    337. if(controlMeassure.equals("强健")&&risk.equals("较低风险")){
    338. cell26.setBackgroundColor(new Color(251, 221, 173));
    339. }
    340. table.addCell(cell26);
    341. //第27个cell
    342. Cell cell27 = new Cell(new Phrase("中低"));
    343. cell27.setVerticalAlignment(cell.ALIGN_MIDDLE);
    344. cell27.setHorizontalAlignment(Element.ALIGN_CENTER);
    345. if(controlMeassure.equals("满意")&&risk.equals("较低风险")){
    346. cell27.setBackgroundColor(new Color(251, 221, 173));
    347. }
    348. table.addCell(cell27);
    349. //第28个cell
    350. Cell cell28 = new Cell(new Phrase("中"));
    351. cell28.setVerticalAlignment(cell.ALIGN_MIDDLE);
    352. cell28.setHorizontalAlignment(Element.ALIGN_CENTER);
    353. if(controlMeassure.equals("一般")&&risk.equals("较低风险")){
    354. cell28.setBackgroundColor(new Color(251, 221, 173));
    355. }
    356. table.addCell(cell28);
    357. //第29个cell
    358. Cell cell29 = new Cell(new Phrase("中"));
    359. cell29.setVerticalAlignment(cell.ALIGN_MIDDLE);
    360. cell29.setHorizontalAlignment(Element.ALIGN_CENTER);
    361. if(controlMeassure.equals("不充分")&&risk.equals("较低风险")){
    362. cell29.setBackgroundColor(new Color(251, 221, 173));
    363. }
    364. table.addCell(cell29);
    365. //第30个cell
    366. Cell cell30 = new Cell(new Phrase("中高"));
    367. cell30.setVerticalAlignment(cell.ALIGN_MIDDLE);
    368. cell30.setHorizontalAlignment(Element.ALIGN_CENTER);
    369. if(controlMeassure.equals("重大缺陷")&&risk.equals("较低风险")){
    370. cell30.setBackgroundColor(new Color(251, 221, 173));
    371. }
    372. table.addCell(cell30);
    373. //第6行
    374. //第31个cell
    375. Cell cell31 = new Cell(new Phrase("低风险"));
    376. cell31.setVerticalAlignment(cell.ALIGN_MIDDLE);
    377. cell31.setHorizontalAlignment(Element.ALIGN_CENTER);
    378. cell31.setBackgroundColor(new Color(217, 241, 250));
    379. table.addCell(cell31);
    380. //第32个cell
    381. Cell cell32 = new Cell(new Phrase("低"));
    382. cell32.setVerticalAlignment(cell.ALIGN_MIDDLE);
    383. cell32.setHorizontalAlignment(Element.ALIGN_CENTER);
    384. if(controlMeassure.equals("强健")&&risk.equals("低风险")){
    385. cell32.setBackgroundColor(new Color(251, 221, 173));
    386. }
    387. table.addCell(cell32);
    388. //第33个cell
    389. Cell cell33 = new Cell(new Phrase("中低"));
    390. cell33.setVerticalAlignment(cell.ALIGN_MIDDLE);
    391. cell33.setHorizontalAlignment(Element.ALIGN_CENTER);
    392. if(controlMeassure.equals("满意")&&risk.equals("低风险")){
    393. cell33.setBackgroundColor(new Color(251, 221, 173));
    394. }
    395. table.addCell(cell33);
    396. //第34个cell
    397. Cell cell34 = new Cell(new Phrase("中低"));
    398. cell34.setVerticalAlignment(cell.ALIGN_MIDDLE);
    399. cell34.setHorizontalAlignment(Element.ALIGN_CENTER);
    400. if(controlMeassure.equals("一般")&&risk.equals("低风险")){
    401. cell34.setBackgroundColor(new Color(251, 221, 173));
    402. }
    403. table.addCell(cell34);
    404. //第35个cell
    405. Cell cell35 = new Cell(new Phrase("中"));
    406. cell35.setVerticalAlignment(cell.ALIGN_MIDDLE);
    407. cell35.setHorizontalAlignment(Element.ALIGN_CENTER);
    408. if(controlMeassure.equals("不充分")&&risk.equals("低风险")){
    409. cell35.setBackgroundColor(new Color(251, 221, 173));
    410. }
    411. table.addCell(cell35);
    412. //第36个cell
    413. Cell cell36 = new Cell(new Phrase("中"));
    414. cell36.setVerticalAlignment(cell.ALIGN_MIDDLE);
    415. cell36.setHorizontalAlignment(Element.ALIGN_CENTER);
    416. if(controlMeassure.equals("重大缺陷")&&risk.equals("低风险")){
    417. cell36.setBackgroundColor(new Color(251, 221, 173));
    418. }
    419. table.addCell(cell36);
    420. return table;
    421. }
    422. //合并列totalRow 总行;totalCol 总列;
    423. public Table insertColSpanTable(TempletResult templetResult) throws DocumentException {
    424. //初始化table
    425. Table table = new Table(templetResult.getColNum());
    426. int[] width = new int[templetResult.getColNum()];
    427. table.setWidths(width);
    428. table.setWidth(100.0F);
    429. table.setAutoFillEmptyCells(true);
    430. table.setAlignment(1);
    431. table.setAlignment(5);
    432. table.setBorder(1000);
    433. table.setBorderWidth(1.0F);
    434. //第一行开始
    435. //第1个cell
    436. List<WordInfo> result = templetResult.getBeanResult();
    437. for (WordInfo info:result) {
    438. Cell cell = new Cell(new Phrase(info.getInfo()));
    439. cell.setRowspan(info.getColNum());
    440. table.addCell(cell);
    441. }
    442. return table;
    443. }
    444. }

    WordUtils:

    1. package myTest;
    2. import com.lowagie.text.Cell;
    3. import com.lowagie.text.Document;
    4. import com.lowagie.text.DocumentException;
    5. import com.lowagie.text.Font;
    6. import com.lowagie.text.Image;
    7. import com.lowagie.text.PageSize;
    8. import com.lowagie.text.Paragraph;
    9. import com.lowagie.text.Table;
    10. import com.lowagie.text.pdf.BaseFont;
    11. import com.lowagie.text.rtf.RtfWriter2;
    12. import com.lowagie.text.rtf.style.RtfParagraphStyle;
    13. import java.awt.Color;
    14. import java.io.FileOutputStream;
    15. import java.io.IOException;
    16. import java.net.MalformedURLException;
    17. public class WordUtils {
    18. private Document document;
    19. private BaseFont bfChinese;
    20. public Document getDocument() {
    21. return this.document;
    22. }
    23. public WordUtils() {
    24. this.document = new Document(PageSize.A4);
    25. }
    26. public void openDocument(String filePath) throws DocumentException, IOException {
    27. RtfWriter2.getInstance(this.document, new FileOutputStream(filePath));
    28. this.document.open();
    29. }
    30. public void insertTitle(String titleStr, int fontsize, int fontStyle, int elementAlign) throws DocumentException {
    31. Font titleFont = new Font(this.bfChinese, (float)fontsize, fontStyle);
    32. Paragraph title = new Paragraph(titleStr);
    33. title.setAlignment(elementAlign);
    34. title.setFont(titleFont);
    35. this.document.add(title);
    36. }
    37. public void insertTitlePattern(String titleStr, RtfParagraphStyle rtfParagraphStyle) throws DocumentException {
    38. Paragraph title = new Paragraph(titleStr);
    39. title.setFont(rtfParagraphStyle);
    40. title.setSpacingBefore(50);
    41. title.setSpacingAfter(50);
    42. this.document.add(title);
    43. }
    44. public void insertTitlePatternSecond(String titleStr, RtfParagraphStyle rtfParagraphStyle) throws DocumentException {
    45. Paragraph title = new Paragraph(titleStr);
    46. title.setFont(rtfParagraphStyle);
    47. this.document.add(title);
    48. }
    49. public void insertContext(String contextStr, int fontsize, int fontStyle, int elementAlign) throws DocumentException {
    50. Font contextFont = new Font(this.bfChinese, (float)fontsize, fontStyle);
    51. Paragraph context = new Paragraph(contextStr);
    52. context.setLeading(10.0F);
    53. context.setFont(contextFont);
    54. context.setSpacingBefore(1.0F);
    55. context.setFirstLineIndent(20.0F);
    56. this.document.add(context);
    57. }
    58. public void closeDocument() throws DocumentException {
    59. this.document.close();
    60. }
    61. }

    导出的效果:
    image.png