将wpf datagrid导出为自定义Excel CSV文件

时间:2011-06-13 16:04:12

标签: c# wpf excel datagrid

我有一个绑定到MVVM集合类的wpf数据网格。我有一些MVVM类的属性,它绑定到datagrid但不是每个都绑定。

我需要从datagrid导出Excel CSV文件中的数据。但MVVM类的某些属性未绑定到datagrid但需要在Excel文件中显示。所以我需要创建自定义列(datagrid中的列+一些额外的列)。

以下是我目前用于创建Excel CSV文件的代码:

importedDG.SelectAllCells(); 
importedDG.ClipboardCopyMode = DataGridClipboardCopyMode.IncludeHeader;
ApplicationCommands.Copy.Execute(null, importedDG);
importedDG.UnselectAllCells();
string path1 = "C:\\test.csv"; 
string result1 = (string)Clipboard.GetData(DataFormats.CommaSeparatedValue); 
Clipboard.Clear(); 
System.IO.StreamWriter file1 = new System.IO.StreamWriter(path1);
file1.WriteLine(result1);file1.Close(); 

这将使用DG中的确切列创建文件,但不是我想要的额外列。我该如何添加它们?

1 个答案:

答案 0 :(得分:1)

我猜你正在网格控件上使用一种方法来生成CSV?你没有在你的问题中说明多少。

如果是这样,我会将此责任移到您的视图模型中。您可以使用KBCsv等CSV库来执行编写数据的任务。

相关问题