1. 程式人生 > >Java使用iText生成word檔案的解決方案

Java使用iText生成word檔案的解決方案

               

轉自【http://www.31km.cn/post/450.html】

JAVA生成WORD檔案的方法目前有以下種:

一種是jacob 但是侷限於windows平臺 往往許多JAVA程式運行於其他作業系統 在此不討論該方案

一種是pio但是他的excel處理很程式 word模組還侷限於讀取word的文字內容,寫word檔案就更弱項了

本文介紹的是itext生成rtf檔案並儲存格式為word 此方案本人已實踐過 並已在專案中使用

用到的jar包:iText-2.1.5.jariText-rtf-2.1.4.jariTextAsian.jar

[java] view plain copy print?
  1. package
     com.rye.test;    
  2. import java.awt.Color;    
  3. import java.io.FileNotFoundException;    
  4. import java.io.FileOutputStream;    
  5. import java.io.IOException;    
  6. import com.lowagie.text.Cell;    
  7. import com.lowagie.text.Document;    
  8. import com.lowagie.text.DocumentException;    
  9. import com.lowagie.text.Font;    
  10. import com.lowagie.text.PageSize;    
  11. import com.lowagie.text.Paragraph;    
  12. import com.lowagie.text.Table;    
  13. import com.lowagie.text.rtf.RtfWriter2;    
  14. /**   
  15.   * 建立word文件 步驟:    
  16.   * 1,建立文件    
  17.   * 2,建立一個書寫器    
  18.   * 3,開啟文件    
  19.   * 4,向文件中寫入資料    
  20.   * 5,關閉文件   
  21.   */
  22.  publicclass WordDemo {    
  23.   public WordDemo() {    
  24.   }    
  25.   /**   
  26.    * @param args   
  27.    */
  28.   publicstaticvoid main(String[] args) {    
  29.  // 建立word文件,並設定紙張的大小
  30.    Document document = new Document(PageSize.A4);   
  31.    try {    
  32.     RtfWriter2.getInstance(document,  
  33.  new FileOutputStream("E:/word.doc"));    
  34.     document.open();    
  35.    //設定合同頭  
  36.    Paragraph ph = new Paragraph();    
  37.    Font f  = new Font();    
  38.    Paragraph p = new Paragraph("出口合同",   
  39.  new Font(Font.NORMAL, 18, Font.BOLDITALIC, new Color(000)) );    
  40.     p.setAlignment(1);    
  41.     document.add(p);    
  42.     ph.setFont(f);    
  43.     // 設定中文字型  
  44.     // BaseFont bfFont =  
  45.     // BaseFont.createFont("STSongStd-Light",
  46.  "UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);    
  47.     // Font chinaFont = new Font();  
  48.     /*   
  49.      * 建立有三列的表格   
  50.      */
  51.     Table table = new Table(4);    
  52.     document.add(new Paragraph("生成表格"));    
  53.     table.setBorderWidth(1);    
  54.     table.setBorderColor(Color.BLACK);    
  55.     table.setPadding(0);    
  56.     table.setSpacing(0);    
  57.     /*   
  58.      * 新增表頭的元素   
  59.      */
  60.     Cell cell = new Cell("表頭");//單元格  
  61.     cell.setHeader(true);    
  62.     cell.setColspan(3);//設定表格為三列  
  63.     cell.setRowspan(3);//設定表格為三行  
  64.     table.addCell(cell);    
  65.     table.endHeaders();// 表頭結束  
  66.     // 表格的主體  
  67.     cell = new Cell("Example cell 2");    
  68.     cell.setRowspan(2);//當前單元格佔兩行,縱向跨度  
  69.     table.addCell(cell);    
  70.     table.addCell("1,1");    
  71.     table.addCell("1,2");    
  72.     table.addCell("1,3");    
  73.     table.addCell("1,4");    
  74.     table.addCell("1,5");    
  75.     table.addCell(new Paragraph("用java生成的表格1"));    
  76.     table.addCell(new Paragraph("用java生成的表格2"));    
  77.     table.addCell(new Paragraph("用java生成的表格3"));    
  78.     table.addCell(new Paragraph("用java生成的表格4"));    
  79.     document.add(new Paragraph("用java生成word檔案"));    
  80.     document.add(table);    
  81.     document.close();    
  82.    } catch (FileNotFoundException e) {    
  83.     e.printStackTrace();    
  84.    } catch (DocumentException e) {    
  85.     e.printStackTrace();    
  86.    } catch (IOException e) {    
  87.     e.printStackTrace();    
  88.    }    
  89.   }    
  90.  }   
package com.rye.test;  import java.awt.Color;  import java.io.FileNotFoundException;  import java.io.FileOutputStream;  import java.io.IOException;   import com.lowagie.text.Cell;  import com.lowagie.text.Document;  import com.lowagie.text.DocumentException;  import com.lowagie.text.Font;  import com.lowagie.text.PageSize;  import com.lowagie.text.Paragraph;  import com.lowagie.text.Table;  import com.lowagie.text.rtf.RtfWriter2;  /**    * 建立word文件 步驟:     * 1,建立文件     * 2,建立一個書寫器     * 3,開啟文件     * 4,向文件中寫入資料     * 5,關閉文件    */  public class WordDemo {      public WordDemo() {    }      /**     * @param args     */   public static void main(String[] args) {   // 建立word文件,並設定紙張的大小   Document document = new Document(PageSize.A4);    try {      RtfWriter2.getInstance(document, new FileOutputStream("E:/word.doc"));       document.open();          //設定合同頭          Paragraph ph = new Paragraph();     Font f  = new Font();          Paragraph p = new Paragraph("出口合同",  new Font(Font.NORMAL, 18, Font.BOLDITALIC, new Color(0, 0, 0)) );      p.setAlignment(1);      document.add(p);      ph.setFont(f);        // 設定中文字型      // BaseFont bfFont =      // BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);      // Font chinaFont = new Font();      /*       * 建立有三列的表格       */     Table table = new Table(4);      document.add(new Paragraph("生成表格"));      table.setBorderWidth(1);      table.setBorderColor(Color.BLACK);      table.setPadding(0);      table.setSpacing(0);            /*       * 新增表頭的元素       */     Cell cell = new Cell("表頭");//單元格      cell.setHeader(true);      cell.setColspan(3);//設定表格為三列      cell.setRowspan(3);//設定表格為三行      table.addCell(cell);      table.endHeaders();// 表頭結束       // 表格的主體      cell = new Cell("Example cell 2");      cell.setRowspan(2);//當前單元格佔兩行,縱向跨度      table.addCell(cell);      table.addCell("1,1");      table.addCell("1,2");      table.addCell("1,3");      table.addCell("1,4");      table.addCell("1,5");      table.addCell(new Paragraph("用java生成的表格1"));      table.addCell(new Paragraph("用java生成的表格2"));      table.addCell(new Paragraph("用java生成的表格3"));      table.addCell(new Paragraph("用java生成的表格4"));      document.add(new Paragraph("用java生成word檔案"));      document.add(table);      document.close();     } catch (FileNotFoundException e) {      e.printStackTrace();     } catch (DocumentException e) {      e.printStackTrace();     } catch (IOException e) {      e.printStackTrace();     }    }     }