使用 JACOB 保存 Word 文档 (Java)

发布于 2024-09-12 19:46:21 字数 376 浏览 2 评论 0原文

我正在尝试制作一个简单的Java程序来打开现有的word文档,更改某些内容并将其另存为.html文件。

不起作用的部分是将其另存为 .html 。 问题是,我得到了 html 文件,但它只是一个重命名的 doc 文件。所以这并不是一个我可以使用的 .html 文件。

这是我在 Google 中发现的:

Object oWordBasic = Dispatch.call(oWord, "WordBasic").getDispatch(); 
Dispatch.call((Dispatch) oWordBasic, "FileSaveAs", path); 

我必须做什么,才能获得 html 文件作为输出?

先感谢您。

i'm trying to make a simple Java program to open an existing word-document, change something and save it as .html-file.

The part which is not working is to save it as .html .
The problem is, i got the html-file but it's only a renamed doc-file. So not really a .html-file which I can work with.

This is what I found with Google:

Object oWordBasic = Dispatch.call(oWord, "WordBasic").getDispatch(); 
Dispatch.call((Dispatch) oWordBasic, "FileSaveAs", path); 

What I have to do, to get a html-file as output?

Thank you in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

红尘作伴 2024-09-19 19:46:21

它使用 OLE 自动化对象来保存文件,因此您必须找到指示文件类型的方法或参数。

这是我可以使用 Word 录制的宏:

ActiveDocument.SaveAs filename:="asdd.htm", FileFormat:=wdFormatHTML, _
    LockComments:=False, Password:="", AddToRecentFiles:=True, WritePassword _
    :="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:=False, _
    SaveNativePictureFormat:=False, SaveFormsData:=False, SaveAsAOCELetter:= _
    False

因此,这意味着您必须向 SaveAs 方法指示 FileFormat := wdFormatHTML (或常量值)参数。这留给读者作为练习:)

It's using the OLE Automation Object to save the file, so you have to find the method or parameter to indicate filetype.

This is the macro I could record using Word:

ActiveDocument.SaveAs filename:="asdd.htm", FileFormat:=wdFormatHTML, _
    LockComments:=False, Password:="", AddToRecentFiles:=True, WritePassword _
    :="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:=False, _
    SaveNativePictureFormat:=False, SaveFormsData:=False, SaveAsAOCELetter:= _
    False

So it means you have to indicate FileFormat := wdFormatHTML (or the constant value) parameter to the SaveAs method. That's left as an exercise to the reader :)

赢得她心 2024-09-19 19:46:21

我明白了,感谢赫利俄斯的提示。

正确的代码是:

Object oWordBasic = Dispatch.call(oWord, "WordBasic").getDispatch(); 
Dispatch.call((Dispatch) oWordBasic, "FileSaveAs", path, new Variant(8)); 

变体的参数是输出格式。 (例如8是html,6是rtf,17是pdf)
您可以在以下位置找到完整列表:WdSaveFormat 枚举

I figured it out, thanks to helios for the tip.

The correct code is:

Object oWordBasic = Dispatch.call(oWord, "WordBasic").getDispatch(); 
Dispatch.call((Dispatch) oWordBasic, "FileSaveAs", path, new Variant(8)); 

The Parameter of the variant is the output format. (for example 8 is html, 6 is rtf, 17 is pdf)
You can find the full list at: WdSaveFormat Enumeration

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文