在java中将word文件另存为html

时间:2012-12-07 17:12:56

标签: java command-line macros ms-word doc

我尝试使用java将word文件保存为html。我将一个word文件保存为xml,它为我工作

Runtime rt1 = Runtime.getRuntime();
rt1.exec("C:/Program Files/Microsoft Office/Office12/WINWORD.EXE /msaveasxml C:/myfolder/AB_00040.doc");

它将我的doc文件保存为特定文件夹C:/ myfolder中的xml文件,我在C:/myfolder/AB_00040.xml

中查看该xml文件

如果我想保存与html相同的文件,我该怎么办?任何一个帮助

rt1.exec("C:/Program Files/Microsoft Office/Office12/WINWORD.EXE /msaveas??? C:/myfolder/AB_00040.doc");

提前致谢

1 个答案:

答案 0 :(得分:5)

我找到了Zack Macomber提示的答案我使用宏转换word文件到html文件。我给出了该宏的编码。将宏名称保存为“saveashtml”

Sub saveashtml()
Dim xmlname As String
xmlname = ActiveDocument.FullName
xmlname = Replace(xmlname, ".docx", ".html", , , vbTextCompare)
xmlname = Replace(xmlname, ".doc", ".html", , , vbTextCompare)
ActiveDocument.SaveAs FileName:=xmlname, FileFormat:=wdFormatHTML, LockComments:=False, Password:="", AddToRecentFiles:=True, WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:=False, SaveNativePictureFormat:=False, SaveFormsData:=False, SaveAsAOCELetter:=False
Application.Quit
End sub

您可以通过

执行此宏
Runtime rt1 = Runtime.getRuntime();
rt1.exec("C:/Program Files/Microsoft Office/Office12/WINWORD.EXE /msaveashtml C:/myfolder/AB_00040.doc");

saveasxml宏编码

Sub saveasxml()
Dim xmlname As String
xmlname = ActiveDocument.FullName
xmlname = Replace(xmlname, ".docx", ".xml", , , vbTextCompare)
xmlname = Replace(xmlname, ".doc", ".xml", , , vbTextCompare)
ActiveDocument.SaveAs FileName:=xmlname, FileFormat:=wdFormatFlatXML, LockComments:=False, Password:="", AddToRecentFiles:=True, WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:=False, SaveNativePictureFormat:=False, SaveFormsData:=False, SaveAsAOCELetter:=False
End Sub