开发者

Exporting wpf datagrid into custom Excel CSV file

I have a wpf datagrid which is bound to a MVVM collection class. And I have some properties of MVVM class which is bound to datagrid but not every.

I need to export data in Excel CSV file from the datagrid. But there are some properties of the MVVM class which are not bound to datagrid but need to be shown in Excel file. So I need to create custom columns (columns from datagrid+some extra).

Here is the code I am using currently to create the Excel CSV file:

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

This creates the file with the exact columns from the DG but not extra which I want. How can I add them?


I'm guessing you're using a method on the grid control to produce the CSV? You've not stated as much in your question.

If so, I would move this responsibility to your view model. You can use a CSV library such as KBCsv to perform the task of writing your data.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜