C#粘贴剪贴板中的内容到Excel工作表 [英] C# Paste Clipboard content into Excel worksheet
问题描述
正如标题所说,我尝试实际粘贴什么是我的剪贴板到Excel
As the title says, I try to actually paste what is in my Clipboard into an Excel.
我下面的代码:
Clipboard.SetText(html);
sheet.Range("A1").Value = Clipboard.GetText();
其实,变量HTML包含HTML代码的文件,当我这样做,我其实粘贴只有HTML内容纳入范围,但是,如果我打开Excel和做手工,选择性粘贴...我可以粘贴HTML代码,但它把代码变成一个真正的表,而不是HTML代码,这才是真正的结果,我想,没有做手工。
Actually, the variable html contains a html code file, when I do it like that, I actually paste only the html content into the Range, but, if I open Excel and do by hand, Paste Special... I can paste the html code, but it transforms the code into a real table instead of a html code and this is the real result that I want, without doing it by hand.
的 Excel.Range.Copy()与Clipboard.GetText()
另一种方法是粘贴:
foreach (Excel.Worksheet sheet in workbook.Sheets)
{
foreach (Excel.Shape shape in sheet.Shapes)
{
Clipboard.SetText(html);
//doesn't work:
sheet.Range("A1").Value = sheet.PasteSpecial(Clipboard.GetText());
sheet.PasteSpecial(Clipboard.GetText()); //throws error
}
}
不过,这种方式行不通太。我可以用HTML做到这一点 - >图像并粘贴图像,但实际值应该可以访问,而不是一个图片
But this way doesn't work too. I can do it with an html -> image and paste the image, but real values should be accessible and not a picture.
希望有人能阐明如何解决它。
Hope someone can clarify how to solve it.
感谢。
推荐答案
您可以尝试使用的SetData
而不是的setText
:
You may try to use SetData
instead of SetText
:
Clipboard.SetData(DataFormats.Html, html);
的字符串复制到剪贴板中,将其标记为HTML(如果没有你的情况下工作,的setText
可能是确定)。
要调用 PasteSpecial的
为您要插入的情况发生,同时考虑到您的意见单元格区域:
The call to PasteSpecial
for the cell range where you want the insert to happen, taking regard to your comments:
ActiveSheet.Range("A1").PasteSpecial(
Excel.Enums.XlPasteType.xlPasteAll,
Excel.Enums.XlPasteSpecialOperation.xlPasteSpecialOperationNone,
false, false);
请注意,通过使用值$指定的单元格的新值C $ C>属性的从不拷贝任何格式等。
Note that assigning a cell a new value by using the Value
property never copies any formats etc.
这篇关于C#粘贴剪贴板中的内容到Excel工作表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!