C# 以正确的格式将数据从剪贴板粘贴到excel

C# 以正确的格式将数据从剪贴板粘贴到excel,c#,.net,excel,C#,.net,Excel,在performance analyzer中,我从剪贴板中的表中复制数据 然后将其粘贴到excel文件中。 结果是: 但是,当我将其粘贴到文本编辑器中时,我会看到: 函数名包含样本独占样本包含样本 %独家样品% [clr.dll]26 26 39.39 39 39.39 Bee.Client.Common.beerRight.CheckRightsForBeeUser()10 0 15.15 0.00 Bee.Client.Common.beerRight.get_Invoke()6 0 9

在performance analyzer中,我从剪贴板中的表中复制数据

然后将其粘贴到excel文件中。 结果是:

但是,当我将其粘贴到文本编辑器中时,我会看到:

函数名包含样本独占样本包含样本 %独家样品%
[clr.dll]26 26 39.39 39 39.39
Bee.Client.Common.beerRight.CheckRightsForBeeUser()10 0 15.15 0.00
Bee.Client.Common.beerRight.get_Invoke()6 0 9.09 0.00
Bee.Client.Common.beerRight.Method(字符串,字符串)13 0 19.70 0.00
Bee.Client.Common.Custom.FmCustom..ctor()9 0 13.64 0.00

那么你能告诉我,我怎样才能存档这个效果

谢谢

更新

我会尽力解释的。 我的winform应用程序中有DataGridView。我编写了一些函数,将数据从表复制到剪贴板(结果与我的示例中的文本类似)。如果我将此文本从剪贴板粘贴到excel,结果将是excel文件,其中包含剪贴板中的数据,但将完全没有格式,并且此excel将很难读取


我想知道,他们是如何从表(图1)中准备数据的,当我将其粘贴到excel时,它有格式(图2),当我将其粘贴到文本编辑器中时,我们看到了原始文本。

原因是,为什么datagrid和excel之间的直接工作很好,是DataGridView组件的实现及其对复制操作的反应,以及要将内容粘贴到的应用程序的行为。它可以使用一些被记事本忽略的特殊代码

编辑

所以,现在我很理解你的兴趣。我不知道它在C#中是如何工作的,但在Java中看起来是这样的

每当剪贴板中有任何信息时,都会出现许多变体,以及其他应用程序如何使用这些内容

假设我想从剪贴板中获取内容。我这样做:

Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
Transferable contents = clipboard.getContents(null);
但是现在我应该决定我的申请应该如何查找这些信息,现在你的问题开始了

如果剪贴板中有图片,我只有1可能的图片表示形式:

[mimetype=image/x-java-image;representationclass=java.awt.Image]
如果我有记事本中的一些文本,那么已经有27个变体:

[mimetype=application/x-java-text-encoding;representationclass=[B]
[mimetype=application/x-java-serialized-object;representationclass=java.lang.String]
[mimetype=text/plain;representationclass=java.io.Reader]
[mimetype=text/plain;representationclass=java.lang.String]
[mimetype=text/plain;representationclass=java.nio.CharBuffer]
and so on...
[mimetype=application/x-java-text-encoding;representationclass=[B]
[mimetype=text/html;representationclass=java.io.Reader]
[mimetype=text/html;representationclass=java.lang.String]
[mimetype=text/html;representationclass=java.nio.CharBuffer]
[mimetype=text/html;representationclass=[C]
and so on...
如果我有Excel工作表中的一些单元格,则有56个变量:

[mimetype=application/x-java-text-encoding;representationclass=[B]
[mimetype=application/x-java-serialized-object;representationclass=java.lang.String]
[mimetype=text/plain;representationclass=java.io.Reader]
[mimetype=text/plain;representationclass=java.lang.String]
[mimetype=text/plain;representationclass=java.nio.CharBuffer]
and so on...
[mimetype=application/x-java-text-encoding;representationclass=[B]
[mimetype=text/html;representationclass=java.io.Reader]
[mimetype=text/html;representationclass=java.lang.String]
[mimetype=text/html;representationclass=java.nio.CharBuffer]
[mimetype=text/html;representationclass=[C]
and so on...
甚至还有Excel单元格的图像变体

[mimetype=image/x-java-image;representationclass=java.awt.Image]
这就是为什么可以从Excel复制一些单元格并将它们粘贴到绘图中作为位图!当然,记事本是不可能的,因为它的开发者不想使用这个演示文稿

现在我们可以看到,剪贴板并不像看上去那样原始。每次应用程序都可以分析内容,并从中选取最佳变体


现在,您可以尝试查找一些C#开发信息。我相信,你会明白的

导致datagrid和Excel之间直接工作良好的原因是DataGridView组件的实现及其对复制操作的反应,以及要将内容粘贴到其中的应用程序的行为。它可以使用一些被记事本忽略的特殊代码

编辑

所以,现在我很理解你的兴趣。我不知道它在C#中是如何工作的,但在Java中看起来是这样的

每当剪贴板中有任何信息时,都会出现许多变体,以及其他应用程序如何使用这些内容

假设我想从剪贴板中获取内容。我这样做:

Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
Transferable contents = clipboard.getContents(null);
但是现在我应该决定我的申请应该如何查找这些信息,现在你的问题开始了

如果剪贴板中有图片,我只有1可能的图片表示形式:

[mimetype=image/x-java-image;representationclass=java.awt.Image]
如果我有记事本中的一些文本,那么已经有27个变体:

[mimetype=application/x-java-text-encoding;representationclass=[B]
[mimetype=application/x-java-serialized-object;representationclass=java.lang.String]
[mimetype=text/plain;representationclass=java.io.Reader]
[mimetype=text/plain;representationclass=java.lang.String]
[mimetype=text/plain;representationclass=java.nio.CharBuffer]
and so on...
[mimetype=application/x-java-text-encoding;representationclass=[B]
[mimetype=text/html;representationclass=java.io.Reader]
[mimetype=text/html;representationclass=java.lang.String]
[mimetype=text/html;representationclass=java.nio.CharBuffer]
[mimetype=text/html;representationclass=[C]
and so on...
如果我有Excel工作表中的一些单元格,则有56个变量:

[mimetype=application/x-java-text-encoding;representationclass=[B]
[mimetype=application/x-java-serialized-object;representationclass=java.lang.String]
[mimetype=text/plain;representationclass=java.io.Reader]
[mimetype=text/plain;representationclass=java.lang.String]
[mimetype=text/plain;representationclass=java.nio.CharBuffer]
and so on...
[mimetype=application/x-java-text-encoding;representationclass=[B]
[mimetype=text/html;representationclass=java.io.Reader]
[mimetype=text/html;representationclass=java.lang.String]
[mimetype=text/html;representationclass=java.nio.CharBuffer]
[mimetype=text/html;representationclass=[C]
and so on...
甚至还有Excel单元格的图像变体

[mimetype=image/x-java-image;representationclass=java.awt.Image]
这就是为什么可以从Excel复制一些单元格并将它们粘贴到绘图中作为位图!当然,记事本是不可能的,因为它的开发者不想使用这个演示文稿

现在我们可以看到,剪贴板并不像看上去那样原始。每次应用程序都可以分析内容,并从中选取最佳变体


现在,您可以尝试查找一些C#开发信息。我相信,你会明白的

你想达到什么效果?值或表格之间有一定的距离?或者您想在文本编辑器中也使用网格?我想知道如何在excel文件中归档格式,就像这样?我的意思是,例如,在文本编辑器中,粗体文本、对齐等等?原始文本没有任何格式,这是您的文本编辑器显示给您的。我理解这一点。但我的问题不是关于文本编辑器。我已经编辑了我的问题。你想达到什么效果?值或表格之间有一定的距离?或者您想在文本编辑器中也使用网格?我想知道如何在excel文件中归档格式,就像这样?我的意思是,例如,在文本编辑器中,粗体文本、对齐等等?原始文本没有任何格式,这是您的文本编辑器显示给您的。我理解这一点。但我的问题不是关于文本编辑器。我已经编辑了我的问题。谢谢你的回答,安东!我想知道这些特殊代码是什么?我在哪里能读到他们的消息?谢谢你的回答,安东!我想知道这些特殊代码是什么?我在哪里可以读到他们的消息?