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文档的代码:

Java代码   收藏代码
  1. <span style="">import java.awt.Color;  
  2. import java.io.FileNotFoundException;  
  3. import java.io.FileOutputStream;  
  4. import java.io.IOException;  
  5. import com.lowagie.text.Cell;  
  6. import com.lowagie.text.Document;  
  7. import com.lowagie.text.DocumentException;  
  8. import com.lowagie.text.Element;  
  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.pdf.BaseFont;  
  14. import com.lowagie.text.rtf.RtfWriter2;  
  15. public class CreateWordDemo {  
  16.     public void createDocContext(String file,String contextString)throws DocumentException, IOException{  
  17.         //设置纸张大小  
  18.         Document document = new Document(PageSize.A4);  
  19.         //建立一个书写器,与document对象关联  
  20.         RtfWriter2.getInstance(document, new FileOutputStream(file));  
  21.         document.open();  
  22.         //设置中文字体  
  23.         BaseFont bfChinese = BaseFont.createFont("STSongStd-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);  
  24.         //标题字体风格  
  25.         Font titleFont = new Font(bfChinese,12,Font.BOLD);  
  26.         //正文字体风格  
  27.         Font contextFont = new Font(bfChinese,10,Font.NORMAL);  
  28.         Paragraph title = new Paragraph("标题");  
  29.         //设置标题格式对齐方式  
  30.         title.setAlignment(Element.ALIGN_CENTER);  
  31.         title.setFont(titleFont);  
  32.         document.add(title);  
  33.         Paragraph context = new Paragraph(contextString);  
  34.         context.setAlignment(Element.ALIGN_LEFT);  
  35.         context.setFont(contextFont);  
  36.         //段间距  
  37.         context.setSpacingBefore(3);  
  38.         //设置第一行空的列数  
  39.         context.setFirstLineIndent(20);  
  40.         document.add(context);  
  41.         //设置Table表格,创建一个三列的表格  
  42.         Table table = new Table(3);  
  43.         int width[] = {25,25,50};//设置每列宽度比例  
  44.         table.setWidths(width);  
  45.         table.setWidth(90);//占页面宽度比例  
  46.         table.setAlignment(Element.ALIGN_CENTER);//居中  
  47.         table.setAlignment(Element.ALIGN_MIDDLE);//垂直居中  
  48.         table.setAutoFillEmptyCells(true);//自动填满  
  49.         table.setBorderWidth(1);//边框宽度  
  50.         //设置表头  
  51.         Cell haderCell = new Cell("表格表头");  
  52.         haderCell.setHeader(true);  
  53.         haderCell.setColspan(3);  
  54.         table.addCell(haderCell);  
  55.         table.endHeaders();  
  56.           
  57.         Font fontChinese = new Font(bfChinese,12,Font.NORMAL,Color.GREEN);  
  58.         Cell cell = new Cell(new Paragraph("这是一个3*3测试表格数据",fontChinese));  
  59.         cell.setVerticalAlignment(Element.ALIGN_MIDDLE);  
  60.         table.addCell(cell);  
  61.         table.addCell(new Cell("#1"));  
  62.         table.addCell(new Cell("#2"));  
  63.         table.addCell(new Cell("#3"));  
  64.           
  65.         document.add(table);  
  66.         document.close();  
  67.               
  68.     }  
  69.     public static void main(String[] args) {  
  70.         CreateWordDemo word = new CreateWordDemo();  
  71.         String file = "test.doc";  
  72.         try {  
  73.             word.createDocContext(file, "测试iText导出Word文档");  
  74.         } catch (DocumentException e) {  
  75.             e.printStackTrace();  
  76.         } catch (IOException e) {  
  77.             e.printStackTrace();  
  78.         }  
  79.     }  
  80. }</span>  
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值