论坛首页 入门讨论版 Java

itext生成Word文档

浏览 1830 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
最后更新时间:2007-09-14 关键字: itext生成word文档
自己写了一个itext导出word的demo,希望对大家能有所帮助,itext的包,自己去网上下载吧,参考itext生成pdf文档。

java 代码
 
  1. /* 
  2.  * RTFCreate.java 
  3.  * 
  4.  * Created on 2007年8月9日, 上午9:32 
  5.  * 
  6.  * To change this template, choose Tools | Template Manager 
  7.  * and open the template in the editor. 
  8.  */  
  9.   
  10. package test;  
  11.   
  12. import com.lowagie.text.Cell;  
  13. import com.lowagie.text.Document;  
  14. import com.lowagie.text.DocumentException;  
  15. import com.lowagie.text.Font;  
  16. import com.lowagie.text.Image;  
  17. import com.lowagie.text.PageSize;  
  18. import com.lowagie.text.Paragraph;  
  19. import com.lowagie.text.Table;  
  20. import com.lowagie.text.pdf.BaseFont;  
  21. import com.lowagie.text.rtf.RtfWriter2;  
  22. import java.awt.Color;  
  23. import java.io.FileNotFoundException;  
  24. import java.io.FileOutputStream;  
  25. import java.io.IOException;  
  26. import java.net.MalformedURLException;  
  27. import javax.swing.JOptionPane;  
  28.   
  29. /** 
  30.  * 
  31.  * @author julycn 
  32.  */  
  33. public class RTFCreate {  
  34.       
  35.     /** 
  36.      * Creates a new instance of RTFCreate 
  37.      */  
  38.     public RTFCreate() {  
  39.     }  
  40.       
  41.     public static void main(String[] args){  
  42.         RTFCreate rtfCreate=new RTFCreate();  
  43.         try {  
  44.             rtfCreate.createRTF();  
  45.             JOptionPane.showMessageDialog(null,"表格已经成功创建");  
  46.         } catch (MalformedURLException ex) {  
  47.             JOptionPane.showMessageDialog(null,"表格导出出错,错误信息:"+ex+"\n错误原因可能是表格已经打开!");  
  48.             ex.printStackTrace();  
  49.         } catch (FileNotFoundException ex) {  
  50.             JOptionPane.showMessageDialog(null,"表格导出出错,错误信息:"+ex+"\n错误原因可能是表格已经打开!");  
  51.             ex.printStackTrace();  
  52.         } catch (IOException ex) {  
  53.             JOptionPane.showMessageDialog(null,"表格导出出错,错误信息:"+ex+"\n错误原因可能是表格已经打开!");  
  54.             ex.printStackTrace();  
  55.         } catch (DocumentException ex) {  
  56.             JOptionPane.showMessageDialog(null,"表格导出出错,错误信息:"+ex+"\n错误原因可能是表格已经打开!");  
  57.             ex.printStackTrace();  
  58.         }  
  59.     }  
  60.       
  61.     public void createRTF() throws FileNotFoundException, DocumentException, MalformedURLException, IOException{  
  62.         //创建word文档  
  63.         Document document=new Document(PageSize.A4);  
  64.         //输入word文档  
  65.         RtfWriter2.getInstance(document,new FileOutputStream("d:\\word.rtf"));  
  66.         document.open();  
  67.         //中文字体  
  68.         BaseFont bfChinese=BaseFont.createFont("STSongStd-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);  
  69.         Font fontChinese=new Font(bfChinese,12,Font.HELVETICA);  
  70.         //创建有3列的表格  
  71.         Table table=new Table(3);  
  72.         document.add(new Paragraph("生成rft文档!",fontChinese));  
  73.         table.setBorderWidth(1);  
  74.         table.setBorderColor(new Color(0,0,255));  
  75.         table.setPadding(5);  
  76.         table.setSpacing(5);  
  77.           
  78.         //添加表头元素  
  79.         Cell cell=new Cell("header");  
  80.         cell.setHeader(true);  
  81.         cell.setColspan(3);  
  82.         table.addCell(cell);  
  83.         table.endHeaders();//表头结束  
  84.           
  85.         //表格主体  
  86.         cell=new Cell("Example cell with colspan 1 and rowspan 2");  
  87.         cell.setRowspan(2);  
  88.         cell.setBorderColor(new Color(255,0,0));  
  89.         table.addCell(cell);  
  90.         table.addCell("1.1");  
  91.         table.addCell("2.1");  
  92.         table.addCell("1.2");  
  93.         table.addCell("2.2");  
  94.         table.addCell(new Paragraph("测试1",fontChinese));  
  95.         table.addCell("big cell");  
  96.         cell.setRowspan(2);  
  97.         cell.setColspan(2);  
  98.         table.addCell(cell);  
  99.         table.addCell(new Paragraph("测试2",fontChinese));  
  100.         document.add(table);  
  101.         //在表格末尾添加图片  
  102.         Image png=Image.getInstance("d:\\duck.jpg");  
  103.         document.add(png);  
  104.         document.close();  
  105.     }  
  106. }  
   
最后更新时间:2007-08-10
了解一下
   
0 请登录后投票
最后更新时间:2007-09-06
请问怎么在现有的一个rtf文件中追加内容?谢谢!
   
0 请登录后投票
最后更新时间:2008-07-02
楼上的问题我也想问。
如何打开一个存在的RTF?
   
0 请登录后投票
论坛首页 入门讨论版 Java

跳转论坛:
JavaEye推荐