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)) { ExcelApp = new Excel.ApplicationClass(); //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(); } 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)) { ExcelApp = new Excel.ApplicationClass(); //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(); } 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)) { ExcelApp = new Excel.ApplicationClass(); //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; } }
private void btnSelect_Click(object sender, EventArgs e) { ESRI.ArcGIS.Framework.IProgressDialogFactory progressDialogFactory = null; ESRI.ArcGIS.esriSystem.IStepProgressor stepProgressor = null; ESRI.ArcGIS.Framework.IProgressDialog2 progressDialog = null; // Create a CancelTracker ESRI.ArcGIS.esriSystem.ITrackCancel trackCancel = null; ISpatialFilter pSpatFilt = null; IFeatureLayer pFL = null; IFeatureCursor pFCurs = null; IFeature pFeat = null; ISimpleJunctionFeature pSimpFeat = null; // Create an edit operation enabling undo/redo try { (_app.Document as IMxDocument).FocusMap.ClearSelection(); 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 = lstJunctionLayers.Items.Count; stepProgressor.StepValue = 1; stepProgressor.Message = A4LGSharedFunctions.Localizer.GetString("SltByJctCountProc_1"); // 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("SltByJctCountProc_1"); progressDialog.Title = A4LGSharedFunctions.Localizer.GetString("SltByJctCountProc_1"); progressDialog.Animation = ESRI.ArcGIS.Framework.esriProgressAnimationTypes.esriProgressGlobe; progressDialog.ShowDialog(); for (int i = 0; i < lstJunctionLayers.Items.Count; i++) { bool boolean_Continue = trackCancel.Continue(); if (!boolean_Continue) { return; } progressDialog.Description = A4LGSharedFunctions.Localizer.GetString("SltByJctCountProc_2") + lstJunctionLayers.Items[i].ToString(); if (lstJunctionLayers.GetItemCheckState(i) == CheckState.Checked) { bool FCorLayer = true; pFL = (IFeatureLayer)Globals.FindLayer(_app, lstJunctionLayers.Items[i].ToString(), ref FCorLayer); if (pFL != null) { pSpatFilt = new SpatialFilterClass(); pSpatFilt.Geometry = _env as IGeometry; pSpatFilt.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects; pSpatFilt.GeometryField = pFL.FeatureClass.ShapeFieldName; int featCnt = pFL.FeatureClass.FeatureCount(pSpatFilt); if (featCnt > 0) { pFCurs = pFL.Search(pSpatFilt, true); pFeat = pFCurs.NextFeature(); int loopCnt = 1; while (pFeat != null) { boolean_Continue = trackCancel.Continue(); if (!boolean_Continue) { return; } stepProgressor.Message = A4LGSharedFunctions.Localizer.GetString("SltByJctCountProc_3") + loopCnt + A4LGSharedFunctions.Localizer.GetString("Of") + featCnt; if (pFeat is SimpleJunctionFeature) { pSimpFeat = (ISimpleJunctionFeature)pFeat; if (pSimpFeat.EdgeFeatureCount >= numMinEdge.Value && pSimpFeat.EdgeFeatureCount <= numMaxEdge.Value) { (_app.Document as IMxDocument).FocusMap.SelectFeature(pFL as ILayer, pFeat); } } loopCnt++; pFeat = pFCurs.NextFeature(); } } } } stepProgressor.Step(); } } catch (Exception Ex) { MessageBox.Show(A4LGSharedFunctions.Localizer.GetString("ErrorInThe") + A4LGSharedFunctions.Localizer.GetString("SltByJctCountLbl_22") + "\r\n" + Ex.Message); } finally { if (progressDialog != null) { progressDialog.HideDialog(); } progressDialogFactory = null; stepProgressor = null; progressDialog = null; trackCancel = null; pSpatFilt = null; pFL = null; if (pFCurs != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(pFCurs); } pFCurs = null; pFeat = null; pSimpFeat = null; this.Hide(); (_app.Document as IMxDocument).ActiveView.Refresh(); MessageBox.Show((_app.Document as IMxDocument).FocusMap.SelectionCount + A4LGSharedFunctions.Localizer.GetString("SltByJctCountMess_1")); } }