开发者

Drag and drop a html table into another application

I'm having a problem dragging an html table from my C# winforms application into an external application (Outlook email message) and getting it to render as a table instead of a plain text version of that table. I know that when you copy/paste in the clipboard you have to put the table in CF_HTML forma开发者_开发知识库t but that doesn't seem to help with dragging the table. Does anyone know what I am missing?


ObjectListView supports copying and dragging rows from a ListView to other applications, in both text and HTML versions. To do that, it does something like this:

DataObject dataObject = new DataObject();
this.CreateTextFormats(dataObject);
Clipboard.SetDataObject(dataObject);

To do drag and drop, the code is virtually the same:

DataObject dataObject = new DataObject();
this.CreateTextFormats(dataObject);
DragDropEffects effect = this.DoDragDrop(dataObject, DragDropEffects.All);                

CreateTextFormats() is not complicated:

public void CreateTextFormats(DataObject do) {
    string textVersion;
    string htmlVersion;
    // Do the work of making the tab-separated text version and the HTML code
    do.SetData(textVersion);
    do.SetText(ConvertToHtmlFragment(htmlVersion), TextDataFormat.Html);
}

Getting the HTML format right took longer:

/// <summary>
/// Convert the fragment of HTML into the Clipboards HTML format.
/// </summary>
/// <remarks>The HTML format is found here http://msdn2.microsoft.com/en-us/library/aa767917.aspx
/// </remarks>
/// <param name="fragment">The HTML to put onto the clipboard. It must be valid HTML!</param>
/// <returns>A string that can be put onto the clipboard and will be recognized as HTML</returns>
private string ConvertToHtmlFragment(string fragment) {
    // Minimal implementation of HTML clipboard format
    string source = "http://www.codeproject.com/KB/list/ObjectListView.aspx";

    const String MARKER_BLOCK =
        "Version:1.0\r\n" +
        "StartHTML:{0,8}\r\n" +
        "EndHTML:{1,8}\r\n" +
        "StartFragment:{2,8}\r\n" +
        "EndFragment:{3,8}\r\n" +
        "StartSelection:{2,8}\r\n" +
        "EndSelection:{3,8}\r\n" +
        "SourceURL:{4}\r\n" +
        "{5}";

    int prefixLength = String.Format(MARKER_BLOCK, 0, 0, 0, 0, source, "").Length;

    const String DEFAULT_HTML_BODY =
        "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">" +
        "<HTML><HEAD></HEAD><BODY><!--StartFragment-->{0}<!--EndFragment--></BODY></HTML>";

    string html = String.Format(DEFAULT_HTML_BODY, fragment);
    int startFragment = prefixLength + html.IndexOf(fragment);
    int endFragment = startFragment + fragment.Length;

    return String.Format(MARKER_BLOCK, prefixLength, prefixLength + html.Length, startFragment, endFragment, source, html);
}


If you don't need formatting, simply copy it to the clipboard as tab-delimited text. This way it'll paste as a table in Excel, and presumable most other table-compatible applications.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜