As the title says, I try to actually paste what is in my Clipboard into an Excel.
I've following code:
Clipboard.SetText(html);
sheet.Range("A1").Value = Clipboard.GetText();
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() paste with Clipboard.GetText()
Another way was:
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
}
}
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.
Thanks.
You may try to use SetData
instead of SetText
:
Clipboard.SetData(DataFormats.Html, html);
to copy the string into the clipboard and tag it as HTML (if this does not work in your case, SetText
may be ok).
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);
Note that assigning a cell a new value by using the Value
property never copies any formats etc.