2007-08-09
itext生成Word文档
关键字: itext生成word文档
自己写了一个itext导出word的demo,希望对大家能有所帮助,itext的包,自己去网上下载吧,参考itext生成pdf文档。
java 代码
- /*
- * RTFCreate.java
- *
- * Created on 2007年8月9日, 上午9:32
- *
- * To change this template, choose Tools | Template Manager
- * and open the template in the editor.
- */
- 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;
- /**
- *
- * @author julycn
- */
- public class RTFCreate {
- /**
- * Creates a new instance of 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{
- //创建word文档
- Document document=new Document(PageSize.A4);
- //输入word文档
- 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);
- //创建有3列的表格
- 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();
- }
- }
- 浏览: 56327 次
- 性别:

- 来自: 上海

- 详细资料
搜索本博客
我的相册
seam.jpg
共 13 张
共 13 张
最近加入圈子
最新评论
-
Gwt-Ext学习笔记之基础篇
问题已经解决
-- by chinahcl -
Gwt-Ext学习笔记之基础篇
[ERROR] Unable to find 'com/google/gwt/u ...
-- by chinahcl -
Gwt-Ext学习笔记之基础篇
谢谢楼主,楼主厉害
-- by chinahcl -
Seam实例教程(环境配置)
不好意思,为什么照你这样做的,但是会报错, 10:30:39,562 INFO ...
-- by yuyanshan -
Seam实例教程(环境配置)
总算自己弄明白了
-- by afadgaeg






评论排行榜