Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将wpf datagrid导出到自定义Excel CSV文件_C#_Wpf_Excel_Datagrid - Fatal编程技术网

C# 将wpf datagrid导出到自定义Excel CSV文件

C# 将wpf datagrid导出到自定义Excel CSV文件,c#,wpf,excel,datagrid,C#,Wpf,Excel,Datagrid,我有一个绑定到MVVM集合类的wpf datagrid。我有一些MVVM类的属性,它绑定到datagrid,但不是每一个 我需要从datagrid导出Excel CSV文件中的数据。但MVVM类的某些属性没有绑定到datagrid,但需要在Excel文件中显示。所以我需要创建自定义列(来自datagrid的列+一些额外的列) 以下是我当前用于创建Excel CSV文件的代码: importedDG.SelectAllCells(); importedDG.ClipboardCopyMode =

我有一个绑定到MVVM集合类的wpf datagrid。我有一些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中的精确列创建文件,但不是我想要的额外列。如何添加它们?

我猜您正在使用网格控件上的方法生成CSV?你在问题中没有说得那么多


如果是这样,我会把这个责任转移到您的视图模型上。您可以使用CSV库(如)来执行写入数据的任务。

您正在努力解决的是哪一部分?我可以创建具有相同datagrid列的excel文件,但如何添加datagrid中没有的额外列。您当前如何创建excel文件?importedDG.SelectAllCells();importedDG.ClipboardCopyMode=DataGridClipboardCopyMode.IncludeHeader;ApplicationCommands.Copy.Execute(null,importedDG);importedDG.UnselectAllCells();字符串路径1=“C:\\test.csv”;string result1=(string)Clipboard.GetData(DataFormats.CommaSeparatedValue);剪贴板。清除();System.IO.StreamWriter file1=新的System.IO.StreamWriter(路径1);file1.WriteLine(result1);file1.Close();这将使用DG中的精确列创建文件,但不是我想要的额外列。我收到一个错误:引用和编译程序集时生成失败。我已解决了它,但现在我收到csvwriter的initilizer错误。我确实解决了它。但是,如果我需要将日期之类的数据放在数据后面的标题列之前。有办法吗?