IText导出Word文档

      在开发过程中,经常使用一些导出功能,如Excel,Word,PDF等,今天项目中需要做一个Word文档的导出功能,使用了一个开源工具IText。

 

      iText是著名的开放源码的站点sourceforge一个项目,是用于生成PDF文档的一个java类库。通过iText不仅可以生成PDF或rtf的文档,而且可以将XML、Html文件转化为PDF文件。

  iText的安装非常方便,在http://www.lowagie.com/iText/download.html - download 网站上下载iText.jar文件后,只需要在系统的CLASSPATH中加入iText.jar的路径,在程序中就可以使用iText类库了。

     一下是一段使用iText生成Word文档的代码:

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.Element;
import com.lowagie.text.Font;
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;
public class CreateWordDemo {
	public void createDocContext(String file,String contextString)throws DocumentException, IOException{
		//设置纸张大小
		Document document = new Document(PageSize.A4);
		//建立一个书写器,与document对象关联
		RtfWriter2.getInstance(document, new FileOutputStream(file));
		document.open();
		//设置中文字体
		BaseFont bfChinese = BaseFont.createFont("STSongStd-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
		//标题字体风格
		Font titleFont = new Font(bfChinese,12,Font.BOLD);
		//正文字体风格
		Font contextFont = new Font(bfChinese,10,Font.NORMAL);
		Paragraph title = new Paragraph("标题");
		//设置标题格式对齐方式
		title.setAlignment(Element.ALIGN_CENTER);
		title.setFont(titleFont);
		document.add(title);
		Paragraph context = new Paragraph(contextString);
		context.setAlignment(Element.ALIGN_LEFT);
		context.setFont(contextFont);
		//段间距
		context.setSpacingBefore(3);
		//设置第一行空的列数
		context.setFirstLineIndent(20);
		document.add(context);
		//设置Table表格,创建一个三列的表格
		Table table = new Table(3);
		int width[] = {25,25,50};//设置每列宽度比例
		table.setWidths(width);
		table.setWidth(90);//占页面宽度比例
		table.setAlignment(Element.ALIGN_CENTER);//居中
		table.setAlignment(Element.ALIGN_MIDDLE);//垂直居中
		table.setAutoFillEmptyCells(true);//自动填满
		table.setBorderWidth(1);//边框宽度
		//设置表头
		Cell haderCell = new Cell("表格表头");
		haderCell.setHeader(true);
		haderCell.setColspan(3);
		table.addCell(haderCell);
		table.endHeaders();
		
		Font fontChinese = new Font(bfChinese,12,Font.NORMAL,Color.GREEN);
		Cell cell = new Cell(new Paragraph("这是一个3*3测试表格数据",fontChinese));
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		table.addCell(cell);
		table.addCell(new Cell("#1"));
		table.addCell(new Cell("#2"));
		table.addCell(new Cell("#3"));
		
		document.add(table);
		document.close();
			
	}
	public static void main(String[] args) {
		CreateWordDemo word = new CreateWordDemo();
		String file = "test.doc";
		try {
			word.createDocContext(file, "测试iText导出Word文档");
		} catch (DocumentException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值