简体   繁体   中英

C# Copy HTML-formatted table to clipboard

I need to get a HTML-formatted table from a local network address, fill in a few fields in it and then copy it to clipboard. The closest I got to the solution was the code below:

string text = File.ReadAllText(@"[network path]");
        text = text.Replace("::TTYPE::", "BLABLABLA");
        text = text.Replace("::VERSIONNAME::", SWVBox.Text);
        text = text.Replace("::TRESULT::", FinalResult());
        text = text.Replace("::SERVERPATH::", "BLABLABLA");
        text = text.Replace("::TESTERNAME::", Environment.UserName);
        text = text.Replace("::COMMENTS::", "BLABLABLA");

        Clipboard.SetText(text);

This code has copied the data contained in the HTML file as a plain text, so I've also tried Clipboard.SetText(text, TextDataFormat.Html); but it didn't work. Any help will be appreciated.

This may be what you're looking for: https://msdn.microsoft.com/en-us/library/windows/apps/xaml/jj159584.aspx

Here is a code example from that page (doesn't look like you need the part that handles images):

{
    // Prepare some HTML for copying to the Clipboard.
    const string imgSrc = "ms-appx-web:///assets/windows-sdk.png";
    const string htmlFragment = "This sample shows how to copy HTML to the "
            + "Clipboard. <br />"
            + "To <b>copy</b>, add text formats to a <i>DataPackage</i>, "
            + "and then pass <i>DataPackage</i> the to Clipboard.<br /> "
            + "To handle local image files (such as the one below), use "
            + "resourceMap collection."
            + "<br /><img id=\"scenario1LocalImage\" src=\""
            + imgSrc
            + "\" /><br />";

    string htmlFormat = HtmlFormatHelper.CreateHtmlFormat(htmlFragment);

    // Create a DataPackage object.
    var dataPackage = new DataPackage();

    // Set the content of the DataPackage as HTML format.
    dataPackage.SetHtmlFormat(htmlFormat);

    // Populate the resource map with RandomAccessStreamReference objects 
    // corresponding to local image files embedded in the HTML.
    var imgUri = new Uri(imgSrc);
    var imgRef = RandomAccessStreamReference.CreateFromUri(imgUri);
    dataPackage.ResourceMap[imgSrc] = imgRef;

    try
    {
        // Set the DataPackage to the clipboard.
        Clipboard.SetContent(dataPackage);
        OutputText.Text = "HTML format was copied to the Clipboard. ";
    }
    catch (Exception ex)
    {
        // Copying data to the Clipboard can fail if, another application is 
        // holding the Clipboard open.
        rootPage.NotifyUser("Error copying content to Clipboard: " +
            ex.Message + ". Try again", NotifyType.ErrorMessage);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM