直接运行就行:
package myTest;import com.lowagie.text.*;import com.lowagie.text.rtf.style.RtfParagraphStyle;import dto.TempletResult;import dto.WordInfo;import java.awt.*;import java.io.IOException;import java.util.List;import java.util.List;public class WordDemo {public WordDemo() {}//原始的评估报告模板public static void excelInfo1()throws IOException, DocumentException {WordUtils wordUtils = new WordUtils();wordUtils.openDocument("D:\\机构自评估模板"+System.currentTimeMillis()+".doc");RtfParagraphStyle rtfGsBt1 = RtfParagraphStyle.STYLE_HEADING_1;rtfGsBt1.setAlignment(0);rtfGsBt1.setStyle(1);rtfGsBt1.setSize(15.0F);RtfParagraphStyle rtfGsBt2 = RtfParagraphStyle.STYLE_HEADING_2;rtfGsBt2.setAlignment(0);rtfGsBt2.setStyle(1);rtfGsBt2.setSize(13.0F);wordUtils.insertTitle("山东高速支付集团2020年风险评估报告", 13, 1, 1);wordUtils.insertTitle("评估周期:2020年1月1日-2020年12月31日", 8, 1, 1);wordUtils.insertTitlePattern("一、基本情况", rtfGsBt1);//wordUtils.insertTitlePatternSecond("1.1 第一小节内容", rtfGsBt2);wordUtils.insertContext("本次风险评估,风险指标156个,其中地域维度23个,客户维度45个,产品业务维度58个,渠道维度30个。控制措施指标67个,其中地域维度" +"10个,客户维度23个,产品业务维度20个,渠道维度14个", 12, 0, 1);wordUtils.insertTitlePattern("二、评估结果分析:", rtfGsBt1);wordUtils.insertTitlePatternSecond("(一)机构风险评估剩余风险", rtfGsBt2);wordUtils.getDocument().add(new Phrase(""));//调用插入表格wordUtils.getDocument().add((new WordDemo()).insertComplexTable("满意","高风险"));wordUtils.insertContext("\n",12,0,1);wordUtils.insertContext("本次风险评估:剩余风险为中高,固有风险评估结果为高风险、控制措施有效性评估结果为满意。\n", 12, 0, 1);wordUtils.insertContext("固有风险结果分析:地域维度风险较高的因素有风险因素1、风险因素2、风险" +"因素3;客户维度风险较高的因素有风险因素1、风险因素2、风险因素3;产品维度风险较高的因素有风险因素" +"1、风险因素2、风险因素3;渠道维度风险较高的因素有风险因素1、风险因素2、风险因素3;(备注:取固有" +"风险指标得分为最高分的风险因素)\n", 12, 0, 1);wordUtils.insertContext("控制措施有效性结果分析:地域维度控制措施有效性缺陷的措施有措施1、措施" +"2、措施3;客户维度控制措施有效性缺陷的措施有措施1、措施2、措施3;产品维度控制措施有效性缺陷的措施" +"有措施1、措施2、措施3;渠道维度控制措施有效性缺陷的措施有措施1、措施2、措施3;(备注:取控制措施" +"指标得分为最低分的控制措施)\n", 12, 0, 1);wordUtils.insertTitlePatternSecond("(二)各维度剩余风险较高的风险因素", rtfGsBt2);//调用插入表格wordUtils.getDocument().add((new WordDemo()).insertParameterTable());wordUtils.insertContext("\n",12,0,1);wordUtils.insertTitlePatternSecond("(三)地域维度", rtfGsBt2);wordUtils.insertContext("地域维度评估结果明细\n",12,0,1);wordUtils.insertTitlePatternSecond("(四)客户维度", rtfGsBt2);wordUtils.insertContext("\n",12,0,1);wordUtils.insertTitlePatternSecond("(五)产品维度", rtfGsBt2);wordUtils.insertContext("\n",12,0,1);wordUtils.insertTitlePatternSecond("(六)渠道维度", rtfGsBt2);wordUtils.insertContext("\n",12,0,1);wordUtils.insertTitlePatternSecond("(七)总结", rtfGsBt2);wordUtils.closeDocument();}public static void main(String[] args) throws IOException, DocumentException {excelInfo1();}public Table insertParameterTable() throws DocumentException {//初始化tableTable table = new Table(3);int[] width = new int[]{20, 20, 20};table.setWidths(width);table.setWidth(100.0F);table.setAutoFillEmptyCells(true);table.setAlignment(1);table.setAlignment(5);table.setBorder(1000);table.setBorderWidth(1.0F);//第一行开始//第1个cellCell cell = new Cell(new Phrase("维度"));cell.setVerticalAlignment(cell.ALIGN_MIDDLE);cell.setHorizontalAlignment(Element.ALIGN_CENTER);table.addCell(cell);Cell cell2 = new Cell(new Phrase("高风险因素"));cell2.setVerticalAlignment(cell.ALIGN_MIDDLE);cell2.setHorizontalAlignment(Element.ALIGN_CENTER);table.addCell(cell2);Cell cell3 = new Cell(new Phrase("控制措施缺陷点"));cell3.setVerticalAlignment(cell.ALIGN_MIDDLE);cell3.setHorizontalAlignment(Element.ALIGN_CENTER);table.addCell(cell3);Cell cell4 = new Cell(new Phrase("地域"));cell4.setVerticalAlignment(cell.ALIGN_MIDDLE);cell4.setHorizontalAlignment(Element.ALIGN_CENTER);table.addCell(cell4);Cell cell5 = new Cell(new Phrase("固有风险指标得分为最高分的风险因素"));cell5.setVerticalAlignment(cell.ALIGN_MIDDLE);cell5.setHorizontalAlignment(Element.ALIGN_CENTER);table.addCell(cell5);Cell cell6 = new Cell(new Phrase("控制措施指标得分为最低分的控制措施"));cell6.setVerticalAlignment(cell.ALIGN_MIDDLE);cell6.setHorizontalAlignment(Element.ALIGN_CENTER);table.addCell(cell6);Cell cell7 = new Cell(new Phrase("客户"));cell7.setVerticalAlignment(cell.ALIGN_MIDDLE);cell7.setHorizontalAlignment(Element.ALIGN_CENTER);table.addCell(cell7);Cell cell8 = new Cell(new Phrase(""));cell8.setVerticalAlignment(cell.ALIGN_MIDDLE);cell8.setHorizontalAlignment(Element.ALIGN_CENTER);table.addCell(cell8);Cell cell9 = new Cell(new Phrase(""));cell9.setVerticalAlignment(cell.ALIGN_MIDDLE);cell9.setHorizontalAlignment(Element.ALIGN_CENTER);table.addCell(cell9);Cell cell10 = new Cell(new Phrase("产品业务"));cell10.setVerticalAlignment(cell.ALIGN_MIDDLE);cell10.setHorizontalAlignment(Element.ALIGN_CENTER);table.addCell(cell10);Cell cell11 = new Cell(new Phrase(""));cell11.setVerticalAlignment(cell.ALIGN_MIDDLE);cell11.setHorizontalAlignment(Element.ALIGN_CENTER);table.addCell(cell11);Cell cell12 = new Cell(new Phrase(""));cell12.setVerticalAlignment(cell.ALIGN_MIDDLE);cell12.setHorizontalAlignment(Element.ALIGN_CENTER);table.addCell(cell12);Cell cell13 = new Cell(new Phrase("渠道"));cell13.setVerticalAlignment(cell.ALIGN_MIDDLE);cell13.setHorizontalAlignment(Element.ALIGN_CENTER);table.addCell(cell13);Cell cell14 = new Cell(new Phrase(""));cell14.setVerticalAlignment(cell.ALIGN_MIDDLE);cell14.setHorizontalAlignment(Element.ALIGN_CENTER);table.addCell(cell14);Cell cell15 = new Cell(new Phrase(""));cell15.setVerticalAlignment(cell.ALIGN_MIDDLE);cell15.setHorizontalAlignment(Element.ALIGN_CENTER);table.addCell(cell15);return table;}public Table insertComplexTable(String controlMeassure,String risk) throws DocumentException {//初始化tableTable table = new Table(6);int[] width = new int[]{20, 20, 20, 20, 20,20};table.setWidths(width);table.setWidth(100.0F);table.setAutoFillEmptyCells(true);table.setAlignment(1);table.setAlignment(5);table.setBorder(1000);table.setBorderWidth(1.0F);//第一行开始//第1个cellCell cell = new Cell(new Phrase("控制措施有效性/固有风险"));cell.setBackgroundColor(new Color(217, 241, 250));table.addCell(cell);//第2个cellCell cell2 = new Cell(new Phrase("强健"));cell2.setVerticalAlignment(cell.ALIGN_MIDDLE);cell2.setHorizontalAlignment(Element.ALIGN_CENTER);cell2.setBackgroundColor(new Color(217, 241, 250));table.addCell(cell2);//第3个cellCell cell3 = new Cell(new Phrase("满意"));cell3.setVerticalAlignment(cell.ALIGN_MIDDLE);cell3.setHorizontalAlignment(Element.ALIGN_CENTER);cell3.setBackgroundColor(new Color(217, 241, 250));table.addCell(cell3);//第4个cellCell cell4 = new Cell(new Phrase("一般"));cell4.setVerticalAlignment(cell.ALIGN_MIDDLE);cell4.setHorizontalAlignment(Element.ALIGN_CENTER);cell4.setBackgroundColor(new Color(217, 241, 250));table.addCell(cell4);//第5个cellCell cell5 = new Cell(new Phrase("不充分"));cell5.setVerticalAlignment(cell.ALIGN_MIDDLE);cell5.setHorizontalAlignment(Element.ALIGN_CENTER);cell5.setBackgroundColor(new Color(217, 241, 250));table.addCell(cell5);//第6个cellCell cell6 = new Cell(new Phrase("重大缺陷"));cell6.setVerticalAlignment(cell.ALIGN_MIDDLE);cell6.setHorizontalAlignment(Element.ALIGN_CENTER);cell6.setBackgroundColor(new Color(217, 241, 250));table.addCell(cell6);//第二行开始//第7个cellCell cell7 = new Cell(new Phrase("高风险"));cell7.setVerticalAlignment(cell.ALIGN_MIDDLE);cell7.setHorizontalAlignment(Element.ALIGN_CENTER);cell7.setBackgroundColor(new Color(217, 241, 250));table.addCell(cell7);//第8个cellCell cell8 = new Cell(new Phrase("中"));cell8.setVerticalAlignment(cell.ALIGN_MIDDLE);cell8.setHorizontalAlignment(Element.ALIGN_CENTER);if(controlMeassure.equals("强健")&&risk.equals("高风险")){cell8.setBackgroundColor(new Color(251, 221, 173));}table.addCell(cell8);//第9个cellCell cell9 = new Cell(new Phrase("中高"));cell9.setVerticalAlignment(cell.ALIGN_MIDDLE);cell9.setHorizontalAlignment(Element.ALIGN_CENTER);if(controlMeassure.equals("满意")&&risk.equals("高风险")){cell9.setBackgroundColor(new Color(251, 221, 173));}table.addCell(cell9);//第10个cellCell cell10 = new Cell(new Phrase("中高"));cell10.setVerticalAlignment(cell.ALIGN_MIDDLE);cell10.setHorizontalAlignment(Element.ALIGN_CENTER);if(controlMeassure.equals("一般")&&risk.equals("高风险")){cell10.setBackgroundColor(new Color(251, 221, 173));}table.addCell(cell10);//第11个cellCell cell11 = new Cell(new Phrase("高"));cell11.setVerticalAlignment(cell.ALIGN_MIDDLE);cell11.setHorizontalAlignment(Element.ALIGN_CENTER);if(controlMeassure.equals("不充分")&&risk.equals("高风险")){cell11.setBackgroundColor(new Color(251, 221, 173));}table.addCell(cell11);//第12个cellCell cell12 = new Cell(new Phrase("高"));cell12.setVerticalAlignment(cell.ALIGN_MIDDLE);cell12.setHorizontalAlignment(Element.ALIGN_CENTER);if(controlMeassure.equals("重大缺陷")&&risk.equals("高风险")){cell12.setBackgroundColor(new Color(251, 221, 173));}table.addCell(cell12);//第3行开始//第13个cellCell cell13 = new Cell(new Phrase("较高风险"));cell13.setVerticalAlignment(cell.ALIGN_MIDDLE);cell13.setHorizontalAlignment(Element.ALIGN_CENTER);cell13.setBackgroundColor(new Color(217, 241, 250));table.addCell(cell13);//第14个cellCell cell14 = new Cell(new Phrase("中"));cell14.setVerticalAlignment(cell.ALIGN_MIDDLE);cell14.setHorizontalAlignment(Element.ALIGN_CENTER);if(controlMeassure.equals("强健")&&risk.equals("较高风险")){cell14.setBackgroundColor(new Color(251, 221, 173));}table.addCell(cell14);//第15个cellCell cell15 = new Cell(new Phrase("中"));cell15.setVerticalAlignment(cell.ALIGN_MIDDLE);cell15.setHorizontalAlignment(Element.ALIGN_CENTER);if(controlMeassure.equals("满意")&&risk.equals("较高风险")){cell15.setBackgroundColor(new Color(251, 221, 173));}table.addCell(cell15);//第16个cellCell cell16 = new Cell(new Phrase("中高"));cell16.setVerticalAlignment(cell.ALIGN_MIDDLE);cell16.setHorizontalAlignment(Element.ALIGN_CENTER);if(controlMeassure.equals("一般")&&risk.equals("较高风险")){cell16.setBackgroundColor(new Color(251, 221, 173));}table.addCell(cell16);//第17个cellCell cell17 = new Cell(new Phrase("中高"));cell17.setVerticalAlignment(cell.ALIGN_MIDDLE);cell17.setHorizontalAlignment(Element.ALIGN_CENTER);if(controlMeassure.equals("不充分")&&risk.equals("较高风险")){cell17.setBackgroundColor(new Color(251, 221, 173));}table.addCell(cell17);//第18个cellCell cell18 = new Cell(new Phrase("高"));cell18.setVerticalAlignment(cell.ALIGN_MIDDLE);cell18.setHorizontalAlignment(Element.ALIGN_CENTER);if(controlMeassure.equals("重大缺陷")&&risk.equals("较高风险")){cell18.setBackgroundColor(new Color(251, 221, 173));}table.addCell(cell18);//第4行//第19个cellCell cell19 = new Cell(new Phrase("中风险"));cell19.setVerticalAlignment(cell.ALIGN_MIDDLE);cell19.setHorizontalAlignment(Element.ALIGN_CENTER);cell19.setBackgroundColor(new Color(217, 241, 250));table.addCell(cell19);//第20个cellCell cell20 = new Cell(new Phrase("中低"));cell20.setVerticalAlignment(cell.ALIGN_MIDDLE);cell20.setHorizontalAlignment(Element.ALIGN_CENTER);if(controlMeassure.equals("强健")&&risk.equals("中风险")){cell20.setBackgroundColor(new Color(251, 221, 173));}table.addCell(cell20);//第21个cellCell cell21 = new Cell(new Phrase("中"));cell21.setVerticalAlignment(cell.ALIGN_MIDDLE);cell21.setHorizontalAlignment(Element.ALIGN_CENTER);if(controlMeassure.equals("满意")&&risk.equals("中风险")){cell21.setBackgroundColor(new Color(251, 221, 173));}table.addCell(cell21);//第22个cellCell cell22 = new Cell(new Phrase("中"));cell22.setVerticalAlignment(cell.ALIGN_MIDDLE);cell22.setHorizontalAlignment(Element.ALIGN_CENTER);if(controlMeassure.equals("一般")&&risk.equals("中风险")){cell22.setBackgroundColor(new Color(251, 221, 173));}table.addCell(cell22);//第23个cellCell cell23 = new Cell(new Phrase("中高"));cell23.setVerticalAlignment(cell.ALIGN_MIDDLE);cell23.setHorizontalAlignment(Element.ALIGN_CENTER);if(controlMeassure.equals("不充分")&&risk.equals("中风险")){cell23.setBackgroundColor(new Color(251, 221, 173));}table.addCell(cell23);//第24个cellCell cell24 = new Cell(new Phrase("中高"));cell24.setVerticalAlignment(cell.ALIGN_MIDDLE);cell24.setHorizontalAlignment(Element.ALIGN_CENTER);if(controlMeassure.equals("重大缺陷")&&risk.equals("中风险")){cell24.setBackgroundColor(new Color(251, 221, 173));}table.addCell(cell24);//第5行//第25个cellCell cell25 = new Cell(new Phrase("较低风险"));cell25.setVerticalAlignment(cell.ALIGN_MIDDLE);cell25.setHorizontalAlignment(Element.ALIGN_CENTER);cell25.setBackgroundColor(new Color(217, 241, 250));table.addCell(cell25);//第26个cellCell cell26 = new Cell(new Phrase("中低"));cell26.setVerticalAlignment(cell.ALIGN_MIDDLE);cell26.setHorizontalAlignment(Element.ALIGN_CENTER);if(controlMeassure.equals("强健")&&risk.equals("较低风险")){cell26.setBackgroundColor(new Color(251, 221, 173));}table.addCell(cell26);//第27个cellCell cell27 = new Cell(new Phrase("中低"));cell27.setVerticalAlignment(cell.ALIGN_MIDDLE);cell27.setHorizontalAlignment(Element.ALIGN_CENTER);if(controlMeassure.equals("满意")&&risk.equals("较低风险")){cell27.setBackgroundColor(new Color(251, 221, 173));}table.addCell(cell27);//第28个cellCell cell28 = new Cell(new Phrase("中"));cell28.setVerticalAlignment(cell.ALIGN_MIDDLE);cell28.setHorizontalAlignment(Element.ALIGN_CENTER);if(controlMeassure.equals("一般")&&risk.equals("较低风险")){cell28.setBackgroundColor(new Color(251, 221, 173));}table.addCell(cell28);//第29个cellCell cell29 = new Cell(new Phrase("中"));cell29.setVerticalAlignment(cell.ALIGN_MIDDLE);cell29.setHorizontalAlignment(Element.ALIGN_CENTER);if(controlMeassure.equals("不充分")&&risk.equals("较低风险")){cell29.setBackgroundColor(new Color(251, 221, 173));}table.addCell(cell29);//第30个cellCell cell30 = new Cell(new Phrase("中高"));cell30.setVerticalAlignment(cell.ALIGN_MIDDLE);cell30.setHorizontalAlignment(Element.ALIGN_CENTER);if(controlMeassure.equals("重大缺陷")&&risk.equals("较低风险")){cell30.setBackgroundColor(new Color(251, 221, 173));}table.addCell(cell30);//第6行//第31个cellCell cell31 = new Cell(new Phrase("低风险"));cell31.setVerticalAlignment(cell.ALIGN_MIDDLE);cell31.setHorizontalAlignment(Element.ALIGN_CENTER);cell31.setBackgroundColor(new Color(217, 241, 250));table.addCell(cell31);//第32个cellCell cell32 = new Cell(new Phrase("低"));cell32.setVerticalAlignment(cell.ALIGN_MIDDLE);cell32.setHorizontalAlignment(Element.ALIGN_CENTER);if(controlMeassure.equals("强健")&&risk.equals("低风险")){cell32.setBackgroundColor(new Color(251, 221, 173));}table.addCell(cell32);//第33个cellCell cell33 = new Cell(new Phrase("中低"));cell33.setVerticalAlignment(cell.ALIGN_MIDDLE);cell33.setHorizontalAlignment(Element.ALIGN_CENTER);if(controlMeassure.equals("满意")&&risk.equals("低风险")){cell33.setBackgroundColor(new Color(251, 221, 173));}table.addCell(cell33);//第34个cellCell cell34 = new Cell(new Phrase("中低"));cell34.setVerticalAlignment(cell.ALIGN_MIDDLE);cell34.setHorizontalAlignment(Element.ALIGN_CENTER);if(controlMeassure.equals("一般")&&risk.equals("低风险")){cell34.setBackgroundColor(new Color(251, 221, 173));}table.addCell(cell34);//第35个cellCell cell35 = new Cell(new Phrase("中"));cell35.setVerticalAlignment(cell.ALIGN_MIDDLE);cell35.setHorizontalAlignment(Element.ALIGN_CENTER);if(controlMeassure.equals("不充分")&&risk.equals("低风险")){cell35.setBackgroundColor(new Color(251, 221, 173));}table.addCell(cell35);//第36个cellCell cell36 = new Cell(new Phrase("中"));cell36.setVerticalAlignment(cell.ALIGN_MIDDLE);cell36.setHorizontalAlignment(Element.ALIGN_CENTER);if(controlMeassure.equals("重大缺陷")&&risk.equals("低风险")){cell36.setBackgroundColor(new Color(251, 221, 173));}table.addCell(cell36);return table;}//合并列totalRow 总行;totalCol 总列;public Table insertColSpanTable(TempletResult templetResult) throws DocumentException {//初始化tableTable table = new Table(templetResult.getColNum());int[] width = new int[templetResult.getColNum()];table.setWidths(width);table.setWidth(100.0F);table.setAutoFillEmptyCells(true);table.setAlignment(1);table.setAlignment(5);table.setBorder(1000);table.setBorderWidth(1.0F);//第一行开始//第1个cellList<WordInfo> result = templetResult.getBeanResult();for (WordInfo info:result) {Cell cell = new Cell(new Phrase(info.getInfo()));cell.setRowspan(info.getColNum());table.addCell(cell);}return table;}}
WordUtils:
package myTest;import com.lowagie.text.Cell;import com.lowagie.text.Document;import com.lowagie.text.DocumentException;import com.lowagie.text.Font;import com.lowagie.text.Image;import com.lowagie.text.PageSize;import com.lowagie.text.Paragraph;import com.lowagie.text.Table;import com.lowagie.text.pdf.BaseFont;import com.lowagie.text.rtf.RtfWriter2;import com.lowagie.text.rtf.style.RtfParagraphStyle;import java.awt.Color;import java.io.FileOutputStream;import java.io.IOException;import java.net.MalformedURLException;public class WordUtils {private Document document;private BaseFont bfChinese;public Document getDocument() {return this.document;}public WordUtils() {this.document = new Document(PageSize.A4);}public void openDocument(String filePath) throws DocumentException, IOException {RtfWriter2.getInstance(this.document, new FileOutputStream(filePath));this.document.open();}public void insertTitle(String titleStr, int fontsize, int fontStyle, int elementAlign) throws DocumentException {Font titleFont = new Font(this.bfChinese, (float)fontsize, fontStyle);Paragraph title = new Paragraph(titleStr);title.setAlignment(elementAlign);title.setFont(titleFont);this.document.add(title);}public void insertTitlePattern(String titleStr, RtfParagraphStyle rtfParagraphStyle) throws DocumentException {Paragraph title = new Paragraph(titleStr);title.setFont(rtfParagraphStyle);title.setSpacingBefore(50);title.setSpacingAfter(50);this.document.add(title);}public void insertTitlePatternSecond(String titleStr, RtfParagraphStyle rtfParagraphStyle) throws DocumentException {Paragraph title = new Paragraph(titleStr);title.setFont(rtfParagraphStyle);this.document.add(title);}public void insertContext(String contextStr, int fontsize, int fontStyle, int elementAlign) throws DocumentException {Font contextFont = new Font(this.bfChinese, (float)fontsize, fontStyle);Paragraph context = new Paragraph(contextStr);context.setLeading(10.0F);context.setFont(contextFont);context.setSpacingBefore(1.0F);context.setFirstLineIndent(20.0F);this.document.add(context);}public void closeDocument() throws DocumentException {this.document.close();}}
导出的效果:
