利用IText导出Word

生成Word文档的类库有不少,经常使用的有jacob,poi,itext等等,jacob操做office的能力是不错的,可是对于我我的来讲,我不喜欢它的两方面:1、jacob只能应用于windows平台。2、除了要把相应的ar包加载到类路径下,还须要把jacob.dll复制到windows/system32目录中。poi操做excel方面的能力很是强大,对于word方面的操做能力仍是不够的。

   如今来讲说IText吧。IText是用于生成PDF文档的一个java类库。经过iText不只能够生成PDF或rtf的文档,并且能够将XML、Html文件转化为PDF文件。固然也能够生成word文档。并且操做方法简单。使用IText须要3个jar包:iText-2.1.7.jar(核心包),iTextAsian.jar(解决中文输出问题),itext-rtf-2.1.7.jar(操做rtf格式)。(最后会提供jar包与实例下载)。

   用iText生成Word文档须要5个步骤:

  ①创建com.lowagie.text.Document对象的实例。 Document document = new Document();java

②创建一个书写器(Writer)与document对象关联,经过书写器(Writer)能够将文档写入到磁盘中。

      RtfWriter2.getInstance(this.document, new FileOutputStream(filePath));

  ③打开文档。windows

document.open();

  ④向文档中添加内容。字体

document.add(new Paragraph("Hello World"));

  ⑤关闭文档。(在最后必须关闭文档,不然即便生成了word文档也会打不开)this

document.close(); 


  下面提供一个通用文件给你们:

public class WordUtils { private Document document; private BaseFont bfChinese;.net

public BaseFont getBfChinese() {
    return bfChinese;  
}  

public void setBfChinese(BaseFont bfChinese) {  
    this.bfChinese = bfChinese;  
}  

public Document getDocument() {  
    return document;  
}  

public void setDocument(Document document) {  
    this.document = document;  
}  

public WordUtils(){  
    this.document = new Document(PageSize.A4);//设置纸张大小
      
}  
/** 创建一个书写器(Writer)与document对象关联,经过书写器(Writer)能够将文档写入到磁盘中
 * @param filePath  要操做的文档路径,若文档不存在会自动建立
 * @throws com.lowagie.text.DocumentException
 * @throws java.io.IOException
 */  
public void openDocument(String filePath) throws DocumentException,  
IOException {

// 创建一个书写器(Writer)与document对象关联,经过书写器(Writer)能够将文档写入到磁盘中
RtfWriter2.getInstance(this.document, new FileOutputStream(filePath));
this.document.open();
// 设置中文字体
this.bfChinese = BaseFont.createFont("STSongStd-Light",
"UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); }excel

/** 
 * @param titleStr 标题
 * @param fontsize 字体大小 
 * @param fontStyle 字体样式 
 * @param elementAlign 对齐方式 
 * @throws com.lowagie.text.DocumentException
 */  
public void insertTitle(String titleStr,int fontsize,int fontStyle,int elementAlign) throws DocumentException{  
    Font titleFont = new Font(this.bfChinese, fontsize, fontStyle);  
    Paragraph title = new Paragraph(titleStr);  
    // 设置标题格式对齐方式  
    title.setAlignment(elementAlign);  
    title.setFont(titleFont);  
      
    this.document.add(title);  
}  
  


/**
 * 设置带有目录格式的标题(标题1格式)
 *
 * @param rtfParagraphStyle 标题1样式
 * @param titleStr 标题
 * @throws DocumentException
 */
public void insertTitlePattern(String titleStr, RtfParagraphStyle rtfParagraphStyle) throws DocumentException{
    Paragraph title = new Paragraph(titleStr);
    title.setFont(rtfParagraphStyle);
    this.document.add(title);
}  


/**
 * 设置带有目录格式的标题(标题2格式)
 * @param titleStr 标题
 * @param rtfParagraphStyle 标题2样式
 * @throws DocumentException
 */
public void insertTitlePatternSecond(String titleStr,RtfParagraphStyle rtfParagraphStyle) throws DocumentException{
    Paragraph title = new Paragraph(titleStr);
    // 设置标题格式对齐方式  
    title.setFont(rtfParagraphStyle);
    this.document.add(title);
}  


/** 
 * @param tableName 标题 
 * @param fontsize 字体大小 
 * @param fontStyle 字体样式 
 * @param elementAlign 对齐方式 
 * @throws com.lowagie.text.DocumentException
 */  
public void insertTableName(String tableName,int fontsize,int fontStyle,int elementAlign) throws DocumentException{  
    Font titleFont = new Font(this.bfChinese, fontsize, fontStyle);  
    titleFont.setColor(102, 102, 153);
    Paragraph title = new Paragraph(tableName);  
    // 设置标题格式对齐方式  
    title.setAlignment(elementAlign);  
    title.setFont(titleFont);  
      
    this.document.add(title);  
}  

/** 
 * @param contextStr 内容 
 * @param fontsize 字体大小 
 * @param fontStyle 字体样式 
 * @param elementAlign 对齐方式 
 * @throws com.lowagie.text.DocumentException
 */  
public void insertContext(String contextStr,int fontsize,int fontStyle,int elementAlign) throws DocumentException{  
    // 正文字体风格  
    Font contextFont = new Font(bfChinese, fontsize, fontStyle);  
    Paragraph context = new Paragraph(contextStr);  
    //设置行距  
    context.setLeading(3f);
    // 正文格式左对齐  
  //  context.setAlignment(elementAlign);
    context.setFont(contextFont);  
    // 离上一段落(标题)空的行数  
    context.setSpacingBefore(1);
    // 设置第一行空的列数  
    context.setFirstLineIndent(20);  
    document.add(context);  
}


/**
 * @param imgUrl 图片路径
 * @param imageAlign 显示位置
 * @param height 显示高度
 * @param weight 显示宽度
 * @param percent 显示比例
 * @param heightPercent 显示高度比例
 * @param weightPercent 显示宽度比例
 * @param rotation 显示图片旋转角度
 * @throws java.net.MalformedURLException
 * @throws java.io.IOException
 * @throws com.lowagie.text.DocumentException
 */
public void insertImg(String imgUrl,int imageAlign,int height,int weight,int percent,int heightPercent,int weightPercent,int rotation) throws MalformedURLException, IOException, DocumentException{

// 添加图片
Image img = Image.getInstance(imgUrl);
if(img==null)
return;
img.setAbsolutePosition(0, 0);
img.setAlignment(imageAlign);
img.scaleAbsolute(height, weight); img.scaleAbsolute(1000, 1000); img.scalePercent(percent); img.scalePercent(heightPercent, weightPercent);
img.setRotation(rotation);
document.add(img); }code

/**
 * 添加简单表格
 * @param column 表格列数(必须)
 * @param row 表格行数
 * @throws DocumentException
 */
public void insertSimpleTable(int column,int row) throws DocumentException {
    Table table=new Table(column);//列数必须设置,而行数则能够按照我的要求来决定是否须要设置
    table.setAlignment(Element.ALIGN_CENTER);// 居中显示
    table.setAlignment(Element.ALIGN_MIDDLE);// 纵向居中显示
    table.setAutoFillEmptyCells(true);// 自动填满
    table.setBorderColor(new Color(0, 125, 255));// 边框颜色
    table.setBorderWidth(1);// 边框宽度
    table.setSpacing(2);// 衬距,
    table.setPadding(2);// 即单元格之间的间距
    table.setBorder(20);// 边框
    for (int i = 0; i < column*3; i++) {
        table.addCell(new Cell(""+i));
    }
    document.add(table);
}
/**
 * 在操做完成后必须关闭document,不然即便生成了word文档,打开也会发生错误
 * @throws DocumentException
 */
public void closeDocument() throws DocumentException{  
    this.document.close();  
}

}orm

相关文章
相关标签/搜索