日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

超实用的doc 转 docx

發布時間:2023/12/16 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 超实用的doc 转 docx 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、可用的doc轉docx(window)

1、采用 java開發語言

<!--添加本地的jacob.jar包--><dependency><groupId>com.jacob</groupId><artifactId>jacob</artifactId><version>1.20</version><scope>system</scope><systemPath>${project.basedir}/src/main/resources/lib/jacob-1.20.jar</systemPath></dependency>

2、項目代碼:

/*** 根據格式類型轉換doc文件** @param srcPath doc path 源文件* @param descPath the docx path 目標文件* @param fmt fmt 所轉格式* 格式大全:前綴對應以下方法的fmt值* 0:Microsoft Word 97 - 2003 文檔 (.doc)* 1:Microsoft Word 97 - 2003 模板 (.dot)* 2:文本文檔 (.txt)* 3:文本文檔 (.txt)* 4:文本文檔 (.txt)* 5:文本文檔 (.txt)* 6:RTF 格式 (.rtf)* 7:文本文檔 (.txt)* 8:HTML 文檔 (.htm)(帶文件夾)* 9:MHTML 文檔 (.mht)(單文件)* 10:MHTML 文檔 (.mht)(單文件)* 11:XML 文檔 (.xml)* 12:Microsoft Word 文檔 (.docx)* 13:Microsoft Word 啟用宏的文檔 (.docm)* 14:Microsoft Word 模板 (.dotx)* 15:Microsoft Word 啟用宏的模板 (.dotm)* 16:Microsoft Word 文檔 (.docx)* 17:PDF 文件 (.pdf)* 18:XPS 文檔 (.xps)* 19:XML 文檔 (.xml)* 20:XML 文檔 (.xml)* 21:XML 文檔 (.xml)* 22:XML 文檔 (.xml)* 23:OpenDocument 文本 (.odt)* 24:WTF 文件 (.wtf)* @return the file* @throws Exception the exception*/public static File convertDocFmt(String srcPath, String descPath, int fmt) throws Exception {long start = System.currentTimeMillis();fmt=12;// 實例化ComThread線程與ActiveXComponentComThread.InitSTA();ActiveXComponent app = new ActiveXComponent("Word.Application");try {// 文檔隱藏時進行應用操作app.setProperty("Visible", new Variant(false));app.setProperty("DisplayAlerts", new Variant(false));// 實例化模板Document對象Dispatch document = app.getProperty("Documents").toDispatch();// 打開Document進行另存為操作Dispatch doc = Dispatch.invoke(document, "Open", Dispatch.Method,new Object[]{srcPath, new Variant(true), new Variant(true)}, new int[1]).toDispatch();Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[]{descPath, new Variant(fmt)}, new int[1]);Dispatch.call(doc, "Close", new Variant(false));return new File(descPath);} catch (Exception e){throw e;} finally {long end = System.currentTimeMillis();System.out.println("轉換完成,用時:"+ (end - start) +"ms");// 釋放線程與ActiveXComponentapp.invoke("Quit", new Variant[]{});ComThread.Release();}}

3、引入本地包,包下載地址:GitHub

4、注意,只在window系統可用
java調用windwos系統動態庫,例:
下載jacob.rar,解壓出jacob.dll和jacob.jar

然后:

  • 把jacob.dll在 C:\Program Files\Java\jdk1.5.0_08\bin、C:\Program Files\Java\jdk1.5.0_08\jre\bin、C:\WINDOWS\system32 目錄下各.放一份
  • 把jacob.jar放入 項目的lib包下,并且在“java構建路徑”中也要加載此jar包。.
  • 運行項目即可編譯和運行通過.
  • 二、跨平臺的doc 轉 docx

    1、引入pom.xml ,引入本地包,具體操作請跳轉:第三方包引入

    <dependency><groupId>com.aspose</groupId><artifactId>aspose-words</artifactId><version>19.2</version><scope>system</scope><systemPath>${project.basedir}/src/main/resources/lib/aspose-words-19.2-jdk16.jar</systemPath></dependency>

    2、實現代碼,很簡單

    /*** `doc` 轉 `docx`* @param fileRootPath: 文件根位置* @param wordFileName: 需轉換的`doc`文件(絕對路徑)* @param wordFileNameNew: 待生成的的`docx`文件名* @return 生成的`docx`文件路徑* @author shaoyy* @date 2022/10/25 17:00*/private static String doc2DocxFile(String fileRootPath, String wordFileName, String wordFileNameNew) throws Exception {// word 文件路徑final String wordFilePathNew = fileRootPath + "/" + wordFileNameNew;// 將`doc`格式轉換成`docx`Document document = new Document(wordFileName);document.save(wordFilePathNew);// 返回生成的`docx`文件路徑return wordFilePathNew;}

    3、查看文件是否為真實的docx
    將文件擴展名改為.zip,看到有word文件夾就是真實的docx了

    總結

    以上是生活随笔為你收集整理的超实用的doc 转 docx的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。