IText生成doc文檔須要三個包:iTextAsian.jar,iText-rtf-2.1.4.jar,iText-2.1.4.jarjava
親測無誤,代碼以下所示:web
import com.lowagie.text.*; import com.lowagie.text.Font; import com.lowagie.text.Rectangle; import com.lowagie.text.pdf.BaseFont; import com.lowagie.text.pdf.PdfPCell; import com.lowagie.text.pdf.PdfPTable; import com.lowagie.text.rtf.RtfWriter2; import java.awt.*; import java.io.FileOutputStream; /** * 生成報修系統的報表doc文檔 * User: HYY * Date: 13-8-1 * Time: 下午9:54 * To change this template use File | Settings | File Templates. */ public class GenerateBaoxiu { public static void main(String[] args) throws Exception { BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); //設置紙張的大小對象 Rectangle rectangle = new Rectangle(PageSize.A4); // 建立word文檔,並旋轉,使其橫向 Document document = new Document(rectangle.rotate()); RtfWriter2.getInstance(document, new FileOutputStream("C:/無幽之路IText教程.doc")); document.open(); //報表標題 Paragraph headTitle = new Paragraph("學期報修報表"); Font headFont = new Font(bfChinese); headFont.setSize(22); headFont.setStyle(Font.BOLD); headTitle.setFont(headFont); headTitle.setAlignment(1);//居中 document.add(headTitle); //正文表格 PdfPTable table = new PdfPTable(10); table.setWidths(new float[]{15f,9f,9f,9f,11f,8f,20f,15f,9f,9f}); table.setTotalWidth(114); //設置表頭字體樣式 Font tableHeadFont = new Font(bfChinese);//表頭字體樣式 tableHeadFont.setSize(10.5f);//設置表頭字體爲五號字體 tableHeadFont.setStyle(Font.BOLD);//加粗 //表頭第一列:表單申請時間 PdfPCell headCell1 = new PdfPCell(); Paragraph headCell1Phrase = new Paragraph("表單申請時間"); headCell1Phrase.setFont(tableHeadFont); headCell1.setPhrase(headCell1Phrase); headCell1Phrase.setAlignment(1); headCell1.setHorizontalAlignment(1); //表頭第二列:申請人 PdfPCell headCell2 = new PdfPCell(); Paragraph headCell2Phrase = new Paragraph("申請人"); headCell2Phrase.setFont(tableHeadFont); headCell2.setPhrase(headCell2Phrase); headCell2Phrase.setAlignment(1); headCell2.setHorizontalAlignment(1); //表頭第三列:維修人 PdfPCell headCell3 = new PdfPCell(); Paragraph headCell3Phrase = new Paragraph("維修人"); headCell3Phrase.setFont(tableHeadFont); headCell3.setPhrase(headCell3Phrase); headCell3Phrase.setAlignment(1); headCell3.setHorizontalAlignment(1); //表頭第四列:後勤人 PdfPCell headCell4 = new PdfPCell(); Paragraph headCell4Phrase = new Paragraph("後勤人"); headCell4Phrase.setFont(tableHeadFont); headCell4.setPhrase(headCell4Phrase); headCell4Phrase.setAlignment(1); headCell4.setHorizontalAlignment(1); //表頭第五列:設備類型 PdfPCell headCell5 = new PdfPCell(); Paragraph headCell5Phrase = new Paragraph("設備類型"); headCell5Phrase.setFont(tableHeadFont); headCell5.setPhrase(headCell5Phrase); headCell5Phrase.setAlignment(1); headCell5.setHorizontalAlignment(1); //表頭第六列:維修設備 PdfPCell headCell6 = new PdfPCell(); Paragraph headCell6Phrase = new Paragraph("維修設備"); headCell6Phrase.setFont(tableHeadFont); headCell6.setPhrase(headCell6Phrase); headCell6Phrase.setAlignment(1); headCell6.setHorizontalAlignment(1); //表頭第七列:維修地點 PdfPCell headCell7 = new PdfPCell(); Paragraph headCell7Phrase = new Paragraph("維修地點"); headCell7Phrase.setFont(tableHeadFont); headCell7.setPhrase(headCell7Phrase); headCell7Phrase.setAlignment(1); headCell7.setHorizontalAlignment(1); //表頭第八列:維修完成時間 PdfPCell headCell8 = new PdfPCell(); Paragraph headCell8Phrase = new Paragraph("維修完成時間"); headCell8Phrase.setFont(tableHeadFont); headCell8.setPhrase(headCell8Phrase); headCell8Phrase.setAlignment(1); headCell8.setHorizontalAlignment(1); //表頭第九列:維修評價 PdfPCell headCell9 = new PdfPCell(); Paragraph headCell9Phrase = new Paragraph("維修評價"); headCell9Phrase.setFont(tableHeadFont); headCell9.setPhrase(headCell9Phrase); headCell9Phrase.setAlignment(1); headCell9.setHorizontalAlignment(1); //表頭第十列:維修費用 PdfPCell headCell10 = new PdfPCell(); Paragraph headCell10Phrase = new Paragraph("維修費用"); headCell10Phrase.setFont(tableHeadFont); headCell10.setPhrase(headCell10Phrase); headCell10Phrase.setAlignment(1); headCell10.setHorizontalAlignment(1); table.addCell(headCell1); table.addCell(headCell2); table.addCell(headCell3); table.addCell(headCell4); table.addCell(headCell5); table.addCell(headCell6); table.addCell(headCell7); table.addCell(headCell8); table.addCell(headCell9); table.addCell(headCell10); //表頭添加完畢 //添加表格體 for(int i=0; i<1000; i++) { table.addCell(""); } document.add(table); //頁腳段落 Paragraph paraFooter = new Paragraph(); Font font = new Font(); //頁腳的字體大小 font.setSize(12f); font.setColor(new Color(0, 0, 0)); paraFooter.setFont(font); paraFooter.setAlignment("center"); //頁腳的段落和是否有頁碼 HeaderFooter footer = new HeaderFooter(paraFooter, true); //頁腳的對齊方式(應該在footer設置而不是段落中設置) footer.setAlignment(1); document.setFooter(footer); document.close(); } }