Select to view content in your preferred language

Exporting .geodatabase dataset to Excel or CSV format

4447
7
04-28-2015 12:55 PM
HaniDraidi
Frequent Contributor

Is there a way to export a .geodatabase dataset to Excel or CSV file by android mobile application?

For example, I want to have a button in my app that exports a .geodatabase dataset stored locally on my device to Excel or CSV file and save it on the device.

If this possible, how can I do this?

Any help is appreciated!

Hani

0 Kudos
7 Replies
EricBader
Honored Contributor

Very interesting.

So there is no API in the product that does this, at least not at this time.

However, there are APIs to access the necessary geodatabase fields and values that you can take advantage of to accomplish something like this.

For example, this is what I did. It worked for me:

  1. Use Java code to create and open a file to write to
  2. Access the GeodatabaseFeatureTable you wish to export
  3. If you want the first line of your CSV file to contain the field names, get the collection of field names and write the comma-separated list of names to the file.
  4. Next, select all the records in the table and get a FeatureSet result.
  5. Iterate through the FeatureSet. For each Feature, call getAttributes(). This gives you access to each field value that you can write out. You can also check the field type if you want to do specific value handling, etc.
  6. Close the file.

Something like that. May sound like a hack, and I would share my exact code, but it is embarrassingly hackish-looking. But it worked....

Care to share more details about the need to do such a thing? Thanks!

HaniDraidi
Frequent Contributor

Thank you very much Eric for showing interest, and for your prompt input!

Actually, I am not a developer, and I think it will take me a lot of time to complete all the steps you outlined in your post. It will be highly appreciated if you share your code, so I can use it and I am sure that it will be a helpful resource for me.

Once again, thank you!

EricBader
Honored Contributor

I will share it as soon as I can clean things up....stay tuned!

0 Kudos
melodiking
Occasional Contributor

Hi Eric-

Still staying tuned...  did you get the code written for this?  I'm about to write the same code, myself.  Would be great to not have to reinvent the wheel...

-Melo

EricBader
Honored Contributor

Ah! Caught me. I will try to do this asap. I'm sorry for letting this get away!

0 Kudos
EricBader
Honored Contributor

It could go something like this. See attached. It's a Java program, but the flow will be similar. Pardon my hacks.

0 Kudos
melodiking
Occasional Contributor

Thanks!  Code was perfect.  I adapted it slightly to add Latitude and Longitude fields.  I also adapt the date field from epoch to a readable format. So I thought I'd share that code here in case it is useful to anyone.

PrintWriter writer  = new PrintWriter(file.getAbsolutePath(), "WINDOWS-1252");
writer.write("Longitude" + ",");
writer.write("Latitude" + ",");
for(com.esri.core.map.Field field: obsTable.getFields()){
     writer.write(field.getName() + ",");
}


Iterator<Object> it = objs.iterator();
while(it.hasNext()){

     GeodatabaseFeature feature = (GeodatabaseFeature)it.next();

     Point point = (Point) GeometryEngine.project(feature.getGeometry(), SpatialReference.create(102113), SpatialReference.create(4326));
          
     StringBuffer values = new StringBuffer();
     values.append(point.getX() + ",");
     values.append(point.getY() + ",");


     for(com.esri.core.map.Field f : objs.getFields()){
          if(f.getName().equals("Date")){
               values.append(epochTimeConvert(Long.parseLong(feature.getAttributeValue(f).toString()), "EEE MMM dd, yyyy hh:mm a") + ",");
          }

          else{
               values.append(feature.getAttributeValue(f) + ",");
          }

     }

     writer.println(values);
}
writer.close();
0 Kudos