{"id":1934,"date":"2023-04-01T09:58:38","date_gmt":"2023-04-01T01:58:38","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1934"},"modified":"2023-04-22T08:47:11","modified_gmt":"2023-04-22T00:47:11","slug":"using-java-to-connect-openoffice4","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/04\/01\/using-java-to-connect-openoffice4\/","title":{"rendered":"Java\u8fde\u63a5OpenOffice4\u7684\u4f7f\u7528"},"content":{"rendered":"<h2>\u9879\u76ee\u4f7f\u7528\u80cc\u666f<\/h2>\n<p>\u5185\u90e8\u4f7f\u7528\u7684\u6587\u4ef6\u7ba1\u7406\u7cfb\u7edf\uff0c\u5b9e\u73b0doc\uff0cdocx\u6587\u4ef6\u7684\u5728\u7ebf\u9884\u89c8\u529f\u80fd\uff1b\u6574\u4f53\u529f\u80fd\u7c7b\u4f3c\u4e8e\u77e5\u7f51\u7684\u529f\u80fd\u7f29\u51cf\u7248\uff1a\u6587\u4ef6\u7684\u7ba1\u7406\uff0c\u5728\u7ebf\u9884\u89c8\uff0c\u67e5\u91cd\u529f\u80fd\u3002\u5176\u4e2d\u8fd8\u4f7f\u7528\u5230pdf2htmlEX\u5b9e\u73b0pdf\u8f6chtml\uff0c\u6700\u7ec8\u5b9e\u73b0\u6587\u7ae0\u91cd\u590d\u90e8\u5206\u6807\u7ea2\u64cd\u4f5c\u3002<\/p>\n<p>\u5b98\u65b9\u4e0b\u8f7d\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/www.openoffice.org\/download\/\">https:\/\/www.openoffice.org\/download\/<\/a><\/p>\n<p><!-- more --><\/p>\n<h3>\u4f18\u70b9<\/h3>\n<p>doc\uff0cdocx\u683c\u5f0f\u8f6c\u5176\u5b83pdf\uff0chtml\u683c\u5f0f\u5feb\u901f\u4fbf\u6377\u3002<\/p>\n<h3>\u7f3a\u70b9<\/h3>\n<ol>\n<li>docx\u89e3\u6790\uff0c\u652f\u6301\u6709\u9650\u5f88\u591a\u6587\u672c\u683c\u5f0f\u65e0\u6cd5\u89e3\u6790\uff0c\u4f8b\u5982\u5d4c\u5165\u7684excel\u753b\u5e03\u56fe\u7b49<\/li>\n<li>\u65e0\u6cd5\u63a7\u5236\u89e3\u6790\u8f6c\u6362\u5b8c\u7684\u6837\u5f0f<\/li>\n<\/ol>\n<h2>\u670d\u52a1\u542f\u52a8<\/h2>\n<h3>Windows\u73af\u5883<\/h3>\n<p>\uff081\uff09\u4e0b\u8f7d\u5b89\u88c5\u7701\u7565\u3002\u542f\u52a8\u547d\u4ee4\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-bash\">1.\u627e\u5230\u5b89\u88c5\u76ee\u5f55\ncd  C:\\Program Files (x86)\\OpenOffice 4\\program\n\n2.\u8fd0\u884c\u542f\u52a8\u547d\u4ee4\nsoffice -headless -accept=&quot;socket,host=127.0.0.1,port=8100;urp;&quot; -nofirststartwizard<\/code><\/pre>\n<p>\u5b89\u88c5\u547d\u4ee4\u4ecb\u7ecd<\/p>\n<ul>\n<li><code>host=127.0.0.1<\/code>\u9879\u76ee\u672c\u5730\u80fd\u8bbf\u95ee\uff0c\u6539\u6210<code>host=0.0.0.0<\/code>\u53ef\u5916\u7f51\u8bbf\u95ee<\/li>\n<li><code>port=8100<\/code>\u76d1\u6d4b\u7684\u7aef\u53e3\uff0c\u9ed8\u8ba4<code>8100<\/code>\uff0c\u53ef\u4ee5\u66f4\u6539\u7aef\u53e3<\/li>\n<\/ul>\n<p>\uff082\uff09\u63d2\u4ef6\u7684\u4f7f\u7528<\/p>\n<ul>\n<li>\u672c\u5730\u4f7f\u7528<code>host=127.0.0.1<\/code>\u670d\u52a1\u548c\u63d2\u4ef6\u5728\u4e00\u53f0windows\u670d\u52a1\u5668\u4e0a<\/li>\n<li>\u63d2\u4ef6\u548c\u670d\u52a1\u5206\u5f00<code>host=0.0.0.0<\/code>\uff0c\u5f00\u653e\u7aef\u53e3\u8bbf\u95ee\uff0c\u589e\u52a0\u5165\u5e93\u89c4\u5219\u653e\u5f008100\u7aef\u53e3<\/li>\n<\/ul>\n<pre><code class=\"language-bash\">netstat -ano\n\u53ef\u4ee5\u67e5\u770b\u5230\u7aef\u53e3\u7684\u72b6\u6001,\u6709\u670d\u52a1\u7684\u7aef\u53e3\u5373\u53ef\ntelnet ip:port\n\u67e5\u8be2\u5916\u90e8\u80fd\u5426\u8bbf\u95ee<\/code><\/pre>\n<p>\u5982\u6709\u5f00\u653e\u670d\u52a1\u7684\u72b6\u6001<code>0.0.0.0:8100<\/code>\uff0c\u57fa\u672c\u5c31\u80fd\u4f7f\u7528<\/p>\n<h3>Linux\u73af\u5883<\/h3>\n<p>Linux\u73af\u5883\u4e00\u5411\u662f\u6bd4\u8f83\u5751\uff0c\u7aef\u53e3\u5f00\u653e\u8fd9\u4e9b\u540c\u7406Windows<\/p>\n<p>\u547d\u4ee4<\/p>\n<pre><code class=\"language-bash\">cd \/opt\/openoffice4\/program \n.\/soffice -headless -accept=&quot;socket,host=127.0.0.1,port=8100;urp;&quot; -nofirststartwizard &amp;\n\u6216\n\/opt\/openoffice4\/program\/soffice -headless -accept=&quot;socket,host=127.0.0.1,port=8100;urp;&quot; -nofirststartwizard &amp;<\/code><\/pre>\n<p>\u516c\u53f8\u7684\u73af\u5883\u90fd\u662f\u7528\u7684Docker\uff0c\u6240\u4ee5\u5728<code>Docker Hub<\/code>\u641c\u7d22\u4e00\u4e2a\u955c\u50cf\u5f00\u542f\u670d\u52a1\u5373\u53ef\uff0c\u5982\u679c\u89c9\u5f97\u6709\u4ec0\u4e48\u4e0d\u5bf9\uff0c\u53ef\u4ee5\u81ea\u5df1\u5b89\u88c5\u5236\u4f5c\u4e00\u4e2a\u955c\u50cf<\/p>\n<blockquote>\n<p>\u7279\u522b\u8bf4\u660e\uff1adocker \u505a\u8fd9\u7c7b\u7684\u6a21\u5757\u670d\u52a1\u662f\u975e\u5e38\u5408\u9002\u7684\uff0c\u5355\u72ec\u542f\u52a8\u8d77\u6765\uff0c\u6240\u6709\u7684\u6a21\u5757\u90fd\u53ef\u4ee5\u901a\u8fc7ip\u548c\u7aef\u53e3\u6765\u8bbf\u95ee\uff0c\u8c01\u7528\u8c01\u77e5\u9053<\/p>\n<\/blockquote>\n<p><strong>Linux\u73af\u5883\u4e0b\u7684OpenOffice\u89e3\u6790\u4e71\u7801<\/strong><\/p>\n<p>\u539f\u56e0\u662fLinux\u7f3a\u5c11Windows\u73af\u5883\u7684\u5b57\u4f53\u5e93\uff0c\u89e3\u51b3\u65b9\u6848\uff1a<\/p>\n<ul>\n<li>\u590d\u5236Windows\u7cfb\u7edf\u7684\u5b57\u4f53\u5e93<code>C:\\Windows\\Fonts<\/code>\u5230<code>\/usr\/share\/fonts<\/code>\u7136\u540e<\/li>\n<li><code>fc-cache<\/code>\u66f4\u65b0\u7f13\u5b58\uff0c\u91cd\u542f<code>OpenOffice<\/code>\uff0c\u641e\u5b9a\uff01<\/li>\n<\/ul>\n<h2>Java\u4f7f\u7528<\/h2>\n<pre><code class=\"language-java\"> \/**\n * \u5c06word\u6587\u6863\u8f6c\u6362\u6210html\u6587\u6863\n *\n * @param docFilePath \u9700\u8981\u8f6c\u6362\u7684word\u6587\u6863\n * @param filepath \u8f6c\u6362\u4e4b\u540ehtml\u7684\u5b58\u653e\u8def\u5f84\n * @return \u8f6c\u6362\u4e4b\u540e\u7684html\u6587\u4ef6\n *\/\npublic static void convert(String docFilePath, String filepath) throws Exception {\n    File docFile = new File(docFilePath);\n    \/\/ \u521b\u5efa\u4fdd\u5b58html\u7684\u6587\u4ef6\n    File htmlFile = new File(filepath);\n    \/\/ \u521b\u5efaOpenoffice\u8fde\u63a5,\u6307\u5b9a\u670d\u52a1ip \u7aef\u53e3\n    OpenOfficeConnection con = new SocketOpenOfficeConnection(&quot;192.168.0.158&quot;, 8100);\n    \/\/ \u8fde\u63a5\n    con.connect();\n    System.out.println(&quot;\u83b7\u53d6\u94fe\u63a5\u6210\u529f\uff01\uff01\uff01\uff01\uff01\uff01\uff01\uff01\uff01\uff01\uff01\uff01\uff01\uff01\uff01\uff01\uff01\uff01&quot;);\n    \/\/ \u521b\u5efa\u8f6c\u6362\u5668\n    DocumentConverter converter;\n    \/\/ \u8f6c\u6362\u6587\u6863\u95eehtml\n    try {\n        converter = new StreamOpenOfficeDocumentConverter(con);\n        converter.convert(docFile, htmlFile);\n        System.out.println(&quot;\u83b7\u53d6\u94fe\u63a5\u6210\u529f\uff01\uff01OpenOfficeDocumentConverter&quot;);\n    } catch (Exception e) {\n        converter = new OpenOfficeDocumentConverter(con);\n        converter.convert(docFile, htmlFile);\n        System.out.println(&quot;\u83b7\u53d6\u94fe\u63a5\u6210\u529f\uff01\uff01StreamOpenOfficeDocumentConverter&quot;);\n        e.printStackTrace();\n    }\n    \/\/ \u5173\u95edopenoffice\u8fde\u63a5\n    con.disconnect();\n    System.out.println(&quot;\u5173\u95ed\u8fde\u63a5\uff01\uff01&quot;);\n}<\/code><\/pre>\n<p><code>jodconverter-2.2.1<\/code>\u4e0d\u652f\u6301docx\u89e3\u6790\uff0cmaven\u4ed3\u5e93\u6ca1\u6709\uff0c\u53ef\u4ee5\u81ea\u884c\u4e0b\u8f7d<code>jodconverter-2.2.2<\/code>\u653e\u5728\u81ea\u5df1\u7684\u4ed3\u5e93\u4e2d<\/p>\n<pre><code class=\"language-xml\">&lt;!-- https:\/\/mvnrepository.com\/artifact\/com.artofsolving\/jodconverter-maven-plugin --&gt;\n&lt;dependency&gt;\n    &lt;groupId&gt;com.artofsolving&lt;\/groupId&gt;\n    &lt;artifactId&gt;jodconverter-maven-plugin&lt;\/artifactId&gt;\n    &lt;version&gt;2.2.1&lt;\/version&gt;\n&lt;\/dependency&gt;<\/code><\/pre>\n<p>\u6216\u91cd\u5199\u65b9\u6cd5\uff1a<\/p>\n<pre><code class=\"language-java\">\/**\n *\u5176\u4e2d\u4e0d\u652f\u6301docx\u89e3\u6790\uff0c\u91cd\u5199\u6e90\u7801\n *\u6ce8\u610f\u65b0\u5efa\u540c\u540d\u7684\u5305com.artofsolving.jodconverter\uff0c\u7c7b\u540d\u4fdd\u6301\u4e00\u81f4ok\n *\/\npackage com.artofsolving.jodconverter;\n\nimport java.util.ArrayList;\nimport java.util.Iterator;\nimport java.util.List;\n\npublic class BasicDocumentFormatRegistry implements DocumentFormatRegistry {\n    private List documentFormats = new ArrayList();\n\n    public BasicDocumentFormatRegistry() {\n    }\n\n    public void addDocumentFormat(DocumentFormat documentFormat) {\n        this.documentFormats.add( documentFormat );\n    }\n\n    protected List getDocumentFormats() {\n        return this.documentFormats;\n    }\n\n    @Override\n    public DocumentFormat getFormatByFileExtension(String extension) {\n        if (extension == null) {\n            return null;\n        } else {\n            if (extension.indexOf( &quot;doc&quot; ) &gt;= 0) {\n                extension = &quot;doc&quot;;\n            }\n            if (extension.indexOf( &quot;ppt&quot; ) &gt;= 0) {\n                extension = &quot;ppt&quot;;\n            }\n            if (extension.indexOf( &quot;xls&quot; ) &gt;= 0) {\n                extension = &quot;xls&quot;;\n            }\n            String lowerExtension = extension.toLowerCase();\n            Iterator it = this.documentFormats.iterator();\n            DocumentFormat format;\n            do {\n                if (!it.hasNext()) {\n                    return null;\n                }\n                format = (DocumentFormat) it.next();\n            } while (!format.getFileExtension().equals( lowerExtension ));\n\n            return format;\n        }\n    }\n\n    @Override\n    public DocumentFormat getFormatByMimeType(String mimeType) {\n        Iterator it = this.documentFormats.iterator();\n        DocumentFormat format;\n        do {\n            if (!it.hasNext()) {\n                return null;\n            }\n            format = (DocumentFormat) it.next();\n        } while (!format.getMimeType().equals( mimeType ));\n        return format;\n    }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u9879\u76ee\u4f7f\u7528\u80cc\u666f \u5185\u90e8\u4f7f\u7528\u7684\u6587\u4ef6\u7ba1\u7406\u7cfb\u7edf\uff0c\u5b9e\u73b0doc\uff0cdocx\u6587\u4ef6\u7684\u5728\u7ebf\u9884\u89c8\u529f\u80fd\uff1b\u6574\u4f53\u529f\u80fd\u7c7b\u4f3c\u4e8e\u77e5\u7f51\u7684\u529f\u80fd\u7f29\u51cf\u7248\uff1a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43],"tags":[487],"class_list":["post-1934","post","type-post","status-publish","format-standard","hentry","category-java-basic","tag-openoffice"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1934","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1934"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1934\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1934"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1934"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1934"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}