using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Text;using System.Windows;using System.Windows.Controls;using System.Windows.Data;using ESRI.ArcGIS.Client;using ESRI.ArcGIS.Client.Tasks;namespace ExportTest{ public partial class MainPage : UserControl { public MainPage() { InitializeComponent(); } void QueryButton_Click(object sender, RoutedEventArgs e) { QueryTask queryTask = new QueryTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/5"); queryTask.ExecuteCompleted += QueryTask_ExecuteCompleted; queryTask.Failed += QueryTask_Failed; ESRI.ArcGIS.Client.Tasks.Query query = new ESRI.ArcGIS.Client.Tasks.Query(); query.Text = StateNameTextBox.Text; query.OutFields.Add("*"); queryTask.ExecuteAsync(query); } void QueryTask_ExecuteCompleted(object sender, ESRI.ArcGIS.Client.Tasks.QueryEventArgs args) { FeatureSet featureSet = args.FeatureSet; if (featureSet != null && featureSet.Features.Count > 0) { QueryDetailsDataGrid.ItemsSource = args.FeatureSet; } else MessageBox.Show("No features returned from query"); } private void QueryTask_Failed(object sender, TaskFailedEventArgs args) { MessageBox.Show("Query execute error: " + args.Error); } private void Excel_Click(object sender, RoutedEventArgs e) { DataGridExtensions.ExportDataGrid(QueryDetailsDataGrid, null); } } public static class DataGridExtensions { public static void Export(this DataGrid dg, List<string> excludedColumns) { ExportDataGrid(dg, excludedColumns); } public static void ExportDataGrid(DataGrid dGrid, List<string> excludedColumns) { SaveFileDialog objSFD = new SaveFileDialog() { DefaultExt = "csv", Filter = "CSV Files (*.csv)|*.csv|Excel XML (*.xml)|*.xml|All files (*.*)|*.*", FilterIndex = 1 }; if (objSFD.ShowDialog() == true) { string strFormat = objSFD.SafeFileName.Substring(objSFD.SafeFileName.IndexOf('.') + 1).ToUpper(); StringBuilder strBuilder = new StringBuilder(); if (dGrid.ItemsSource == null) return; if (dGrid.HeadersVisibility == DataGridHeadersVisibility.Column || dGrid.HeadersVisibility == DataGridHeadersVisibility.All) { var listFields = from col in dGrid.Columns where excludedColumns == null || !excludedColumns.Contains(col.Header.ToString()) select col.Header.ToString(); BuildStringOfRow(strBuilder, listFields, strFormat); } foreach (Graphic data in dGrid.ItemsSource) { var listFields = from col in dGrid.Columns where excludedColumns == null || !excludedColumns.Contains(col.Header.ToString()) select GetValue(col.GetCellContent(data)); BuildStringOfRow(strBuilder, listFields, strFormat); } StreamWriter sw = new StreamWriter(objSFD.OpenFile()); if (strFormat == "XML") { //Let us write the headers for the Excel XML sw.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>"); sw.WriteLine("<?mso-application progid=\"Excel.Sheet\"?>"); sw.WriteLine("<Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\">"); sw.WriteLine("<DocumentProperties xmlns=\"urn:schemas-microsoft-com:office:office\">"); sw.WriteLine("<Author>Arasu Elango</Author>"); sw.WriteLine("<Created>" + DateTime.Now.ToLocalTime().ToLongDateString() + "</Created>"); sw.WriteLine("<LastSaved>" + DateTime.Now.ToLocalTime().ToLongDateString() + "</LastSaved>"); sw.WriteLine("<Company>Atom8 IT Solutions (P) Ltd.,</Company>"); sw.WriteLine("<Version>12.00</Version>"); sw.WriteLine("</DocumentProperties>"); sw.WriteLine("<Worksheet ss:Name=\"Silverlight Export\" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\">"); sw.WriteLine("<Table>"); } sw.Write(strBuilder.ToString()); if (strFormat == "XML") { sw.WriteLine("</Table>"); sw.WriteLine("</Worksheet>"); sw.WriteLine("</Workbook>"); } sw.Close(); } } private static string GetValue(FrameworkElement frameworkElement) { return frameworkElement == null ? null : (frameworkElement is TextBlock ? (frameworkElement as TextBlock).Text : frameworkElement.ToString()); } private static void BuildStringOfRow(StringBuilder strBuilder, IEnumerable<string> lstFields, string strFormat) { switch (strFormat) { case "XML": strBuilder.AppendLine("<Row>"); strBuilder.AppendLine(String.Join("", lstFields.ToArray())); strBuilder.AppendLine("</Row>"); break; case "CSV": strBuilder.AppendLine(String.Join(",", lstFields.ToArray())); break; } } private static string FormatField(string data, string format) { switch (format) { case "XML": return String.Format("<Cell><Data ss:Type=\"String\">{0}</Data></Cell>", data); case "CSV": return String.Format("\"{0}\"", data.Replace("\"", "\"\"\"").Replace("\n", "").Replace("\r", "")); } return data; } }}
private static string GetValue(DataGridColumn col, Graphic graphic) { if (col is DataGridBoundColumn) { var path = (col as DataGridBoundColumn).Binding.Path.Path; var att = Regex.Replace(path, ".*\\[(.*)\\]", "$1"); return graphic.Attributes[att] == null ? string.Empty : graphic.Attributes[att].ToString(); } return string.Empty; }
var listFields = from col in dGrid.Columns where excludedColumns == null || !excludedColumns.Contains(col.Header.ToString()) select GetValue(col.GetCellContent(data));
foreach (Graphic data in dGrid.ItemsSource) { lstFields.Clear(); foreach (DataGridColumn col in dGrid.Columns) { string tempString = null; if (col is DataGridTextColumn) { tempString = ((DataGridTextColumn)col).Binding.Path.Path.ToString(); } else if (col is DataGridTemplateColumn) { //This is the case when the column has a hyperlink in the cell template HyperlinkButton innerControl = ((DataGridTemplateColumn)col).CellTemplate.LoadContent() as HyperlinkButton; if (innerControl != null) tempString = innerControl.GetBindingExpression(HyperlinkButton.ContentProperty).ParentBinding.Path.Path.ToString(); else tempString = string.Empty; } string attributeName = tempString.Replace("Attributes[", "").Replace("]", ""); string strValue = string.Empty; if (excludedColumns != null) { foreach (string columnName in excludedColumns) { if (col.Header.ToString() == columnName) { continue; } else { if (data.Attributes[attributeName] != null) strValue = data.Attributes[attributeName].ToString(); lstFields.Add(FormatField(strValue, strFormat)); } } } else { if (data.Attributes[attributeName] != null) strValue = data.Attributes[attributeName].ToString(); lstFields.Add(FormatField(strValue, strFormat)); } } BuildStringOfRow(strBuilder, lstFields, strFormat); }
private static string GetValue(FrameworkElement frameworkElement) { return frameworkElement == null ? null frameworkElement is TextBlock ? (frameworkElement as TextBlock).Text : frameworkElement.ToString()); }
private static string GetValue(DataGridColumn col, Graphic graphic) { if (col is DataGridBoundColumn) { var path = (col as DataGridBoundColumn).Binding.Path.Path; var att = Regex.Replace(path, ".*\\[(.*)\\]", "$1"); return graphic.Attributes[att].ToString(); } return string.Empty; }
if (col is DataGridBoundColumn) { var path = (col as DataGridBoundColumn).Binding.Path.Path; var att = Regex.Replace(path, ".*\\[(.*)\\]", "$1"); return graphic.Attributes[att].ToString(); } return string.Empty;
string strValue = string.Empty; strValue = data.Attributes[field].ToString();
string strValue = data.Attributes[field] != null ? data.Attributes[field].ToString() : string.Empty;
string strValue = string.Empty; strValue = data.Attributes[field.Header.ToString()].ToString();
string strValue = data.Attributes[field.Header.ToString()] != null ? data.Attributes[field.Header.ToString()].ToString() : string.Empty;
public static class DataGridExtensions { //added the extra parameter public static void Export(this FeatureDataGrid dg, IEnumerable<Graphic> graphics, List<string> excludedColumns) { ExportDataGrid(dg, graphics, excludedColumns); } //added the extra parameter public static void ExportDataGrid(FeatureDataGrid dGrid, IEnumerable<Graphic> graphics, List<string> excludedColumns) { SaveFileDialog objSFD = new SaveFileDialog() { DefaultExt = "csv", Filter = "CSV Files (*.csv)|*.csv|Excel XML (*.xml)|*.xml|All files (*.*)|*.*", FilterIndex = 1 }; if (objSFD.ShowDialog() == true) { string strFormat = objSFD.SafeFileName.Substring(objSFD.SafeFileName.IndexOf('.') + 1).ToUpper(); StringBuilder strBuilder = new StringBuilder(); if (dGrid.ItemsSource == null) return; if (dGrid.HeadersVisibility == DataGridHeadersVisibility.Column || dGrid.HeadersVisibility == DataGridHeadersVisibility.All) { var listFields = from col in dGrid.Columns where excludedColumns == null || !excludedColumns.Contains(col.Header.ToString()) select col.Header.ToString(); BuildStringOfRow(strBuilder, listFields, strFormat); } //this line changed a little foreach (Graphic graphic in graphics) { var listValues = from col in dGrid.Columns where excludedColumns == null || !excludedColumns.Contains(col.Header.ToString()) select GetValue(col, graphic); BuildStringOfRow(strBuilder, listValues, strFormat); } StreamWriter sw = new StreamWriter(objSFD.OpenFile()); if (strFormat == "XML") { //Let us write the headers for the Excel XML sw.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>"); sw.WriteLine("<?mso-application progid=\"Excel.Sheet\"?>"); sw.WriteLine("<Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\">"); sw.WriteLine("<DocumentProperties xmlns=\"urn:schemas-microsoft-com:office:office\">"); sw.WriteLine("<Author>Arasu Elango</Author>"); sw.WriteLine("<Created>" + DateTime.Now.ToLocalTime().ToLongDateString() + "</Created>"); sw.WriteLine("<LastSaved>" + DateTime.Now.ToLocalTime().ToLongDateString() + "</LastSaved>"); sw.WriteLine("<Company>Atom8 IT Solutions (P) Ltd.,</Company>"); sw.WriteLine("<Version>12.00</Version>"); sw.WriteLine("</DocumentProperties>"); sw.WriteLine("<Worksheet ss:Name=\"Silverlight Export\" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\">"); sw.WriteLine("<Table>"); } sw.Write(strBuilder.ToString()); if (strFormat == "XML") { sw.WriteLine("</Table>"); sw.WriteLine("</Worksheet>"); sw.WriteLine("</Workbook>"); } sw.Close(); } } private static string GetValue(DataGridColumn col, Graphic graphic) { if (col is DataGridBoundColumn) { var path = (col as DataGridBoundColumn).Binding.Path.Path; var att = Regex.Replace(path, ".*\\[(.*)\\]", "$1"); return graphic.Attributes[att].ToString(); } return string.Empty; } private static void BuildStringOfRow(StringBuilder strBuilder, IEnumerable<string> lstFields, string strFormat) { switch (strFormat) { case "XML": strBuilder.AppendLine("<Row>"); strBuilder.AppendLine(String.Join("", lstFields.ToArray())); strBuilder.AppendLine("</Row>"); break; case "CSV": strBuilder.AppendLine(String.Join(",", lstFields.ToArray())); break; } } private static string FormatField(string data, string format) { switch (format) { case "XML": return String.Format("<Cell><Data ss:Type=\"String\">{0}</Data></Cell>", data); case "CSV": return String.Format("\"{0}\"", data.Replace("\"", "\"\"\"").Replace("\n", "").Replace("\r", "")); } return data; } }
//handle the Export All To Excel click private void exportAll_Click(object sender, RoutedEventArgs e) { FeatureLayer fe = (FeatureLayer)MyMap.Layers["California"]; //ete.ExportData(fe.Graphics.ToList(), MyDataGrid.Columns); MyDataGrid.Export(fe.Graphics, null); } private void exportSelected_Click(object sender, RoutedEventArgs e) { FeatureLayer fe = (FeatureLayer)MyMap.Layers["California"]; //ete.ExportData(fe.SelectedGraphics.ToList(), MyDataGrid.Columns); MyDataGrid.Export(fe.SelectedGraphics.ToList(), null); }
return graphic.Attributes[att] == null ? string.Empty : graphic.Attributes[att].ToString();
return graphic.Attributes[path] == null ? string.Empty : graphic.Attributes[path].ToString();
private static string GetValue(DataGridColumn col, Graphic graphic) { if (col is DataGridBoundColumn) { DataGridBoundColumn column = col as DataGridBoundColumn; if ((column.Binding != null) && (column.Binding.Path != null)) { string path = column.Binding.Path.Path; if (!string.IsNullOrEmpty(path)) { var att = Regex.Replace(path, ".*\\[(.*)\\]", "$1"); return graphic.Attributes[att].ToString(); } } } return string.Empty; }
return graphic.Attributes[att].ToString();
public static class DataGridExtensions { public static void Export(this FeatureDataGrid dg, IEnumerable<Graphic> graphics, List<string> excludedColumns) { ExportDataGrid(dg, graphics, excludedColumns); } public static void ExportDataGrid(FeatureDataGrid dGrid, IEnumerable<Graphic> graphics, List<string> excludedColumns) { SaveFileDialog objSFD = new SaveFileDialog() { DefaultExt = "csv", Filter = "CSV Files (*.csv)|*.csv|Excel XML (*.xml)|*.xml|All files (*.*)|*.*", FilterIndex = 1 }; if (objSFD.ShowDialog() == true) { string strFormat = objSFD.SafeFileName.Substring(objSFD.SafeFileName.IndexOf('.') + 1).ToUpper(); StringBuilder strBuilder = new StringBuilder(); if (dGrid.ItemsSource == null) return; if (dGrid.HeadersVisibility == DataGridHeadersVisibility.Column || dGrid.HeadersVisibility == DataGridHeadersVisibility.All) { var listFields = from col in dGrid.Columns where excludedColumns == null || !excludedColumns.Contains(col.Header.ToString()) select FormatField(col.Header.ToString(), strFormat); BuildStringOfRow(strBuilder, listFields, strFormat); } foreach (Graphic graphic in graphics) { var listValues = from col in dGrid.Columns where excludedColumns == null || !excludedColumns.Contains(col.Header.ToString()) select FormatField(GetValue(col, graphic), strFormat); BuildStringOfRow(strBuilder, listValues, strFormat); } StreamWriter sw = new StreamWriter(objSFD.OpenFile()); if (strFormat == "XML") { //Let us write the headers for the Excel XML sw.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>"); sw.WriteLine("<?mso-application progid=\"Excel.Sheet\"?>"); sw.WriteLine("<Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\">"); sw.WriteLine("<DocumentProperties xmlns=\"urn:schemas-microsoft-com:office:office\">"); sw.WriteLine("<Author>Arasu Elango</Author>"); sw.WriteLine("<Created>" + DateTime.Now.ToLocalTime().ToLongDateString() + "</Created>"); sw.WriteLine("<LastSaved>" + DateTime.Now.ToLocalTime().ToLongDateString() + "</LastSaved>"); sw.WriteLine("<Company>Atom8 IT Solutions (P) Ltd.,</Company>"); sw.WriteLine("<Version>12.00</Version>"); sw.WriteLine("</DocumentProperties>"); sw.WriteLine("<Worksheet ss:Name=\"Silverlight Export\" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\">"); sw.WriteLine("<Table>"); } sw.Write(strBuilder.ToString()); if (strFormat == "XML") { sw.WriteLine("</Table>"); sw.WriteLine("</Worksheet>"); sw.WriteLine("</Workbook>"); } sw.Close(); } } private static string GetValue(DataGridColumn col, Graphic graphic) { if (col is DataGridBoundColumn) { DataGridBoundColumn column = col as DataGridBoundColumn; if ((column.Binding != null) && (column.Binding.Path != null)) { string path = column.Binding.Path.Path; if (!string.IsNullOrEmpty(path)) { var att = Regex.Replace(path, ".*\\[(.*)\\]", "$1"); return graphic.Attributes[att].ToString(); } } } return string.Empty; } private static void BuildStringOfRow(StringBuilder strBuilder, IEnumerable<string> lstFields, string strFormat) { switch (strFormat) { case "XML": strBuilder.AppendLine("<Row>"); strBuilder.AppendLine(String.Join("", lstFields.ToArray())); strBuilder.AppendLine("</Row>"); break; case "CSV": strBuilder.AppendLine(String.Join(",", lstFields.ToArray())); break; } } private static string FormatField(string data, string format) { switch (format) { case "XML": return String.Format("<Cell><Data ss:Type=\"String\">{0}</Data></Cell>", data); case "CSV": return String.Format("\"{0}\"", data.Replace("\"", "\"\"\"").Replace("\n", "").Replace("\r", "")); } return data; } }
resultFeature.Attributes.Add("OWNADDRESS_FULL", resultFeature.Attributes["OWNADDRESS"] + "\r" + (resultFeature.Attributes["OWNCITYST"] + " " + resultFeature.Attributes["OWNZIPCODE"]).Trim()); // + "\r" + resultFeature.Attributes["OWNCOUNTRY"]);
Sorry I zipped the wrong file :
First off, thank you for providing this code. It works pretty well for me if I am not exporting more than 10-15 records at a time. Anymore than that then it starts either dropping data or wont save. Screencap of an exported csv. Are there limits to how much data can be exported? I have found that if I export data from a datagrid that has fewer columns I am able to export more records before it either doesn't work or stops populating columns.
Aangemelde leden kunnen berichten plaatsen, updates volgen en meer. Nieuw hier? Registreer een gratis account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.