开发者社区> 问答> 正文

java如何不用openoffice将docx转doc

急求 java将docx转doc不用openoffice,工作礼物。哪位高人能告诉下,万分感谢。

展开
收起
蛮大人123 2016-06-12 11:55:04 4485 0
2 条回答
写回答
取消 提交回答
  • 你好,请问后续是是用的什么 看下面是openoffice

    2020-07-15 16:44:30
    赞同 展开评论 打赏
  • 我说我不帅他们就打我,还说我虚伪
    /**
         * 将Office文档转换为PDF. 运行该函数需要用到OpenOffice, OpenOffice下载地址为
         * http://www.openoffice.org/
         * 
         * <pre>
         * 方法示例:
         * String sourcePath = "F:\\office\\source.doc";
         * String destFile = "F:\\pdf\\dest.pdf";
         * Converter.office2PDF(sourcePath, destFile);
         * </pre>
         * 
         * @param sourceFile
         *            源文件, 绝对路径. 可以是Office2003-2007全部格式的文档, Office2010的没测试. 包括.doc,
         *            .docx, .xls, .xlsx, .ppt, .pptx等. 示例: F:\\office\\source.doc
         * @param destFile
         *            目标文件. 绝对路径. 示例: F:\\pdf\\dest.pdf
         * @return 操作成功与否的提示信息. 如果返回 -1, 表示找不到源文件, 或url.properties配置错误; 如果返回 0,
         *         则表示操作成功; 返回1, 则表示转换失败
         */
        public static int office2PDF(String sourceFile, String destFile) {
            try {
                File inputFile = new File(sourceFile);
                if (!inputFile.exists()) {
                    return -1;// 找不到源文件, 则返回-1
                }
    
                // 如果目标路径不存在, 则新建该路径
                File outputFile = new File(destFile);
                if (!outputFile.getParentFile().exists()) {
                    outputFile.getParentFile().mkdirs();
                }
                
                // connect to an OpenOffice.org instance running on port 8100
                OpenOfficeConnection connection = new SocketOpenOfficeConnection(
                        "127.0.0.1", 8100);
                connection.connect();
    
                // convert
                DocumentConverter converter = new OpenOfficeDocumentConverter(
                        connection);
                converter.convert(inputFile, outputFile);
    
                // close the connection
                connection.disconnect();
    
                return 0;
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                return -1;
            } catch (ConnectException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
    
            return 1;
        }
    2019-07-17 19:33:40
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
Spring Cloud Alibaba - 重新定义 Java Cloud-Native 立即下载
The Reactive Cloud Native Arch 立即下载
JAVA开发手册1.5.0 立即下载