超实用的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线程与ActiveXComponent
        ComThread.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");
            // 释放线程与ActiveXComponent
            app.invoke("Quit", new Variant[]{});
            ComThread.Release();
        }
    }

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

4、注意,只在window系统可用
java调用windwos系统动态库,例:
下载jacob.rar,解压出jacob.dll和jacob.jar

然后:

  1. 把jacob.dll在 C:\Program Files\Java\jdk1.5.0_08\bin、C:\Program Files\Java\jdk1.5.0_08\jre\bin、C:\WINDOWS\system32 目录下各.放一份
  2. 把jacob.jar放入 项目的lib包下,并且在“java构建路径”中也要加载此jar包。.
  3. 运行项目即可编译和运行通过.

二、跨平台的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了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值