Load String of comma-separated-values (csv) into Quickoffice or Google Doc

1.1k views Asked by At

I am writing an Android App that collects data as a String of comma-separated-values (terminating each line with '\n') and then emails me the data once a month.

One of my beta-testers has informed me that he would like to be able to view the intermediate data between uploads. The natural way to do this (to my mind) would be to load the data to an "excel-like" app (such as QuickOffice or google doc's viewer). I tried writing the String out to a file and then loading it into QuickOffice. I can get QuickOffice to launch, but the file comes up blank. Also, when I look for the file on my SD card, I can't find it.

My code:

File root = Environment.getExternalStorageDirectory();
try
{
    BufferedWriter out = new BufferedWriter(new FileWriter(root.toString() + "/ww.csv"));
    out.write(getPreferences(MODE_PRIVATE).getString("allTrips", ""));
    out.flush();  // Probably not necessary
    out.close();
}
catch (IOException e)
{
    // TODO Auto-generated catch block
    e.printStackTrace();
}

Intent i1 = new Intent(android.content.Intent.ACTION_VIEW);
Uri data = Uri.fromFile(new File(root.toString() + "/ww.csv"));
i1.setDataAndType(data, "application/vnd.ms-excel");
startActivity(i1);

Please let me know what I'm doing wrong. I'm open to small tweaks to my code above, as well as completely different approaches that get the job done.

Before you ask, I remembered to add

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

to my manifest.

Also, I'm as happy to write to the Internal File System instead of the SD card (my data are small) but I couldn't get that to even open QuickOffice.

An example of the data:

09/19/2011,AP2,Home,GW Parkway,12:03 PM,12:41 PM,38,Dry,Day
09/20/2011,Home,AP2,MacArthur Blvd,7:18 AM,8:32 AM,74,Rain,Day
09/20/2011,AP2,Home,Rock Creek Pkwy (Blagden),4:51 PM,5:45 PM,54,Dry,Day
09/21/2011,Home,AP2,Rte 295,6:44 AM,7:36 AM,52,Rain,Day

Thanks.

UPDATE (OCT 09 2011):

Sometimes when writing code, you need to take a step back and rethink what you're trying to accomplish. All I want to do is reformat my interim data and make an easy-to-read table to the user can see the data in-between monthly uploads. I thought the easiest way to do this would be to load my .csv file into a spreadsheet. Nikolay's suggestion of using an outside library to ensure that my file was properly formatted might have worked, but I'm trying to keep this app as lean as possible, so I want to avoid using (and learning to use) external libraries unnecessarily. So, my end solution has been to write a slightly larger file to the SD card using html format and then opening the file as text/html rather than text/plain or application/vnd.ms-excel. That took about an hour of coding and has solved the issue nicely, so I'm going to consider the problem closed.

2

There are 2 answers

0
Gdalya On BEST ANSWER

At least part of the problem is file format. When I change "application/vnd.ms-excel" to "text/plain" I am able to open the data in a text file. This is less than ideal, because it's harder to read than if it opened in a spreadsheet.

I'll keep looking for a better answer.

UPDATE (OCT 09 2011):

In the end, my better answer was "Use HTML table as a work-around". It turned out to be the path of least resistance. See my updated question for a longer explanation.

0
Nikolay Elenkov On

First, you need to debug your code and figure out why the file doesn't get written to the SD card properly (or maybe you are just looking for it in the wrong place?). After you find it, mount the SD card, and try to open in Excel/Open/LibreOffice to make sure it's correctly formatted. You might want to use a CSV library, such as opencsv, to produce the CSV file. Handling spaces, escapes, etc., can be tricky, and QuickOffice may not tolerate malformed CSV.

Google Docs is another matter altogether, you need to authenticate with the user's Google account, and use the GDocs API/client library to upload the file.