我使用C#将内容添加到Windows剪贴板中,例如:
System.Windows.Forms.Clipboard.SetData("Foo Format", "Hello World");
由于数据是字符串,我希望它只是将字符串数据粘贴到剪贴板中,但在开头和结尾添加了额外的位,即:
00000000 96 A7 9E FD 13 3B 70 43 A6 79 56 10 6B B2 88 FB –§žý.;pC¦yV.k²ˆû
00000010 00 01 00 00 00 FF FF FF FF 01 00 00 00 00 00 00 .....ÿÿÿÿ.......
00000020 00 06 01 00 00 00 0B 48 65 6C 6C 6F 20 57 6F 72 .......Hello Wor
00000030 6C 64 0B ld.
前39个字节和最后一个字节是什么意思?有没有一种方法可以使用C#将原始字符串放入剪贴板?
发布于 2018-08-25 08:31:19
使用System.IO.MemoryStream
将字符串添加到剪贴板,而不在字符串值的开头和结尾添加额外的字节。
例如,以下字符串表示Microsoft Excel电子表格的一个片段:
class Program
{
[STAThread]
static void Main(string[] args)
{
string content = @"<?xml version=""1.0""?>
<?mso-application progid=""Excel.Sheet""?>
<Workbook xmlns=""urn:schemas-microsoft-com:office:spreadsheet""
xmlns:o=""urn:schemas-microsoft-com:office:office""
xmlns:x=""urn:schemas-microsoft-com:office:excel""
xmlns:ss=""urn:schemas-microsoft-com:office:spreadsheet""
xmlns:html=""http://www.w3.org/TR/REC-html40"">
<Styles>
<Style ss:ID=""Default"" ss:Name=""Normal"">
<Alignment ss:Vertical=""Bottom""/>
<Borders/>
<Font ss:FontName=""Calibri"" x:Family=""Swiss"" ss:Size=""11"" ss:Color=""#000000""/>
<Interior/>
<NumberFormat/>
<Protection/>
</Style>
<Style ss:ID=""s18"" ss:Name=""Currency"">
<NumberFormat
ss:Format=""_("$"* #,##0.00_);_("$"* \(#,##0.00\);_("$"* "-"??_);_(@_)""/>
</Style>
<Style ss:ID=""s64"">
<Font ss:FontName=""Calibri"" x:Family=""Swiss"" ss:Size=""11"" ss:Color=""#000000""
ss:Bold=""1""/>
</Style>
<Style ss:ID=""s65"" ss:Parent=""s18"">
<Font ss:FontName=""Calibri"" x:Family=""Swiss"" ss:Size=""11"" ss:Color=""#000000""/>
</Style>
</Styles>
<Worksheet ss:Name=""Sheet1"">
<Table ss:ExpandedColumnCount=""2"" ss:ExpandedRowCount=""2""
ss:DefaultRowHeight=""15"">
<Row>
<Cell><Data ss:Type=""String"">Month</Data></Cell>
<Cell><Data ss:Type=""String"">Year</Data></Cell>
</Row>
<Row>
<Cell ss:StyleID=""s64""><Data ss:Type=""String"">August</Data></Cell>
<Cell ss:StyleID=""s65""><Data ss:Type=""Number"">999.99</Data></Cell>
</Row>
</Table>
</Worksheet>
</Workbook>";
将其作为字符串传递给Clipboard
类:
Clipboard.SetData("XML Spreadsheet", content);
创建与Excel不兼容的格式,Excel在尝试粘贴该内容时显示以下错误:
但是,将其序列化为字节,然后将其作为MemoryStream传递
Clipboard.SetData("XML Spreadsheet", new MemoryStream(Encoding.UTF8.GetBytes(content)));
创建与Excel兼容的剪贴板内容:
https://stackoverflow.com/questions/52012704
复制相似问题