Writing (Word) Document

Tutorials with advanced 'difficulty' and more Lines of Code.

Writing (Word) Document

Postby idiot2k1 » Sun Sep 05, 2010 6:57 am

I am trying to conceptualize what I want to do in my application; I don't have any code yet.

Are there any tutorials / resources to read up on to create Microsoft Word type documents programmatically via Android?

Essentially I would like my program to take GPS coordinates and push them to a database. That part I can do. From there I would like to create a human readable document and export it, either as an email or uploaded to a web server. The document will have different information based on the amount of data in the database. More data would create a longer file with potentially different information being expressed.

I have seen some tutorials about creating a .txt file, but I would like to export a file with some differing justification (center / left / right) as well as changes in the font size and weights. It will pretty much be an output report of the GPS locations that would be suitable to present to my customer's bosses.

My only idea for a workaround would be to somehow create an HTML file that would be able to manipulate the text and layout to make it look more appealing. I assume there would be an easier way.

Any help is appreciated.
Posts: 9
Joined: Wed Jul 14, 2010 2:58 am


Re: Writing (Word) Document

Postby jonbonazza » Sun Sep 05, 2010 6:45 pm

First off, you need to understand how filetypes work internally.

Every file type has its own internal "format" or "structure."

Your first step is to find this structure. USUALLY there is what is called a "header" and then the actual byte representation of the content.

Here, I found this for you. It is a tad more complex than some of your more simple file types, but it's really still rather simple.

Once your have that, you basically need to create a class for this file type's header containing a data memeber for each of the pieces. Here is an example class populated with the first piece of the header:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class DocHeader
  2. {
  3.          byte[] fileId;
  5.          public DocHeader()
  6.          {
  7.                    fileId = new byte[8];
  8.          }
  9. }
Parsed in 0.011 seconds, using GeSHi

EDIT: Forgot to finish my though haha.
Anyway, once you finish populating the header, you need to make a second class containing (at minimum; more items, such as file-size, etc can be added to ease population) a pointer to the DocHeader class you just made, and an array of bytes for each sector (described in the document) of content. So for example, you might have something like this:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class DocFile
  2. {
  3.        DocHeader header;
  4.        byte[] headerSize;
  5.        byte[] sector1;
  6.        //another array of bytes for each consecutive sector you have/need
  7.        float fileSize;
  9.        public DocFile()
  10.        {
  11.              header = new Header();
  12.              headerSize = new byte[512];
  13.              //other data members will need to be initialized when they become available
  14.        }
  15. }
Parsed in 0.010 seconds, using GeSHi

To populate the content, you will need to read in the content as Strings and then convert each string into an array of bytes, storing them in their proper sector.

Once you have your entire file populated, you can use a FileOutputStream object to place it in memory with a name like, "fileName.doc"

One thing I am unsure is if this structure i for a doc file or a docx file. I didn't really read the pdf, just glanced through it when looking for specific info to give you examples. You will need to do some hunting on your part for that.

Hope this helps!
Master Developer
Master Developer
Posts: 665
Joined: Thu Jul 15, 2010 2:58 pm
Location: WV USA


Return to Advanced Tutorials

Who is online

Users browsing this forum: No registered users and 2 guests