- julycn
- 等级: 初级会员

- 性别:

- 文章: 213
- 积分: 90
- 来自: 上海

|
最后更新时间:2007-09-14 关键字: itext生成word文档
自己写了一个itext导出word的demo,希望对大家能有所帮助,itext的包,自己去网上下载吧,参考itext生成pdf文档。
java 代码
-
-
-
-
-
-
-
-
-
- package test;
-
- 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 java.awt.Color;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.net.MalformedURLException;
- import javax.swing.JOptionPane;
-
-
-
-
-
- public class RTFCreate {
-
-
-
-
- public RTFCreate() {
- }
-
- public static void main(String[] args){
- RTFCreate rtfCreate=new RTFCreate();
- try {
- rtfCreate.createRTF();
- JOptionPane.showMessageDialog(null,"表格已经成功创建");
- } catch (MalformedURLException ex) {
- JOptionPane.showMessageDialog(null,"表格导出出错,错误信息:"+ex+"\n错误原因可能是表格已经打开!");
- ex.printStackTrace();
- } catch (FileNotFoundException ex) {
- JOptionPane.showMessageDialog(null,"表格导出出错,错误信息:"+ex+"\n错误原因可能是表格已经打开!");
- ex.printStackTrace();
- } catch (IOException ex) {
- JOptionPane.showMessageDialog(null,"表格导出出错,错误信息:"+ex+"\n错误原因可能是表格已经打开!");
- ex.printStackTrace();
- } catch (DocumentException ex) {
- JOptionPane.showMessageDialog(null,"表格导出出错,错误信息:"+ex+"\n错误原因可能是表格已经打开!");
- ex.printStackTrace();
- }
- }
-
- public void createRTF() throws FileNotFoundException, DocumentException, MalformedURLException, IOException{
-
- Document document=new Document(PageSize.A4);
-
- RtfWriter2.getInstance(document,new FileOutputStream("d:\\word.rtf"));
- document.open();
-
- BaseFont bfChinese=BaseFont.createFont("STSongStd-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
- Font fontChinese=new Font(bfChinese,12,Font.HELVETICA);
-
- Table table=new Table(3);
- document.add(new Paragraph("生成rft文档!",fontChinese));
- table.setBorderWidth(1);
- table.setBorderColor(new Color(0,0,255));
- table.setPadding(5);
- table.setSpacing(5);
-
-
- Cell cell=new Cell("header");
- cell.setHeader(true);
- cell.setColspan(3);
- table.addCell(cell);
- table.endHeaders();
-
-
- cell=new Cell("Example cell with colspan 1 and rowspan 2");
- cell.setRowspan(2);
- cell.setBorderColor(new Color(255,0,0));
- table.addCell(cell);
- table.addCell("1.1");
- table.addCell("2.1");
- table.addCell("1.2");
- table.addCell("2.2");
- table.addCell(new Paragraph("测试1",fontChinese));
- table.addCell("big cell");
- cell.setRowspan(2);
- cell.setColspan(2);
- table.addCell(cell);
- table.addCell(new Paragraph("测试2",fontChinese));
- document.add(table);
-
- Image png=Image.getInstance("d:\\duck.jpg");
- document.add(png);
- document.close();
- }
- }
声明:JavaEye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
|
| 返回顶楼 |
|
|
- daimon
- 等级: 初级会员

- 性别:

- 文章: 19
- 积分: 50
- 来自: 陕西。西安

|
了解一下
|
| 返回顶楼 |
|
|
- wbe3191
- 等级: 初级会员

- 性别:

- 文章: 1
- 积分: 0
- 来自: xi'an

|
请问怎么在现有的一个rtf文件中追加内容?谢谢!
|
| 返回顶楼 |
|
|
- 咖啡舞者
- 等级: 初级会员

- 性别:

- 文章: 53
- 积分: 0
- 来自: 福州

|
楼上的问题我也想问。
如何打开一个存在的RTF?
|
| 返回顶楼 |
|
|