在Android中将docx转换为html

时间:2015-09-03 19:41:27

标签: android ms-word apache-poi docx

我想将Word docx文档(不是doc)转换为Android中的html。我尝试使用Apache XWPF,但它使用javax.xml.stream中的本机库,这些库在Android中不可用。当我尝试使用行compile 'javax.xml.stream:stax-api:1.0-2'手动将它们包含在我的build.gradle文件中时,Android Studio给出了关于使用本机库的这个描述性错误:

trouble processing "javax/xml/stream/EventFilter.class":
Ill-advised or mistaken usage of a core class (java.* or javax.*)
when not building a core library.
This is often due to inadvertently including a core library file
in your application's project, when using an IDE (such as
Eclipse). If you are sure you're not intentionally defining a
core class, then this is the most likely explanation of what's
going on.

<Information about how to override this warning and why it's a bad idea>

If you are legitimately using some code that happens to be in a
core package, then the easiest safe alternative you have is to
repackage that code. That is, move the classes in question into
your own package namespace. This means that they will never be in
conflict with core system classes. JarJar is a tool that may help
you in this endeavor. If you find that you cannot do this, then
that is an indication that the path you are on will ultimately
lead to pain, suffering, grief, and lamentation.

当我不手动包含这些库时,我在运行时收到此异常:

 java.lang.ClassNotFoundException: Didn't find class "javax.xml.stream.XMLEventFactory" on path: DexPathList[[zip file "/data/app/<package>"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
            at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
            at org.apache.poi.openxml4j.opc.internal.marshallers.PackagePropertiesMarshaller.<clinit>(PackagePropertiesMarshaller.java:41)
            at org.apache.poi.openxml4j.opc.OPCPackage.init(OPCPackage.java:162)
            at org.apache.poi.openxml4j.opc.OPCPackage.<init>(OPCPackage.java:142)
            at org.apache.poi.openxml4j.opc.Package.<init>(Package.java:37)
            at org.apache.poi.openxml4j.opc.ZipPackage.<init>(ZipPackage.java:89)
            at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:273)
            at org.apache.poi.util.PackageHelper.open(PackageHelper.java:37)
            at org.apache.poi.xwpf.usermodel.XWPFDocument.<init>(XWPFDocument.java:123)

运行以下代码时:

XWPFDocument wordDocument = new XWPFDocument(new FileInputStream(wordFileName));

wordFileName是word文档的有效路径。

所以我的问题是:是否可以在Android上使用Apache XWPF,如果没有,我可以使用什么来转换docx文件到HTML?

1 个答案:

答案 0 :(得分:1)

我无法使Apache XWPF正常工作,但我能够使用Docx4j(Android here示例代码),这对我的目的起作用。我只需要包含该项目中的库。