[英]How do I copy formatted HTML string into a Clipboard for paste using C#?
[英]C# Copy HTML-formatted table to clipboard
我需要從本地網絡地址獲取HTML格式的表格,在其中填寫一些字段,然后將其復制到剪貼板。 我最接近該解決方案的是以下代碼:
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);
此代碼已將HTML文件中包含的數據復制為純文本,因此我也嘗試了Clipboard.SetText(text, TextDataFormat.Html);
但這沒用。 任何幫助將不勝感激。
這可能是您要查找的內容: https : //msdn.microsoft.com/zh-cn/library/windows/apps/xaml/jj159584.aspx
這是該頁面上的代碼示例(看起來好像不需要處理圖像的部分):
{
// 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);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.