如何:将剪贴板中的Html格式内容粘贴到Excel电子表格中。 [英] how to: Pasting Html formatted content from clipboard onto an excel spreadsheet.

查看:486
本文介绍了如何:将剪贴板中的Html格式内容粘贴到Excel电子表格中。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试将html格式的剪贴板内容粘贴到excel中。这是我的代码(来自网络):



Trying to paste content of clipboard in html format into excel. Here's my code (from the net) :

string htmlPath = @"C:\test.html";
string html = File.ReadAll(htmlPath) ;
Htmlfragment.CopyToClipboard(html); // got this class from: blogs.msdn.com/jmstall

Excel.Application xlApp = new Excel.Application() ;

if (xlApp == null) 
{ return ;} 

object misVal = System.Reflection.Missing.Value;
Excel.Workbook xlWorkBook = xlApp.Workbooks.Add(misVal);
Excel.WorkSheet xlWorkSheet = (Excel.WorkSheet) xlWorkBook.Worksheets.get_Item(1);

xlWorkSheet.get_Range("A1").PasteSpecial(Excel.XlPasteType.xlPasteAll, Excel.XlPasteSpecialOperation.xlPasteSpecialOperationNone, false, false) ;

< br $> b $ b

...

它在最后一行发出错误。

错误:



处理了COMException。

Range类的PasteSpecial方法失败。



我一定忽略了一些东西,但我无法理解。



非常感谢



...
And it gave an error on that last line.
Error :

COMException was handled.
PasteSpecial method of Range class failed.

I must've overlooked something, but I just couldn't figure it out.

Many Thanks

推荐答案

你试过吗:



Have you tried:

Clipboard.SetText(html, TextDataFormat.Html);

...

xlWorkSheet.get_Range("A1").PasteSpecial();





如果这不起作用,可能是因为您尝试的范围set是html代码范围的不同。



If this doesn't work, probably is because the range that you are trying to set is different of the range of the html code.


请阅读我对这个问题的评论。



我是建议看到这个: Worksheet.PasteSpecial Method [ ^ ]。 使用此方法粘贴来自其他应用程序的数据或以特定格式粘贴数据。



查看第一个输入参数:

Please, read my comment to the question.

I'd suggest to see this: Worksheet.PasteSpecial Method [^]. Use this method to paste data from other applications or to paste data in a specific format.

Have a look at first input parameter:
Format - A string that specifies the Clipboard format of the data. Common format strings include "HTML", "Bitmap", "Hyperlink", "Unicode Text", and "Text".



和最后一个:


and the last one:

NoHTMLFormatting - true to remove all formatting, hyperlinks, and images from HTML; false to paste HTML as is. The default value is false.


这篇关于如何:将剪贴板中的Html格式内容粘贴到Excel电子表格中。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆