用java生成word文档

来源:互联网 发布:java dfa算法 编辑:程序博客网 时间:2024/04/28 02:09
这几日, 公司有个项目, 要用java生成word文档, 在网上找来找去也没有找到好的生成word文档的库, 找到apache的POI可以使用, 但是所有的release版中也没有支持word的class. 只能从svn上下载源代码编译. 后来发现java支持rtf格式的文档, word也支持, 于是乎便使用此产生word文档了. 呵呵.. java支持的rtf文档功能不是很强大, 我们可以借助于一些开源的库, 比如: itext就可以很好的支持. itext上有很多例子, 有兴趣的可以上去看一下, 这里就不摘录了. 但是itext比较大要1.4M, 不是很喜欢. 在sf上找来找去, 发现一个更小的库, 尽管功能不是很强大, 基本的功能都有, 他就是srw(Simple RTF Writer目前它的版本是0.6,好久都没有人维护了). srw内置了很多例子, 例如: 我们要写一个简单的rtf, 我们只需要这么写:public class TestSimpleRtf { private static final String FILE_NAME = "out_testsimplertf.rtf"; public static void main(String[] args) { try { // RTF Dokument generieren (in Querformat) RTFDocument doc = new RTFDocument(PageSize.DIN_A4_QUER); // Anzeige-Zoom und Ansicht definieren doc.setViewscale(RTFDocument.VIEWSCALE_FULLPAGE); // Anzeige-Zoom auf "komplette Seite" einstellen doc.setViewkind(RTFDocument.VIEWKIND_PAGELAYOUT); // ViewMode auf Seitenlayout stellen Paragraph absatz = new Paragraph(18, 0, 16, Font.ARIAL, new TextPart("Simple RTF Writer Testdokument")); absatz.setAlignment(Paragraph.ALIGN_CENTER); doc.addParagraph(absatz); File savefile = new File(FILE_NAME); doc.save(savefile); System.out.println("Neues RTF Dokument erstellt: " + savefile.getAbsolutePath()); } catch (IOException e) { e.printStackTrace(); } } }用法很简单, 但是功能很少, 比如没有table的功能, 不能设置打印方向等问题. 不过这个基本上就够用了. 后来, 我们的项目要求横向打印, 这可难坏了. 没办法, 自己查找word的rtf格式库, 拓展横向打印功能, 目前已经完成...import com.itseasy.rtf.RTFDocument; import com.itseasy.rtf.text.PageSize; public class MyRTFDocument extends RTFDocument { public static final int ORIENTATION_PORTRAIT = 0; public static final int ORIENTATION_LANDSCAPE = 1; private int orientation; /** * */ public MyRTFDocument() { super(); } /** * @param arg0 */ public MyRTFDocument(PageSize arg0) { super(arg0); } /* (non-Javadoc) * @see com.itseasy.rtf.RTFDocument#getDocumentAsString() */ protected String getDocumentAsString() { StringBuffer sb = new StringBuffer(super.getDocumentAsString()); int pos = -1; if (ORIENTATION_LANDSCAPE == orientation) { pos = sb.indexOf("paperw"); if (pos > 0) { sb.insert(pos, "lndscpsxn"); } } pos = 0; while((pos = sb.indexOf("pardplain", pos)) > 0){ pos = sb.indexOf("{", pos); sb.insert(pos, "dbchaf2"); } return sb.toString(); } /** * @return Returns the orientation. */ public int getOrientation() { return orientation; } /** * @param orientation The orientation to set. */ public void setOrientation(int orientation) { this.orientation = orientation; } } 在最近的一个项目中需要将一段字符类型的文本存为word,html并要将word的内容保存在数据库中,于是就有了如下的一个工具类,希望能对碰到这样需求的朋友提供点帮助。匆匆忙忙的就copy上来了,没有做一些删减,有一些多余的东西,有兴趣的朋友可以自行略去。我的注释相对比较清楚,可以按照自己的需求进行组合。在操作word的地方使用了jacob(jacob_1.9),这个工具网上很容易找到,将jacob.dll放置系统Path中,直接放在system32下也可以,jacob.jar放置在classPath中。代码如下:WordBridge.java/*** WordBridge.java*/package com.kela.util; import java.io.ByteArrayInputStream;import java.io.File;import java.io.FileInputStream;import java.io.InputStream;import java.sql.Connection;import java.sql.PreparedStatement;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import com.jacob.activeX.ActiveXComponent;import com.jacob.com.Dispatch;import com.jacob.com.Variant;import com.kela.db.PoolingDataSource;/*** 说明: 对word的操作 ** @isolated@yeah.net*/public class WordBridge {Log log = LogFactory.getLog("WordBridgt");private ActiveXComponent MsWordApp = null; private Dispatch document = null
原创粉丝点击