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:) CheersMDruzgala