iText生成word文檔代碼示例
iText生成word文檔代碼示例
iText可以用來方便的生成word文檔,使用itext可以創建word文檔,然后插入段落,插入表格,插入圖片等等!
import java.awt.Color; import java.io.FileOutputStream; import java.io.IOException; import java.net.MalformedURLException;import com.lowagie.text.Cell; import com.lowagie.text.Document; import com.lowagie.text.DocumentException; import com.lowagie.text.Element; import com.lowagie.text.Font; import com.lowagie.text.Image; import com.lowagie.text.PageSize; import com.lowagie.text.Paragraph; import com.lowagie.text.Phrase; import com.lowagie.text.Table; import com.lowagie.text.pdf.BaseFont; import com.lowagie.text.rtf.RtfWriter2;
/**
- 根據itext提供的java類庫,構建word模板,并添加相應的內容,從而導出word報告;平臺不相關
- 需要引入iText-2.1.7.jar;iTextAsian.jar;iText-rtf-2.1.7.jar
@author ryan */ public class WordTemplete { private Document document; private BaseFont bfChinese; public BaseFont getBfChinese() {
return bfChinese;
}
public void setBfChinese(BaseFont bfChinese) {
this.bfChinese = bfChinese;
}
public Document getDocument() {
return document;
}
public void setDocument(Document document) {
this.document = document;
}
public WordTemplete(){
this.document = new Document(PageSize.A4);
} /**
- @param filePath 建立一個書寫器(Writer)與document對象關聯,通過書寫器(Writer)可以將文檔寫入到磁盤中
- @throws DocumentException
@throws IOException */ public void openDocument(String filePath) throws DocumentException, IOException { // 建立一個書寫器(Writer)與document對象關聯,通過書寫器(Writer)可以將文檔寫入到磁盤中 RtfWriter2.getInstance(this.document, new FileOutputStream(filePath)); this.document.open(); // 設置中文字體 this.bfChinese = BaseFont.createFont("STSongStd-Light",
"UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
}
/**
- @param titleStr 標題
- @param fontsize 字體大小
- @param fontStyle 字體樣式
- @param elementAlign 對齊方式
@throws DocumentException */ public void insertTitle(String titleStr,int fontsize,int fontStyle,int elementAlign) throws DocumentException{ Font titleFont = new Font(this.bfChinese, fontsize, fontStyle); Paragraph title = new Paragraph(titleStr); // 設置標題格式對齊方式 title.setAlignment(elementAlign); title.setFont(titleFont);
this.document.add(title); }
/**
- @param contextStr 內容
- @param fontsize 字體大小
- @param fontStyle 字體樣式
- @param elementAlign 對齊方式
- @throws DocumentException / public void insertContext(String contextStr,int fontsize,int fontStyle,int elementAlign) throws DocumentException{ // 正文字體風格 Font contextFont = new Font(bfChinese, fontsize, fontStyle); Paragraph context = new Paragraph(contextStr); //設置行距 context.setLeading(30f); // 正文格式左對齊 context.setAlignment(elementAlign); context.setFont(contextFont); // 離上一段落(標題)空的行數 context.setSpacingBefore(5); // 設置第一行空的列數 context.setFirstLineIndent(20); document.add(context); } /
- 測試清單
*/ public void insertRiskTable() throws DocumentException{ Table aTable = new Table(6,3); int width[] = { 10, 40, 17, 13, 10, 10 }; aTable.setWidths(width);// 設置每列所占比例 aTable.setWidth(100); // 占頁面寬度 90% aTable.setAlignment(Element.ALIGN_CENTER);// 居中顯示 aTable.setAlignment(Element.ALIGN_MIDDLE);// 縱向居中顯示 aTable.setAutoFillEmptyCells(true); // 自動填滿 aTable.setBorderWidth(0); // 邊框寬度 aTable.setBorderColor(new Color(0, 125, 255)); // 邊框顏色 aTable.setPadding(2);// 襯距,看效果就知道什么意思了 aTable.setSpacing(3);// 即單元格之間的間距 aTable.setBorder(2);// 邊框
Font fontChinese = new Font(bfChinese, 10, Font.BOLD); Cell cell = new Cell(new Phrase("\n測試代碼\n", fontChinese)); cell.setVerticalAlignment(Element.ALIGN_CENTER); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setBorderColor(new Color(0, 0, 0)); cell.setBackgroundColor(new Color(153, 204, 255)); aTable.addCell(cell);
Cell cell1 = new Cell(new Phrase("測試名稱", fontChinese)); cell1.setVerticalAlignment(Element.ALIGN_CENTER); cell1.setHorizontalAlignment(Element.ALIGN_CENTER); cell1.setBorderColor(new Color(0, 0, 0)); cell1.setBackgroundColor(new Color(153, 204, 255)); aTable.addCell(cell1);
Cell cell2 = new Cell(new Phrase("測試發生可能性", fontChinese)); cell2.setVerticalAlignment(Element.ALIGN_CENTER); cell2.setHorizontalAlignment(Element.ALIGN_CENTER); cell2.setBorderColor(new Color(0, 0, 0)); cell2.setBackgroundColor(new Color(255, 255, 0)); aTable.addCell(cell2);
Cell cell3 = new Cell(new Phrase("測試損失度", fontChinese)); cell3.setVerticalAlignment(Element.ALIGN_CENTER); cell3.setHorizontalAlignment(Element.ALIGN_CENTER); cell3.setBorderColor(new Color(0, 0, 0)); cell3.setBackgroundColor(new Color(255, 255, 0)); aTable.addCell(cell3);
Cell cell4 = new Cell(new Phrase("測試水平", fontChinese)); cell4.setVerticalAlignment(Element.ALIGN_CENTER); cell4.setHorizontalAlignment(Element.ALIGN_CENTER); cell4.setBorderColor(new Color(0, 0, 0)); cell4.setBackgroundColor(new Color(255, 255, 0)); aTable.addCell(cell4);
Cell cell5 = new Cell(new Phrase("測試等級", fontChinese)); cell5.setVerticalAlignment(Element.ALIGN_CENTER); cell5.setHorizontalAlignment(Element.ALIGN_CENTER); cell5.setBorderColor(new Color(0, 0, 0)); cell5.setBackgroundColor(new Color(255, 255, 0)); aTable.addCell(cell5);
for(int i=0;i<12;i++){
aTable.addCell(new Cell(i+""));
}
document.add(aTable); document.add(new Paragraph("\n"));
} /*- 現狀評估
*/ public void insertRiskEvaluationTable() throws DocumentException{ Table aTable = new Table(12,4); int width1[] = { 5, 20, 5, 20, 5, 5, 5, 5, 5, 5, 5, 5}; aTable.setWidths(width1);// 設置每列所占比例 aTable.setWidth(100); // 占頁面寬度 90% aTable.setAlignment(Element.ALIGN_CENTER);// 居中顯示 aTable.setAlignment(Element.ALIGN_MIDDLE);// 縱向居中顯示 aTable.setAutoFillEmptyCells(true); // 自動填滿 aTable.setBorderWidth(0); // 邊框寬度 aTable.setBorderColor(new Color(0, 125, 255)); // 邊框顏色
Font fontChinese = new Font(bfChinese, 10, Font.BOLD); Cell cell = new Cell(new Phrase("\n測試代碼\n", fontChinese)); cell.setVerticalAlignment(Element.ALIGN_CENTER); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setRowspan(2); cell.setBorderColor(new Color(0, 0, 0)); cell.setBackgroundColor(new Color(153, 204, 255)); aTable.addCell(cell);
Cell cell2 = new Cell(new Phrase("測試名稱", fontChinese)); cell2.setVerticalAlignment(Element.ALIGN_CENTER); cell2.setHorizontalAlignment(Element.ALIGN_CENTER); cell2.setRowspan(2); cell2.setBorderColor(new Color(0, 0, 0)); cell2.setBackgroundColor(new Color(153, 204, 255)); aTable.addCell(cell2);
Cell cell3 = new Cell(new Phrase("行為代碼", fontChinese)); cell3.setVerticalAlignment(Element.ALIGN_CENTER); cell3.setHorizontalAlignment(Element.ALIGN_CENTER); cell3.setRowspan(2); cell3.setBorderColor(new Color(0, 0, 0)); cell3.setBackgroundColor(new Color(153, 204, 255)); aTable.addCell(cell3);
Cell cell4 = new Cell(new Phrase("引發測試的行為", fontChinese)); cell4.setVerticalAlignment(Element.ALIGN_CENTER); cell4.setHorizontalAlignment(Element.ALIGN_CENTER); cell4.setRowspan(2); cell4.setBorderColor(new Color(0, 0, 0)); cell4.setBackgroundColor(new Color(153, 204, 255)); aTable.addCell(cell4);
Cell cell5 = new Cell(new Phrase("控制現狀", fontChinese)); cell5.setVerticalAlignment(Element.ALIGN_CENTER); cell5.setHorizontalAlignment(Element.ALIGN_CENTER); cell5.setColspan(8); cell5.setBorderColor(new Color(0, 0, 0)); cell5.setBackgroundColor(new Color(204, 255, 255)); aTable.addCell(cell5);
Cell cell6 = new Cell(new Phrase("部門內審查", fontChinese)); cell6.setVerticalAlignment(Element.ALIGN_CENTER); cell6.setHorizontalAlignment(Element.ALIGN_CENTER); cell6.setBorderColor(new Color(0, 0, 0)); cell6.setBackgroundColor(new Color(204, 255, 255)); aTable.addCell(cell6);
Cell cell7 = new Cell(new Phrase("測試意識", fontChinese)); cell7.setVerticalAlignment(Element.ALIGN_CENTER); cell7.setHorizontalAlignment(Element.ALIGN_CENTER); cell7.setBorderColor(new Color(0, 0, 0)); cell7.setBackgroundColor(new Color(204, 255, 255)); aTable.addCell(cell7);
Cell cell8 = new Cell(new Phrase("過程監控", fontChinese)); cell8.setVerticalAlignment(Element.ALIGN_CENTER); cell8.setHorizontalAlignment(Element.ALIGN_CENTER); cell8.setBorderColor(new Color(0, 0, 0)); cell8.setBackgroundColor(new Color(204, 255, 255)); aTable.addCell(cell8);
Cell cell9 = new Cell(new Phrase("獎懲機制", fontChinese)); cell9.setVerticalAlignment(Element.ALIGN_CENTER); cell9.setHorizontalAlignment(Element.ALIGN_CENTER); cell9.setBorderColor(new Color(0, 0, 0)); cell9.setBackgroundColor(new Color(204, 255, 255)); aTable.addCell(cell9);
Cell cell10 = new Cell(new Phrase("明確責權", fontChinese)); cell10.setVerticalAlignment(Element.ALIGN_CENTER); cell10.setHorizontalAlignment(Element.ALIGN_CENTER); cell10.setBorderColor(new Color(0, 0, 0)); cell10.setBackgroundColor(new Color(204, 255, 255)); aTable.addCell(cell10);
Cell cell11 = new Cell(new Phrase("執行者能力要求", fontChinese)); cell11.setVerticalAlignment(Element.ALIGN_CENTER); cell11.setHorizontalAlignment(Element.ALIGN_CENTER); cell11.setBorderColor(new Color(0, 0, 0)); cell11.setBackgroundColor(new Color(204, 255, 255)); aTable.addCell(cell11);
Cell cell12 = new Cell(new Phrase("專業審查", fontChinese)); cell12.setVerticalAlignment(Element.ALIGN_CENTER); cell12.setHorizontalAlignment(Element.ALIGN_CENTER); cell12.setBorderColor(new Color(0, 0, 0)); cell12.setBackgroundColor(new Color(204, 255, 255)); aTable.addCell(cell12);
Cell cell13 = new Cell(new Phrase("資源配置", fontChinese)); cell13.setVerticalAlignment(Element.ALIGN_CENTER); cell13.setHorizontalAlignment(Element.ALIGN_CENTER); cell13.setBorderColor(new Color(0, 0, 0)); cell13.setBackgroundColor(new Color(204, 255, 255)); aTable.addCell(cell13);
for(int i=0;i<24;i++){
aTable.addCell(new Cell(i+""));
}
document.add(aTable); document.add(new Paragraph("\n")); }
/*
- 測試控制清單
*/ public void insertRiskControlTable() throws DocumentException{ Table aTable = new Table(11,3); int width[] = { 5, 13, 5, 9, 9, 13, 9, 9, 9, 9, 9 }; aTable.setWidths(width);// 設置每列所占比例 aTable.setWidth(100); // 占頁面寬度 90% aTable.setAlignment(Element.ALIGN_CENTER);// 居中顯示 aTable.setAlignment(Element.ALIGN_MIDDLE);// 縱向居中顯示 aTable.setAutoFillEmptyCells(true); // 自動填滿 aTable.setBorderWidth(0); // 邊框寬度 aTable.setBorderColor(new Color(0, 125, 255)); // 邊框顏色
Font fontChinese = new Font(bfChinese, 10, Font.BOLD); Cell cell = new Cell(new Phrase("\n測試代碼\n", fontChinese)); cell.setVerticalAlignment(Element.ALIGN_CENTER); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setBorderColor(new Color(0, 0, 0)); cell.setBackgroundColor(new Color(204, 153, 255)); aTable.addCell(cell);
Cell cell1 = new Cell(new Phrase("測試名稱", fontChinese)); cell1.setVerticalAlignment(Element.ALIGN_CENTER); cell1.setHorizontalAlignment(Element.ALIGN_CENTER); cell1.setBorderColor(new Color(0, 0, 0)); cell1.setBackgroundColor(new Color(204, 153, 255)); aTable.addCell(cell1);
Cell cell2 = new Cell(new Phrase("行為代碼", fontChinese)); cell2.setVerticalAlignment(Element.ALIGN_CENTER); cell2.setHorizontalAlignment(Element.ALIGN_CENTER); cell2.setBorderColor(new Color(0, 0, 0)); cell2.setBackgroundColor(new Color(204, 153, 255)); aTable.addCell(cell2);
Cell cell3 = new Cell(new Phrase("引發測試的行為", fontChinese)); cell3.setVerticalAlignment(Element.ALIGN_CENTER); cell3.setBorderColor(new Color(0, 0, 0)); cell3.setBackgroundColor(new Color(204, 153, 255)); aTable.addCell(cell3);
Cell cell4 = new Cell(new Phrase("測試控制態度", fontChinese)); cell4.setVerticalAlignment(Element.ALIGN_CENTER); cell4.setHorizontalAlignment(Element.ALIGN_CENTER); cell4.setBorderColor(new Color(0, 0, 0)); cell4.setBackgroundColor(new Color(204, 153, 255)); aTable.addCell(cell4);
Cell cell5 = new Cell(new Phrase("控制措施", fontChinese)); cell5.setVerticalAlignment(Element.ALIGN_CENTER); cell5.setHorizontalAlignment(Element.ALIGN_CENTER); cell5.setBorderColor(new Color(0, 0, 0)); cell5.setBackgroundColor(new Color(204, 153, 255)); aTable.addCell(cell5);
Cell cell6 = new Cell(new Phrase("措施類型", fontChinese)); cell6.setVerticalAlignment(Element.ALIGN_CENTER); cell6.setHorizontalAlignment(Element.ALIGN_CENTER); cell6.setBorderColor(new Color(0, 0, 0)); cell6.setBackgroundColor(new Color(204, 153, 255)); aTable.addCell(cell6);
Cell cell7 = new Cell(new Phrase("完成標志", fontChinese)); cell7.setVerticalAlignment(Element.ALIGN_CENTER); cell7.setHorizontalAlignment(Element.ALIGN_CENTER); cell7.setBorderColor(new Color(0, 0, 0)); cell7.setBackgroundColor(new Color(204, 153, 255)); aTable.addCell(cell7);
Cell cell8 = new Cell(new Phrase("控制措施完成時間", fontChinese)); cell8.setVerticalAlignment(Element.ALIGN_CENTER); cell8.setHorizontalAlignment(Element.ALIGN_CENTER); cell8.setBorderColor(new Color(0, 0, 0)); cell8.setBackgroundColor(new Color(204, 153, 255)); aTable.addCell(cell8);
Cell cell9 = new Cell(new Phrase("控制措施牽頭部門", fontChinese)); cell9.setVerticalAlignment(Element.ALIGN_CENTER); cell9.setHorizontalAlignment(Element.ALIGN_CENTER); cell9.setBorderColor(new Color(0, 0, 0)); cell9.setBackgroundColor(new Color(204, 153, 255)); aTable.addCell(cell9);
Cell cell10 = new Cell(new Phrase("控制措施配合部門", fontChinese)); cell10.setVerticalAlignment(Element.ALIGN_CENTER); cell10.setHorizontalAlignment(Element.ALIGN_CENTER); cell10.setBorderColor(new Color(0, 0, 0)); cell10.setBackgroundColor(new Color(204, 153, 255)); aTable.addCell(cell10);
for(int i=0;i<22;i++){
aTable.addCell(new Cell(i+""));
}
document.add(aTable); document.add(new Paragraph("\n"));
}
/**
- @param imgUrl 圖片路徑
- @param imageAlign 顯示位置
- @param height 顯示高度
- @param weight 顯示寬度
- @param percent 顯示比例
- @param heightPercent 顯示高度比例
- @param weightPercent 顯示寬度比例
- @param rotation 顯示圖片旋轉角度
- @throws MalformedURLException
- @throws IOException
@throws DocumentException */ public void insertImg(String imgUrl,int imageAlign,int height,int weight,int percent,int heightPercent,int weightPercent,int rotation) throws MalformedURLException, IOException, DocumentException{ // 添加圖片 Image img = Image.getInstance(imgUrl); if(img==null)
return;
img.setAbsolutePosition(0, 0); img.setAlignment(imageAlign); img.scaleAbsolute(height, weight); img.scalePercent(percent); img.scalePercent(heightPercent, weightPercent); img.setRotation(rotation);
document.add(img); }
public void closeDocument() throws DocumentException{ this.document.close(); }
public static void main(String[] args) throws DocumentException, IOException { WordTemplete wt = new WordTemplete(); wt.openDocument("d:\dome1.doc"); wt.insertTitle("一、測試基本情況", 12, Font.BOLD, Element.ALIGN_CENTER);
wt.insertContext("共識別出XXX個測試,XXX項測試行為,其中,違規類測試XX項,占測試總量的XX%,違約類測試XX項,占測試總量的XX%,侵權類測試XX項,占測試總量的XX%,怠于類測試XX項,占測試總量的XX%,不當類測試XX項,占測試總量的XX%。", 12, Font.NORMAL, Element.ALIGN_LEFT); wt.insertContext("根據測試測評結果,各等級測試數量及所占百分比分別為:一級測試共XX項,占測試總量的XX%;二級測試共XX項,占測試總量的XX%;三級測試共XX項,占測試總量的XX%;四級測試共XX項,占測試總量的XX%;五級測試共XX項,占測試總量的XX%。\n\n", 12, Font.NORMAL, Element.ALIGN_LEFT);
wt.insertContext("測試定向分析結果如下:", 12, Font.NORMAL, Element.ALIGN_LEFT);
wt.insertContext("① 部門角度測試分析", 12, Font.NORMAL, Element.ALIGN_LEFT); wt.insertImg("test.bmp", Image.ALIGN_CENTER, 12, 35, 50, 50, 50, 30); wt.insertContext("② 主體角度測試分析", 12, Font.NORMAL, Element.ALIGN_LEFT); wt.insertImg("test.bmp", Image.ALIGN_CENTER, 12, 35, 50, 60, 60, 30); wt.insertContext("③ 部門主體交叉角度測試分析", 12, Font.NORMAL, Element.ALIGN_LEFT); wt.insertImg("test.bmp", Image.ALIGN_CENTER, 50, 75, 100, 100, 100, 30); wt.insertContext("④ 業務活動角度測試分析", 12, Font.NORMAL, Element.ALIGN_LEFT); wt.insertImg("test.bmp", Image.ALIGN_CENTER, 12, 35, 50, 80, 80, 30);
wt.insertTitle("二、重大測試清單", 12, Font.BOLD, Element.ALIGN_CENTER); wt.insertRiskTable(); wt.insertTitle("三、測試控制現狀評估結果", 12, Font.BOLD, Element.ALIGN_CENTER); wt.insertRiskEvaluationTable(); wt.insertTitle("四、測試控制計劃", 12, Font.BOLD, Element.ALIGN_CENTER); wt.insertRiskControlTable(); wt.closeDocument(); } }</pre>