public void ExportSelectedRecordsToExcel()
{
ESRI.ArcGIS.Framework.IProgressDialogFactory progressDialogFactory = null;
ESRI.ArcGIS.esriSystem.IStepProgressor stepProgressor = null;
ESRI.ArcGIS.Framework.IProgressDialog2 progressDialog = null;
IMxDocument mxdoc = null;
IMap map = null;
IStandaloneTableCollection standTabColl = null;
IStandaloneTable standTable = null;
ITableSelection tableSel = null;
IEnumFeature enumFeat = null;
IFeature feat = null;
IFeatureSelection featSel = null;
ITable openTable = null;
object missing = null;
//object fileName = null;
object newTemplate = null;
object docType = null;
object isVisible = null;
UID geoFeatureLayerID = null;
IEnumLayer enumLayer = null;
IFeatureLayer featlayer = null;
try
{
mxdoc = (IMxDocument)_app.Document;
map = mxdoc.FocusMap;
long standTableCnt;
int i = 0;
bool selectionInTable = false;
missing = System.Reflection.Missing.Value;
// fileName = "normal.dot";
newTemplate = false;
docType = 0;
isVisible = true;
//Get enumeration of feature layers
geoFeatureLayerID = new UIDClass();
geoFeatureLayerID.Value = "{E156D7E5-22AF-11D3-9F99-00C04F6BC78E}";
enumLayer = map.get_Layers(geoFeatureLayerID, true);
enumLayer.Reset();
standTabColl = (IStandaloneTableCollection)map;
standTableCnt = standTabColl.StandaloneTableCount;
enumFeat = (IEnumFeature)map.FeatureSelection;
enumFeat.Reset();
feat = enumFeat.Next();
if (standTableCnt > 0)
{
for (int j = 0; j < standTableCnt; j++)
{
standTable = standTabColl.get_StandaloneTable(j);
if (standTable.Valid)
{
openTable = (ITable)standTable;
tableSel = (ITableSelection)openTable;
if (tableSel.SelectionSet.Count > 0)
{
selectionInTable = true;
break;
}
}
}
}
if ((selectionInTable == false) && (feat == null))
{
MessageBox.Show(A4LGSharedFunctions.Localizer.GetString("DataToolsMess_1") + Environment.NewLine + A4LGSharedFunctions.Localizer.GetString("DataToolsMess_2"), messageBoxHeader);
return;
}
// Create a CancelTracker
ESRI.ArcGIS.esriSystem.ITrackCancel trackCancel = new ESRI.ArcGIS.Display.CancelTrackerClass();
// Set the properties of the Step Progressor
System.Int32 int32_hWnd = _app.hWnd;
progressDialogFactory = new ESRI.ArcGIS.Framework.ProgressDialogFactoryClass();
stepProgressor = progressDialogFactory.Create(trackCancel, int32_hWnd);
stepProgressor.MinRange = 0;
stepProgressor.MaxRange = mxdoc.FocusMap.SelectionCount;
stepProgressor.StepValue = 1;
stepProgressor.Message = A4LGSharedFunctions.Localizer.GetString("ExportXL");
// Create the ProgressDialog. This automatically displays the dialog
progressDialog = (ESRI.ArcGIS.Framework.IProgressDialog2)stepProgressor; // Explict Cast
// Set the properties of the ProgressDialog
progressDialog.CancelEnabled = true;
progressDialog.Description = A4LGSharedFunctions.Localizer.GetString("ExportAsset") + "1" + A4LGSharedFunctions.Localizer.GetString("Of") + mxdoc.FocusMap.SelectionCount.ToString() + ".";
progressDialog.Title = A4LGSharedFunctions.Localizer.GetString("ExportXL");
progressDialog.Animation = ESRI.ArcGIS.Framework.esriProgressAnimationTypes.esriProgressGlobe;
progressDialog.ShowDialog();
if ((feat != null) && (selectionInTable == true))
{
try
{
ExcelApp = new Excel.ApplicationClass();
}
catch
{
MessageBox.Show("Failed to export to Excel, please ensure MS office and Excel is installed");
return;
}
//Delete default worksheets
ExcelApp.DisplayAlerts = false;
try
{
objBook = ExcelApp.Workbooks.Add(missing);
}
catch
{
MessageBox.Show("Failed to export to Excel, please ensure MS office and Excel is installed");
return;
}
//objBook = ExcelApp.Workbooks.get_Item(1);
for (int m = objBook.Sheets.Count; m > 1; m--)
{
objSheet = (Excel.Worksheet)objBook.Sheets.get_Item(m);
objSheet.Delete();
}
featlayer = (IFeatureLayer)enumLayer.Next();
while (featlayer != null)
{
if ((featlayer.Valid) && (featlayer.Selectable))
{
featSel = (IFeatureSelection)featlayer;
if (featSel.SelectionSet.Count > 0)
{
if (ExportLayer(objBook, mxdoc, featlayer, ref progressDialog, ref stepProgressor, ref trackCancel) == false)
{
return;
}
}
}
featlayer = (IFeatureLayer)enumLayer.Next();
}
for (i = 0; i < standTableCnt; i++)
{
standTable = standTabColl.get_StandaloneTable(i);
if (standTable.Valid)
{
openTable = (ITable)standTable;
tableSel = (ITableSelection)openTable;
if (tableSel.SelectionSet.Count > 0)
{
if (ExportTable(objBook, mxdoc, standTable, ref progressDialog, ref stepProgressor, ref trackCancel) == false)
{
return;
}
}
}
}
}
else if ((feat != null) && (selectionInTable == false))
{
try
{
ExcelApp = new Excel.ApplicationClass();
}
catch
{
MessageBox.Show("Failed to export to Excel, please ensure MS office and Excel is installed");
return;
}
//Delete default worksheets
ExcelApp.DisplayAlerts = false;
try
{
objBook = ExcelApp.Workbooks.Add(missing);
}
catch
{
MessageBox.Show("Failed to export to Excel, please ensure MS office and Excel is installed");
return;
}
//objBook = ExcelApp.Workbooks.get_Item(1);
for (int m = objBook.Sheets.Count; m > 1; m--)
{
objSheet = (Excel.Worksheet)objBook.Sheets.get_Item(m);
objSheet.Delete();
}
ILayer pTLay = enumLayer.Next();
while (pTLay != null)
{
if (pTLay is IFeatureLayer)
{
featlayer = (IFeatureLayer)pTLay;
if ((featlayer.Valid))
{
featSel = (IFeatureSelection)featlayer;
if (featSel.SelectionSet.Count > 0)
{
if (ExportLayer(objBook, mxdoc, featlayer, ref progressDialog, ref stepProgressor, ref trackCancel) == false)
{
return;
}
}
}
}
pTLay = enumLayer.Next();
}
pTLay = null;
}
else if ((feat == null) && (selectionInTable == true))
{
try
{
ExcelApp = new Excel.ApplicationClass();
}
catch
{
MessageBox.Show("Failed to export to Excel, please ensure MS office and Excel is installed");
return;
}
//Delete default worksheets
ExcelApp.DisplayAlerts = false;
objBook = ExcelApp.Workbooks.Add(missing);
//objBook = ExcelApp.Workbooks.get_Item(1);
for (int m = objBook.Sheets.Count; m > 1; m--)
{
objSheet = (Excel.Worksheet)objBook.Sheets.get_Item(m);
objSheet.Delete();
}
for (i = 0; i < standTableCnt; i++)
{
standTable = standTabColl.get_StandaloneTable(i);
if (standTable.Valid)
{
openTable = (ITable)standTable;
tableSel = (ITableSelection)openTable;
if (tableSel.SelectionSet.Count > 0)
{
if (ExportTable(objBook, mxdoc, standTable, ref progressDialog, ref stepProgressor, ref trackCancel) == false)
{
return;
}
}
}
}
}
if (objBook.Sheets.Count > 1)
{
objSheet = (Excel.Worksheet)objBook.Sheets.get_Item(1);
objSheet.Delete();
}
//objSheet = (Excel.Worksheet)objBook.Sheets["Sheet2"];
//objSheet.Delete();
//objSheet = (Excel.Worksheet)objBook.Sheets["Sheet3"];
//objSheet.Delete();
ExcelApp.DisplayAlerts = true;
//Make the first sheet active
objSheet = (Excel.Worksheet)objBook.Sheets.get_Item(1) as Excel.Worksheet;
(objSheet as Microsoft.Office.Interop.Excel._Worksheet).Activate();
//Make Excel visible
ExcelApp.Visible = true;
return;
}
catch (Exception ex)
{
if (ExcelApp != null)
ExcelApp = null;
//ExcelApp.Visible = true;
MessageBox.Show("ExportSelectedRecordsToExcel\n" + ex.Message, ex.Source);
return;
}
finally
{
if (progressDialog != null)
progressDialog.HideDialog();
progressDialogFactory = null;
stepProgressor = null;
progressDialog = null;
mxdoc = null;
map = null;
standTabColl = null;
standTable = null;
tableSel = null;
enumFeat = null;
feat = null;
featSel = null;
openTable = null;
missing = null;
//fileName = null;
newTemplate = null;
docType = null;
isVisible = null;
geoFeatureLayerID = null;
enumLayer = null;
featlayer = null;
}
}