有没有办法可以将我的Silverlight DataGrid数据导出到excel或csv?
我在网上搜索但找不到任何例子!
非常感谢
Silverlight 3更改了此问题的答案,因为它使用户能够在用户桌面上的指定位置创建文件.我调整了DaniCE提交的代码,将一些内容分成了一些可读性方法,并使用了Excel应该识别的松散定义的CSV格式.
private void exportHistoryButton_Click(object sender, RoutedEventArgs e) { string data = ExportDataGrid(true, historyDataGrid); SaveFileDialog sfd = new SaveFileDialog() { DefaultExt = "csv", Filter = "CSV Files (*.csv)|*.csv|All files (*.*)|*.*", FilterIndex = 1 }; if (sfd.ShowDialog() == true) { using (Stream stream = sfd.OpenFile()) { using (StreamWriter writer = new StreamWriter(stream)) { writer.Write(data); writer.Close(); } stream.Close(); } } } private string FormatCSVField(string data) { return String.Format("\"{0}\"", data.Replace("\"", "\"\"\"") .Replace("\n", "") .Replace("\r", "") ); } public string ExportDataGrid(bool withHeaders, DataGrid grid) { string colPath; System.Reflection.PropertyInfo propInfo; System.Windows.Data.Binding binding; System.Text.StringBuilder strBuilder = new System.Text.StringBuilder(); System.Collections.IList source = (grid.ItemsSource as System.Collections.IList); if (source == null) return ""; Listheaders = new List (); grid.Columns.ToList().ForEach(col => { if (col is DataGridBoundColumn){ headers.Add(FormatCSVField(col.Header.ToString())); } }); strBuilder .Append(String.Join(",", headers.ToArray())) .Append("\r\n"); foreach (Object data in source) { List csvRow = new List (); foreach (DataGridColumn col in grid.Columns) { if (col is DataGridBoundColumn) { binding = (col as DataGridBoundColumn).Binding; colPath = binding.Path.Path; propInfo = data.GetType().GetProperty(colPath); if (propInfo != null) { csvRow.Add(FormatCSVField(propInfo.GetValue(data, null).ToString())); } } } strBuilder .Append(String.Join(",", csvRow.ToArray())) .Append("\r\n"); } return strBuilder.ToString(); }