![](https://img.51dongshi.com/20250108/wz/18400846152.jpg)
在進(jìn)行Java程序調(diào)用OpenOffice,將doc文件轉(zhuǎn)換為Html文件的過(guò)程中,有時(shí)會(huì)遇到轉(zhuǎn)換后的格式居左的問(wèn)題。為了解決這個(gè)問(wèn)題,首先需要到官網(wǎng)下載Jacob,然后將壓縮包解壓后,將Jacob.jar添加到Libraries中(先復(fù)制到項(xiàng)目目錄中,右鍵單擊jar包選擇BuildPath—>AddtoBuildPath)。接著,將Jacob.dll放置在當(dāng)前項(xiàng)目所用到的“jre\bin”目錄下,比如Eclipse正在使用的Jre路徑是C:\Java\jdk1.7.0_17\jre\bin。需要注意的是,有些電腦可能會(huì)遇到“java.lang.UnsatisfiedLinkError:nojacobinjava.library.path”的錯(cuò)誤,這是由于系統(tǒng)未能加載jacob.dll,可以通過(guò)將Jacob.dll放置在“WINDOWS\SYSTEM32”目錄下解決。接下來(lái)是Java代碼示例,其中包含了將Word文檔轉(zhuǎn)換為Html文件的步驟。首先啟動(dòng)Word應(yīng)用程序(MicrosoftOfficeWord2003),設(shè)置Word應(yīng)用程序不可見(jiàn)。接著,通過(guò)Documents屬性獲取所有Word文檔窗口,然后打開(kāi)要轉(zhuǎn)換的Word文件。隨后,將文檔作為HTML格式保存到指定的路徑,并關(guān)閉Word文件。最后,關(guān)閉Word應(yīng)用程序。代碼如下:public class JacobUtil { public static final int WORD_HTML = 8;public static void main(String[] args) {String docfile = "C:\\Users\\無(wú)名\\Desktop\\xxx.doc";String htmlfile = "C:\\Users\\無(wú)名\\Desktop\\xxx.html";JacobUtil.wordToHtml(docfile, htmlfile);}/*** WORD轉(zhuǎn)HTML* @param docfile WORD文件全路徑* @param htmlfile 轉(zhuǎn)換后HTML存放路徑*/public static void wordToHtml(String docfile, String htmlfile) {// 啟動(dòng)word應(yīng)用程序(MicrosoftOfficeWord2003)ActiveXComponent app = new ActiveXComponent("Word.Application");System.out.println("*****正在轉(zhuǎn)換...*****");try {// 設(shè)置word應(yīng)用程序不可見(jiàn)app.setProperty("Visible", new Variant(false));// documents表示word程序的所有文檔窗口,(word是多文檔應(yīng)用程序)Dispatch docs = app.getProperty("Documents").toDispatch();// 打開(kāi)要轉(zhuǎn)換的word文件Dispatch doc = Dispatch.invoke(docs, "Open", Dispatch.Method, new Object[]{docfile, new Variant(false), new Variant(true)}, new int[1]).toDispatch();// 作為html格式保存到臨時(shí)文件Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[]{htmlfile, new Variant(WORD_HTML)}, new int[1]);// 關(guān)閉word文件Dispatch.call(doc, "Close", new Variant(false));} catch (Exception e) {e.printStackTrace();} finally {// 關(guān)閉word應(yīng)用程序app.invoke("Quit", new Variant[]{});}System.out.println("*****轉(zhuǎn)換完畢********");}}按照以上步驟配置和代碼示例,基本可以解決文檔格式居左的問(wèn)題。需要注意的是,如果遇到相關(guān)錯(cuò)誤,請(qǐng)根據(jù)錯(cuò)誤信息進(jìn)行相應(yīng)的調(diào)整。