将文本从WPF DataGrid复制到剪贴板再到Excel [英] Copy text from WPF DataGrid to Clipboard to Excel

查看:220
本文介绍了将文本从WPF DataGrid复制到剪贴板再到Excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有WPF DataGrid(VS2010 C#)。我将数据从DataGrid复制到剪贴板,并将其写入Excel文件。下面是我的代码。

I have WPF DataGrid (VS2010 C#). I copied the data from DataGrid to Clipboard and write it to an Excel file. Below is my code.

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

一切正常,除了当我打开excel文件时,它会给我两个警告:

Everything works out OK except when I open the excel file it gives me two warning:


您要打开的文件
'test.xls'的格式与文件扩展名指定的格式不同。

打开文件之前,请验证该文件没有损坏
并且来自受信任的来源。您要立即打开
该文件吗?

"The file you are trying to open 'test.xls' is in a different format than specified by the file extension. Verify that the file is not corrupted and is from a trusted source before opening the file. Do you want to open the file now?"

Excel检测到'test.xls'是
a SYLK文件,但无法加载。

"Excel has detected that 'test.xls' is a SYLK file, but cannot load it."

但是在我单击它之后,它仍然打开excel文件,然后确定数据的格式。但是我无法找到如何在打开excel文件之前摆脱这两个警告。

But after I click through it, it still open the excel file OK and data are formated as it supposed to be. But I can't find how to get rid of the two warnings before the excel file is open.

推荐答案

像您这样的问题已经在此处进行了描述:生成/打开CSV从控制台-文件格式错误
它有助于解决您的问题吗?

A problem like yours has already been described here : generating/opening CSV from console - file is in wrong format error. Does it helps to solve yours ?

编辑:这是与Microsoft KB相关的=> http://support.microsoft.com/kb/323626

Edit : Here is the Microsoft KB related => http://support.microsoft.com/kb/323626

这篇关于将文本从WPF DataGrid复制到剪贴板再到Excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆