Select to view content in your preferred language

Export selection to Excel C#

1027
3
05-03-2011 12:58 AM
FaizanTayyab
Deactivated User
Hi All,

I am trying to create an add-in button that allows the selected features to be exported out into an excel work book. I have been able to loop through the features and get the selected features but am not sure how to write out its attribute table to an excel workbook.

I have unsucessfully tried the ExcelWorkspaceFactory. Anybody knows how to solve this one.

Any help will be appreciated.

Regards
0 Kudos
3 Replies
JamesCrandall
MVP Alum
Have a look at this older thread:

http://forums.esri.com/Thread.asp?c=93&f=992&t=227281

Otherwise, you will have to write your attributes to an ADO.NET DataTable/Dataset then either setup an OleDbDataAdapter to run the updates/inserts to Excel or do this via Excel Automation.

Sorry -- I don't have much sample code on this.

Good luck!
0 Kudos
DonaldGreen
New Contributor
You can also use a 3rd party component, for example look at this Excel Writer to work with excel files in C#.
0 Kudos
MarcinDruzgala
Frequent Contributor
Hi All,

I am trying to create an add-in button that allows the selected features to be exported out into an excel work book. I have been able to loop through the features and get the selected features but am not sure how to write out its attribute table to an excel workbook.

I have unsucessfully tried the ExcelWorkspaceFactory. Anybody knows how to solve this one.

Any help will be appreciated.

Regards


Take a look at this: NPOI, it's really easy to write data to excel with this libraries. Lately i did a utility for arccatalog that is doing exactly the same thing you are willing to do . Sample:

QueryFilter queryFilter = new QueryFilterClass();
queryFilter.WhereClause = "OBJECTID <> 0";
int rowIndex = featClass.FeatureCount(queryFilter);
int columnIndex = featClass.Fields.FieldCount;

if (rowIndex > 0)
{
 string[,] valuesArray = new string[rowIndex, columnIndex];
 for (int i = 0; i < rowIndex; i++)
 {
  IFeature feat = featClass.GetFeature(i + 1);

  for (int j = 0; j < feat.Fields.FieldCount; j++)
  {
   string fieldValue = feat.get_Value(j).ToString();
   valuesArray.SetValue(fieldValue, i, j);
  }
 }
 //more logic
}


You will have to adjust it to your needs since i wanted all feature and you want only the selected features to be write into excel.
It's a bit tricky later since you will have to iterate through the 2-dimensional array to write it down to excel, but you will figure it out:)

Cheers
MDruzgala
0 Kudos