14
votes

I have a very simple question: How to create .docx and .xlsx files on Android. Before someone marks this as duplicate, let me tell you that I have extensively tried creating them using Apache POI and my attempts are described in these questions:

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

Creating a .docx file using poi-ooxml jar file

However, as described in those links, I am getting the java.lang.verify error. These are the jar files I have finally imported into my reference libraries:

  • 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-schemas-3.10-FINAL-20140208.jar

  • commons-codec-1.5.jar

  • commons-logging-1.1.jar

  • junit-4.11.jar

  • log4j-1.2.13.jar

but now it doesn't compile at all, and says this on the console

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

Please advise as to what I am to do.

EDIT:

My end purpose is: I wish to create .docx and .xlsx files and add some content to them. Once the files are created, I should be able to successfully view them on the PC system which I have connected the phone to.

EDIT 2: Found a workaround to generate .xlsx files by removing all these dependencies and using jspreadsheet-1.0 jar file. But I still don't know what to do for .docx files.

UPDATED QUESTION:

It seems my problem lies with using the POI jar files which is not supported by Android. Can someone please supply me the link where I can get POI specifically for Android?

Or, can someone please tell me how to create .docx files to which I can add content, and such that they can be viewed?

4
What exactly where you trying to do when you got that error message? It looks like you were trying to add missing java or javax classes, something the Android toolchain unhelpfully tries to stop you from doing. If that's what you were doing, then just rename the needed classes where they are used, and then implement them under the new names. But if at this point all you want is a pointer to a library, that type of request has been deemed to be off topic for Stackoverflow.Chris Stratton
I am trying to generate a valid readable .docx file. Most suggest apache poi so I went ahead with it. But I am unable to generate such a readable file. Essentially I just want a way to do so, and the major part of my post describes my attempt at doing so.SoulRayder
The problem is that you do not meaningfully describe your attempt or the current problem with it., in a way that would allow anyone to assist you in solving it. Instead you've switched your question to asking for a reference to a library. Questions of that type are closed as off-topic as a matter of policy.Chris Stratton
I have described my question as meaningfully as I can in my edits and updated question subsections, so that there should be no elements of doubt. I have also updated my own progress in my attempts to solve the question. If you still have any doubt as to what my problem is, please let me know what exactly you find confusing or unclear.SoulRayder
I will definitely refine my questions accordingly. Also, I am not asking for any library reference, since I have described the libraries I myself have found on the net and tried. That first part of my updated question merely enquires whether I am to use it separately in different way or should I use a different version for Android or not. Also, that was one of the conclusions I came to after many attempts, which is why I posted it so that the experts here could show what I was still doing wrong.SoulRayder

4 Answers

3
votes

You can use docx4j to create .docx files.

docx4j is an open source (Apache v2) library for creating, editing, and saving OpenXML "packages", including docx, pptx, and xslx.

So you need to use JAXB-based Java library for Word docx.

This is a helpful tutorial for that: http://www.javacodegeeks.com/2012/07/java-word-docx-documents-with-docx4j.html

Then your next question "is that compatible with the android ? " Look at this: JAXB can be made to run on Android

I think this will help you.

2
votes

Use Apache POI library only because It is the best out there.

Google also uses it in its Quick Office Application for Android.

For the .***x files like docx and xlsx, It might have some limits on the allowed functions in Dalvik.

1
votes

normally you only must to do this:

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

But it seems that your problem is not in the code. Is in the libraries and the compilation. I'am wrong?

1
votes

Try using http://jexcelapi.sourceforge.net/

The jar file has a very simple apis, to manipulate/create Excel sheets.