如何在Android上创建.docx文件和.xlsx文件

时间:2014-02-19 10:21:14

标签: java android eclipse excel apache-poi

我有一个非常简单的问题:如何在Android上创建 .docx .xlsx 文件。在有人将此标记为重复之前,请允许我告诉您,我已经尝试使用Apache POI创建它们,并且在这些问题中描述了我的尝试:

java.lang.NoClassDefFoundError while creating a .xlsx file using Apache POI

Creating a .docx file using poi-ooxml jar file

但是,如这些链接所述,我收到 java.lang.verify错误。这些是我最终导入到参考库中的jar文件:

  • DOM4J-1.6.1.jar

  • POI-3.10-FINAL-20140208.jar

  • POI-OOXML-3.10-FINAL-20140208.jar

  • STAX-API-1.0.1.jar

  • 的xmlbeans-2.3.0.jar

  • POI-OOXML-架构 - 3.10-FINAL-20140208.jar

  • 公地编解码器-1.5.jar

  • 共享记录-1.1.jar

  • 的junit-4.11.jar

  • 的log4j-1.2.13.jar

但现在根本没有编译,并在控制台上说这个

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.

However, you might actually be trying to define a class in a core
namespace, the source of which you may have taken, for example,
from a non-Android virtual machine project. This will most
assuredly not work. At a minimum, it jeopardizes the
compatibility of your app with future versions of the platform.
It is also often of questionable legality.

If you really intend to build a core library -- which is only
appropriate as part of creating a full virtual machine
distribution, as opposed to compiling an application -- then use
the "--core-library" option to suppress this error message.

If you go ahead and use "--core-library" but are in fact
building an application, then be forewarned that your application
will still fail to build or run, at some point. Please be
prepared for angry customers who find, for example, that your
application ceases to function once they upgrade their operating
system. You will be to blame for this problem.

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.

[2014-02-19 16:59:24 - test] Dx 1 error; aborting
[2014-02-19 16:59:24 - test] Conversion to Dalvik format failed with error 1

请告知我要做什么。

修改

我的最终目的是:我希望创建 .docx和.xlsx文件,并添加一些内容。创建文件后,我应该能够在已连接手机的 PC系统上成功查看

编辑2 :找到一种解决方法,通过删除所有这些依赖项并使用 jspreadsheet-1.0 .xlsx 文件strong> jar文件。但我仍然不知道如何处理 .docx 文件。

更新的问题:

似乎我的问题在于使用Android不支持的POI jar文件。有人可以提供链接,我可以获得 Android 的POI 特别

或者,有人可以告诉我如何创建 .docx 文件,我可以添加内容,以便查看这些文件吗?

4 个答案:

答案 0 :(得分:3)

您可以使用docx4j创建 .docx 文件。

docx4j是一个开源(Apache v2)库,用于创建,编辑和保存OpenXML“软件包”,包括docx,pptx和xslx。

所以你需要使用JAXB-based Java library for Word docx

这是一个有用的教程: http://www.javacodegeeks.com/2012/07/java-word-docx-documents-with-docx4j.html

然后您的下一个问题“是否与Android兼容?”请看:JAXB can be made to run on Android

我认为这会对你有帮助。

答案 1 :(得分:2)

仅使用Apache POI library因为它是最好的。

Google还在其Quick Office Android应用程序中使用它。

对于像docx和xlsx这样的。*** x文件,它可能对Dalvik中允许的函数有一些限制。

答案 2 :(得分:1)

通常你只需要这样做:

FileOutputStream archivoSalida;
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sht = wb.createSheet("Conciliacion");
...
...
archivoSalida = new FileOutputStream(fileDestination);
wb.write(archivoSalida);
archivoSalida.close();

但似乎您的问题不在代码中。是在库和编译中。 我错了吗?

答案 3 :(得分:1)

尝试使用http://jexcelapi.sourceforge.net/

jar文件有一个非常简单的api,用于操作/创建Excel工作表。