34

我想要完成的事情

  • 我的应用程序生成一些表格数据
  • 我希望用户能够启动 Excel 并单击“粘贴”以将数据作为单元格放置在 Excel 中
  • Windows 接受一种名为“CommaSeparatedValue”的格式,它与它的 API 一起使用,所以这似乎是可能的
  • 将原始文本放在剪贴板上是可行的,但尝试使用这种格式却不行
  • 注意:我可以正确地从剪贴板检索 CSV 数据,我的问题是将 CSV 数据粘贴到剪贴板。

我尝试过的不起作用

剪贴板.SetText()

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

剪贴板.SetData()

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

在这两种情况下,剪贴板上都有一些东西,但是当粘贴到 Excel 中时,它会显示为一个垃圾文本单元格:“–§žý;pC¦yVk²ˆû”

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

正如 BFree 的回答所示,使用TextDataFormat的SetText作为一种解决方法

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

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

仍然好奇为什么 CommaSeparatedValue不起作用

4

4 回答 4

42

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

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

这是一些示例代码(请注意,此处使用了 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);
于 2008-12-15T18:09:17.150 回答
7

使用制表符代替逗号。IE:

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

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

于 2008-12-01T03:35:33.940 回答
4

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

于 2008-12-01T03:56:13.213 回答
0

通过使用 CSV 库 ( KBCsv ) 将数据写入临时文件夹中的 CSV 文件,然后在 Excel 中使用Process.Start(). 一旦它在 Excel 中,格式化位很容易(呃),从那里复制粘贴。

string filePath = System.IO.Path.GetTempPath() + Guid.NewGuid().ToString() + ".csv";

using (var streamWriter = new StreamWriter(filePath))
using (CsvWriter csvWriter = new CsvWriter(streamWriter))
{
    // optional header
    csvWriter.WriteRecord(new List<string>(){"Heading1", "Heading2", "YouGetTheIdea" });

    csvWriter.ValueSeparator = ',';
    foreach (var thing in YourListOfThings ?? new List<OfThings>())
    {
        if (thing != null)
        {
            List<string> csvLine = new List<string>
                                         {
                                             thing.Property1, thing.Property2, thing.YouGetTheIdea
                                         };

            csvWriter.WriteRecord(csvLine);
        }
    }
}

Process.Start(filePath);

BYO 错误处理和记录。

于 2021-07-27T00:25:54.097 回答