在迁移ASP.NET代码到Silverlight应用程序的过程中,经常需要将DataGrid中的数据导出到Excel兼容的格式,以便于后续的分析和使用。本文将介绍如何在Silverlight中实现这一功能。
Silverlight是一种富互联网应用程序(RIA)技术,它允许开发者创建具有丰富用户界面的网络应用程序。然而,Silverlight在数据导出方面的能力相对较弱。为了解决这个问题,可以为DataGrid控件创建一个扩展,使其支持数据导出功能。
通过添加一个名为DataGridExtensions.cs的文件到项目中,可以为DataGrid控件添加导出功能。这个扩展提供了两个方法:
以下是Export方法的实现:
public static void Export(this DataGrid dg)
{
ExportDataGrid(dg);
}
以下是ExportDataGrid方法的实现:
public static void ExportDataGrid(DataGrid dGrid)
{
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;
List<string> lstFields = new List<string>();
if (dGrid.HeadersVisibility == DataGridHeadersVisibility.Column || dGrid.HeadersVisibility == DataGridHeadersVisibility.All)
{
foreach (DataGridColumn dgcol in dGrid.Columns)
lstFields.Add(FormatField(dgcol.Header.ToString(), strFormat));
BuildStringOfRow(strBuilder, lstFields, strFormat);
}
foreach (object data in dGrid.ItemsSource)
{
lstFields.Clear();
foreach (DataGridColumn col in dGrid.Columns)
{
string strValue = "";
Binding objBinding = null;
if (col is DataGridBoundColumn)
objBinding = (col as DataGridBoundColumn).Binding;
if (col is DataGridTemplateColumn)
{
// This is a template column...
// Let us see the underlying dependency object
DependencyObject objDO = (col as DataGridTemplateColumn).CellTemplate.LoadContent();
FrameworkElement oFE = (FrameworkElement)objDO;
FieldInfo oFI = oFE.GetType().GetField("TextProperty");
if (oFI != null)
{
if (oFI.GetValue(null) != null)
{
if (oFE.GetBindingExpression((DependencyProperty)oFI.GetValue(null)) != null)
objBinding = oFE.GetBindingExpression((DependencyProperty)oFI.GetValue(null)).ParentBinding;
}
}
}
if (objBinding != null)
{
if (objBinding.Path.Path != "")
{
PropertyInfo pi = data.GetType().GetProperty(objBinding.Path.Path);
if (pi != null) strValue = pi.GetValue(data, null).ToString();
}
if (objBinding.Converter != null)
{
if (strValue != "")
strValue = objBinding.Converter.Convert(strValue, typeof(string), objBinding.ConverterParameter, objBinding.ConverterCulture).ToString();
else
strValue = objBinding.Converter.Convert(data, typeof(string), objBinding.ConverterParameter, objBinding.ConverterCulture).ToString();
}
}
lstFields.Add(FormatField(strValue, strFormat));
}
BuildStringOfRow(strBuilder, lstFields, strFormat);
}
StreamWriter sw = new StreamWriter(objSFD.OpenFile());
if (strFormat == "XML")
{
// Let us write the headers for the Excel XML
sw.WriteLine("");
sw.WriteLine("");
sw.WriteLine("");
sw.WriteLine("");
sw.WriteLine("Arasu Elango ");
sw.WriteLine("" + DateTime.Now.ToLocalTime().ToLongDateString() + " ");
sw.WriteLine("" + DateTime.Now.ToLocalTime().ToLongDateString() + " ");
sw.WriteLine("Atom8 IT Solutions (P) Ltd., ");
sw.WriteLine("12.00 ");
sw.WriteLine(" ");
sw.WriteLine("");
sw.WriteLine("@Table>");
}
sw.Write(strBuilder.ToString());
if (strFormat == "XML")
{
sw.WriteLine("");
sw.WriteLine(" ");
sw.WriteLine(" ");
}
sw.Close();
}
}
ExportDataGrid方法会提示用户选择输出文件名,并根据用户的选择确定保存格式(XML或CSV)。ExcelXML格式不兼容Microsoft Excel 2003或更早版本。
在项目中包含DataGridExtensions.cs文件后,可以通过调用DataGrid的Export()方法来导出DataGrid的内容。例如,如果DataGrid的名称是objDataGrid,将调用objDataGrid.Export()来触发导出。