用 Java 打开 Microsoft Word docx 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10320864/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Open Microsoft Word docx file with Java
提问by CodeGuy
How can I open a Microsoft Word docx file in Java? furthermore, how can I open it if it is password protected?
如何在 Java 中打开 Microsoft Word docx 文件?此外,如果它受密码保护,我该如何打开它?
For instance,
例如,
File f = new File("hello.docx");
Please try to avoid responding with things such as "you shouldn't do this." I have a good reason for this, so please stick to the question when you answer. thanks a lot!
请尽量避免回答诸如“您不应该这样做”之类的问题。我有一个很好的理由,所以请在回答时坚持这个问题。多谢!
回答by Piotr Kochański
There is Apache POIproject for working with MS Office files. DOCX file is just a zip file with series of XML files inside, so you can unzip the file and work with XML. The XML spec (Open XML) is known.
有用于处理 MS Office 文件的Apache POI项目。DOCX 文件只是一个 zip 文件,其中包含一系列 XML 文件,因此您可以解压缩该文件并使用 XML。XML 规范(Open XML)是已知的。
回答by Dan W
I haven't personally used it, but it looks like Apache POI will work for you: http://poi.apache.org/
我没有亲自使用它,但看起来 Apache POI 对你有用:http: //poi.apache.org/
回答by Edgar Muniz Berlinck
You can use docx4j too. http://www.docx4java.org/trac/docx4j
您也可以使用 docx4j。http://www.docx4java.org/trac/docx4j
回答by yams
I have used both docx4j and Apache's POI libraries, if you are working with .docx I would recommend .docx4j. Automated alot of the process of creating a .docx.
There is a great exmaple here : http://java.dzone.com/articles/create-complex-word-docxon how to create a .docx using the docx4j package.
我使用过 docx4j 和 Apache 的 POI 库,如果您使用 .docx,我会推荐 .docx4j。自动化了很多创建 .docx 的过程。
这里有一个很好的例子:http: //java.dzone.com/articles/create-complex-word-docx关于如何使用 docx4j 包创建 .docx。
回答by JasonPlutext
If the docx is password protected, it won't be a zip file. It will be a compound file. See Overview of Protected Office Open XML Documents
如果 docx 受密码保护,则它不会是 zip 文件。它将是一个复合文件。请参阅受保护的 Office Open XML 文档概述
To read a compound file in Java, use POIFS. POIFS is part of POI (docx4j uses it as well, so if you download the docx4j distribution, you'll be able to use the POIFS API)
要在 Java 中读取复合文件,请使用PIOFS。POIFS 是 POI 的一部分(docx4j 也使用它,因此如果您下载 docx4j 发行版,您将能够使用 POIFS API)
Once you have decrypted the encrypted package, you can read it using docx4j or POI.
解密加密包后,您可以使用 docx4j 或 POI 读取它。
Edit: OK, now docx4j can handle password-protected docxautomatically.
编辑:好的,现在 docx4j 可以自动处理受密码保护的 docx。
回答by JasonPlutext
Have you tried to open it using the Open Office api? It can work with a lot of documents types.
您是否尝试过使用Open Office api打开它?它可以处理许多文档类型。
I used it with MS Excel files .xls ( old version ) format.
我将它与 MS Excel 文件 .xls(旧版本)格式一起使用。
Hope this can help you.
希望这可以帮到你。