Original User: gregg.bretonWorking with Feature Data Grid Export Function and not able to download two fields (Date Field and Coded Value Domain Field)Darina/Dominque - Have you had this occur?I did not define any columns to be excluded.[ATTACH=CONFIG]18413[/ATTACH][ATTACH=CONFIG]18414[/ATTACH][ATTACH=CONFIG]18415[/ATTACH]The code is in two parts and was submitted by Dominque and Darina Thank you 🙂 Previous Post
private void ExcelExport_Click(object sender, RoutedEventArgs e)
{
FeatureLayer fe = (FeatureLayer)Map.Layers["Samples"];
RelatedRowsDataGrid.Export(fe.Graphics, null);
}
public static class DataGridExtensionsFDG
{
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();
}
}
//Check for null errors
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 graphic.Attributes[att] == null ? string.Empty : 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;
}
}