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("\r\n", 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;
}
}
}
Solved! Go to Solution.
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;
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; }
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("\r\n", 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();