Html转Word的几种方案

1. LibreOffice

--convert-to doc:"MS Word 97"            # produces a dot graphic
--convert-to doc:"MS Word 2003 XML"      # measure_conversion.xsl: Find no conversion for  to 'twip'!
--convert-to doc:"MS Word 2007 XML"      # produces a graphic with a "read-error"
--convert-to docx:"Office Open XML Text" # produces a graphic with a "read-error"

# 完整命令
soffice --headless --convert-to docx:"Office Open XML Text" abc.html

# 其中,abc.html 是输入的html文件,转换后将在abc.html同目录生成abc.docx文件。
# 也可指定生成文件的路径: --outdir /path

优点:免费开源;windows/linux 均可使用。

缺点:可能有些内容无法转换,比如:html中的图片,如果使用外网图片,则转换后的word图片也需要联网才能显示。数学公式的格式支持的不完善等。

2.Microsoft Office 的相关组件

没有深入研究,缺点是 只能在windows使用。优点是 对Office支持较好。

3.Apache Poi

InputStream is = new FileInputStream(new File("abc.html"));
// 注意 这里扩展名/格式只能是 doc
OutputStream os = new FileOutputStream(new File("abc.doc"));
POIFSFileSystem fs = new POIFSFileSystem();
// 对应于org.apache.poi.hdf.extractor.WordDocument
fs.createDocument(is, "WordDocument");
fs.writeFilesystem(os);
os.close();
is.close();

上面,只能将html转为doc。docx尚未研究。

4.Jacob

ActiveXComponent app = new ActiveXComponent("Word.Application"); // 启动word
try {
    app.setProperty("Visible", new Variant(false));
    Dispatch wordDoc = app.getProperty("Documents").toDispatch();
    wordDoc = Dispatch.invoke(wordDoc, "Add", Dispatch.Method, new Object[0], new int[1]).toDispatch();
    // 使用下面这种方式 可以打开dotx模板新建文档
    //wordDoc = Dispatch.invoke((Dispatch) docs, "Open", Dispatch.Method, new Object[] {(String) fileList.get(0), new Variant(false), new Variant(true) }, new int[3]).toDispatch();
    Dispatch.invoke(app.getProperty("Selection").toDispatch(), "InsertFile", Dispatch.Method, new Object[] { htmlFile, "", new Variant(false), new Variant(false), new Variant(false) }, new int[3]);
    Dispatch.invoke(wordDoc, "SaveAs", Dispatch.Method, new Object[] {wordFile, new Variant(HTML_WORD)}, new int[1]);
    Dispatch.call(wordDoc, "Close", new Variant(false));
} catch (Exception e) {
    e.printStackTrace();
} finally {
    app.invoke("Quit", new Variant[] {}); 
}

缺点:依赖 windows 和 ms office 软件,需要将jacob.dll拷贝到windows\system32或者%javahome%\jre\bin目录下面。

5.docx4j

https://github.com/plutext

优点:开源;不依赖外部组件。可支持装订线这种模板,但该模板.docx必须是ms office 保存(或创建的)兼容模式的docx文档。

缺点:只支持docx;

转载于:https://my.oschina.net/tita/blog/3022749

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值