T
Tommy Carlier
I've seen a lot of messages from people who have trouble copying HTML
to the clipboard. I used to have the same problem, but I found a
solution. I needed to copy grid-data to Excel, so I decided to use the
HTML-format.
My solution (in C#) also solves an encoding-problem.
I hope that someone finds this useful.
using System;
using System.Text;
using System.Windows.Forms;
class Test
{
private void wfcCopyHTMLToClipBoard(string html)
{
Encoding enc = Encoding.UTF8;
string begin = "Version:0.9\r\nStartHTML:{0:000000}\r\nEndHTML:{1:000000}"
+ "\r\nStartFragment:{2:000000}\r\nEndFragment:{3:000000}\r\n";
string html_begin = "<html>\r\n<head>\r\n"
+ "<meta http-equiv=\"Content-Type\""
+ " content=\"text/html; charset=" + enc.WebName + "\">\r\n"
+ "<title>HTML clipboard</title>\r\n</head>\r\n<body>\r\n"
+ "<!--StartFragment-->";
string html_end = "<!--EndFragment-->\r\n</body>\r\n</html>\r\n";
int count_begin = lEncoding.GetByteCount(begin);
int count_html_begin = lEncoding.GetByteCount(html_begin);
int count_html = lEncoding.GetByteCount(html);
int count_html_end = lEncoding.GetByteCount(html_end);
string html_total = String.Format(
begin
, count_begin
, count_begin + count_html_begin + count_html + count_html_end
, count_begin + count_html_begin
, count_begin + count_html_begin + count_html
) + html_begin + html + html_end;
DataObject obj = new DataObject();
obj.SetData(DataFormats.Html, new System.IO.MemoryStream(
enc.GetBytes(html_total)));
Clipboard.SetDataObject(obj, true);
}
}
to the clipboard. I used to have the same problem, but I found a
solution. I needed to copy grid-data to Excel, so I decided to use the
HTML-format.
My solution (in C#) also solves an encoding-problem.
I hope that someone finds this useful.
using System;
using System.Text;
using System.Windows.Forms;
class Test
{
private void wfcCopyHTMLToClipBoard(string html)
{
Encoding enc = Encoding.UTF8;
string begin = "Version:0.9\r\nStartHTML:{0:000000}\r\nEndHTML:{1:000000}"
+ "\r\nStartFragment:{2:000000}\r\nEndFragment:{3:000000}\r\n";
string html_begin = "<html>\r\n<head>\r\n"
+ "<meta http-equiv=\"Content-Type\""
+ " content=\"text/html; charset=" + enc.WebName + "\">\r\n"
+ "<title>HTML clipboard</title>\r\n</head>\r\n<body>\r\n"
+ "<!--StartFragment-->";
string html_end = "<!--EndFragment-->\r\n</body>\r\n</html>\r\n";
int count_begin = lEncoding.GetByteCount(begin);
int count_html_begin = lEncoding.GetByteCount(html_begin);
int count_html = lEncoding.GetByteCount(html);
int count_html_end = lEncoding.GetByteCount(html_end);
string html_total = String.Format(
begin
, count_begin
, count_begin + count_html_begin + count_html + count_html_end
, count_begin + count_html_begin
, count_begin + count_html_begin + count_html
) + html_begin + html + html_end;
DataObject obj = new DataObject();
obj.SetData(DataFormats.Html, new System.IO.MemoryStream(
enc.GetBytes(html_total)));
Clipboard.SetDataObject(obj, true);
}
}