如何使用 C# 将 CSV 数据粘贴到 Windows 剪贴板

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/329918/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 23:25:37  来源:igfitidea点击:

How to paste CSV data to Windows Clipboard with C#

c#windowscsvclipboardpaste

提问by namenlos

What I'm trying to accomplish

我想要完成的事情

  • My app generates some tabular data
  • I want the user to be able to launch Excel and click "paste" to place the data as cells in Excel
  • Windows accepts a format called "CommaSeparatedValue" that is used with it's APIs so this seems possible
  • Putting raw text on the clipboard works, but trying to use this format does not
  • NOTE: I can correctly retrieve CSV data from the clipboard, my problem is about pasting CSV data to the clipboard.
  • 我的应用程序生成一些表格数据
  • 我希望用户能够启动 Excel 并单击“粘贴”将数据作为单元格放置在 Excel 中
  • Windows 接受一种称为“CommaSeparatedValue”的格式,该格式与其 API 一起使用,因此这似乎是可能的
  • 将原始文本放在剪贴板上有效,但尝试使用此格式不起作用
  • 注意:我可以从剪贴板正确检索 CSV 数据,我的问题是将 CSV 数据粘贴到剪贴板。

What I have tried that isn't working

我尝试过的方法不起作用

Clipboard.SetText()

剪贴板.SetText()

System.Windows.Forms.Clipboard.SetText(  
  "1,2,3,4\n5,6,7,8", 
  System.Windows.Forms.TextDataFormat.CommaSeparatedValue
  );

Clipboard.SetData()

剪贴板.SetData()

System.Windows.Forms.Clipboard.SetData(
  System.Windows.Forms.DataFormats.CommaSeparatedValue,
  "1,2,3,4\n5,6,7,8", 
  );

In both cases something is placed on the clipboard, but when pasted into Excel it shows up as one cell of garbarge text: "–§?y;pC|yVk2??"

在这两种情况下,剪贴板上都有一些东西,但是当粘贴到 Excel 中时,它显示为一个垃圾文本单元格:“–§?y;pC|yVk2??”

Update 1: Workaround using SetText()

更新 1:使用 SetText() 的解决方法

As BFree's answer shows SetTextwith TextDataFormatserves as a workaround

由于 BFree 的回答显示SetTextwith TextDataFormat作为一种解决方法

System.Windows.Forms.Clipboard.SetText(  
  "1\t2\t3\t4\n5\t6\t7\t8", 
  System.Windows.Forms.TextDataFormat.Text
  );

I have tried this and confirm that now pasting into Excel and Word works correctly. In each case it pastes as a table with cells instead of plaintext.

我已经尝试过这个并确认现在粘贴到 Excel 和 Word 中可以正常工作。在每种情况下,它都会粘贴为带有单元格而不是纯文本的表格。

Still curious why CommaSeparatedValue is notworking.

仍然好奇,为什么CommaSeparatedValue是工作的。

采纳答案by user46432

The .NET Framework places DataFormats.CommaSeparatedValueon the clipboard as Unicode text. But as mentioned at http://www.syncfusion.com/faq/windowsforms/faq_c98c.aspx#q899q, Excel expects CSV data to be a UTF-8 memory stream (it is difficult to say whether .NET or Excel is at fault for the incompatibility).

.NET FrameworkDataFormats.CommaSeparatedValue作为 Unicode 文本放置在剪贴板上。但正如http://www.syncfusion.com/faq/windowsforms/faq_c98c.aspx#q899q所提到的,Excel 期望 CSV 数据是 UTF-8 内存流(很难说 .NET 还是 Excel 有问题因为不兼容)。

The solution I've come up with in my own application is to place two versions of the tabular data on the clipboard simultaneously as tab-delimited text and as a CSV memory stream. This allows the destination application to acquire the data in its preferred format. Notepad and Excel prefer the tab-delimited text, but you can force Excel to grab the CSV data via the Paste Special... command for testing purposes.

我在自己的应用程序中提出的解决方案是将两个版本的表格数据同时作为制表符分隔的文本和 CSV 内存流放在剪贴板上。这允许目标应用程序以其首选格式获取数据。记事本和 Excel 更喜欢制表符分隔的文本,但您可以强制 Excel 通过选择性粘贴...命令获取 CSV 数据以进行测试。

Here is some example code (note that WinForms-equivalents from the WPF namespaces are used here):

下面是一些示例代码(请注意,此处使用了来自 WPF 命名空间的 WinForms 等效项):

// Generate both tab-delimited and CSV strings.
string tabbedText = //...
string csvText = //...

// Create the container object that will hold both versions of the data.
var dataObject = new System.Windows.DataObject();

// Add tab-delimited text to the container object as is.
dataObject.SetText(tabbedText);

// Convert the CSV text to a UTF-8 byte stream before adding it to the container object.
var bytes = System.Text.Encoding.UTF8.GetBytes(csvText);
var stream = new System.IO.MemoryStream(bytes);
dataObject.SetData(System.Windows.DataFormats.CommaSeparatedValue, stream);

// Copy the container object to the clipboard.
System.Windows.Clipboard.SetDataObject(dataObject, true);

回答by BFree

Use tabs instead of commas. ie:

使用制表符而不是逗号。IE:

Clipboard.SetText("1\t2\t3\t4\t3\t2\t3\t4", TextDataFormat.Text);

Just tested this myself, and it worked for me.

刚刚自己测试了这个,它对我有用。

回答by jkchong

I have had success pasting into Excel using \t (see BFree's answer) as column separators and \n as row separators.

我已经成功地使用 \t(参见 BFree 的答案)作为列分隔符和 \n 作为行分隔符粘贴到 Excel 中。