Beispiel #1
0
        public static async Task <List <Dictionary <string, Tuple <object, bool> > > > ImportFromExcel(string fileName)
        {
            var           tableName     = "ExcelData";
            var           lstDictionary = new List <Dictionary <string, Tuple <object, bool> > >();
            List <object> arguments     = new List <object>();

            arguments.Add(fileName);
            arguments.Add(tableName);
            var env            = Geoprocessing.MakeEnvironmentArray(overwriteoutput: true);
            var valueArray     = Geoprocessing.MakeValueArray(arguments.ToArray());
            var progressDialog = new ProgressDialog("Processing.. Please wait");

            progressDialog.Show();
            try
            {
                IGPResult result = await Geoprocessing.ExecuteToolAsync("ExcelToTable_conversion", valueArray, env, null, null, GPExecuteToolFlags.Default);

                if (!result.IsFailed)
                {
                    lstDictionary = await ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
                    {
                        using (Geodatabase geodatabase = new Geodatabase(FgdbFileToConnectionPath(CoreModule.CurrentProject.DefaultGeodatabasePath)))
                        {
                            QueryDef queryDef = new QueryDef
                            {
                                Tables      = tableName,
                                WhereClause = "1 = 1",
                            };

                            using (RowCursor rowCursor = geodatabase.Evaluate(queryDef, false))
                            {
                                while (rowCursor.MoveNext())
                                {
                                    var dictionary = new Dictionary <string, Tuple <object, bool> >();
                                    using (ArcGIS.Core.Data.Row row = rowCursor.Current)
                                    {
                                        for (int i = 0; i < row.GetFields().Count; i++)
                                        {
                                            var key = row.GetFields()[i].Name;
                                            var val = rowCursor.Current[i];
                                            dictionary.Add(key, Tuple.Create(val, false));
                                        }
                                    }
                                    lstDictionary.Add(dictionary);
                                }
                            }
                        }
                        progressDialog.Hide();
                        return(lstDictionary);
                    });
                }
                else
                {
                    progressDialog.Hide();
                    MessageBox.Show("ExcelToTable_conversion operation failed.");
                }
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                progressDialog.Hide();
            }
            return(lstDictionary);
        }