2017-02-16 147 views
1

我有一个代码,什么是生成一个HTML格式的表,并尝试把剪贴板。VB.Net生成html表格到剪贴板

我的问题是,有时会丢失数据的末尾或粘贴数据的格式。

如果我在将htmlStructureStr变量写入剪切板之前写入调试窗口,它看起来不错,所有数据都在字符串中。

我猜是有一些与bytecounting,但我找不到解决方案。

这是我的函数(输入变量tableContent是一个HTML表格字符串):

Public Function TableStringToClipboard(ByVal tableContent As String) As Boolean 
     Dim htmlStructure As New StringBuilder 

#Region "--raw structure of the clipboard data" 
     htmlStructure.AppendLine("Version:1.0") 
     htmlStructure.AppendLine("StartHTML:0000000001") 
     htmlStructure.AppendLine("EndHTML:0000000004") 
     htmlStructure.AppendLine("StartFragment:0000000002") 
     htmlStructure.AppendLine("EndFragment:0000000003") 
     htmlStructure.AppendLine("<html>") 
     htmlStructure.AppendLine("<head>") 
     htmlStructure.AppendLine("<style>#styleCss#</style>") 
     htmlStructure.AppendLine("</head>") 
     htmlStructure.AppendLine("<body>") 

     htmlStructure.AppendLine("<table cellspacing=0>") 
     htmlStructure.AppendLine("<!--StartFragment-->") 
     htmlStructure.AppendLine("#tableHtml#") 
     htmlStructure.AppendLine("<!--EndFragment-->") 
     htmlStructure.AppendLine("</table>") 

     htmlStructure.AppendLine("</body>") 
     htmlStructure.AppendLine("</html>") 
#End Region 

#Region "--CSS format definition" 

     Dim styleCss As String = "" 

      styleCss = " 
      table {border-collapse: collapse;} 
      tr {color:black; 
       font-size:11.0pt; 
       font-weight: normal; 
       font-style:normal; 
       text-decoration:none; 
       font-family:Calibri, sans-serif;} 
      col {mso-width-source:auto;} 
      br {mso-data-placement:same-cell;} 
      .head 
       {padding:2px; 
       color:black; 
       font-size:11.0pt; 
       font-weight:900; 
       font-style:normal; 
       text-decoration:none; 
       font-family:Calibri, sans-serif; 

       text-align:left; 
       border:.5pt solid #000000; 
       white-space:nowrap; 
       vertical-align:middle; 
       background-color: PowderBlue; 
     } 
      td 
       {padding:2px; 
       text-align:general; 
       border:.5pt solid #000000; 
       white-space:nowrap; 
       vertical-align:middle;}" 

     htmlStructure.Replace("#styleCss#", styleCss) 
#End Region 

htmlStructure.Replace("#tableHtml#", tableContent.ToString) 

#Region "----ByteCounting" 

     Dim byteCounter As System.Text.ASCIIEncoding = System.Text.Encoding.ASCII 

     Dim value As Integer 
     Dim htmlStructureStr As String = htmlStructure.ToString 

     value = byteCounter.GetByteCount(Left(htmlStructureStr, htmlStructureStr.IndexOf("<html"))) 
     htmlStructureStr = htmlStructureStr.Replace("0000000001", value.ToString("D10")) 

     value = byteCounter.GetByteCount(Left(htmlStructureStr, htmlStructureStr.IndexOf("<!--StartFragment-->"))) 
     htmlStructureStr = htmlStructureStr.Replace("0000000002", value.ToString("D10")) 

     value = byteCounter.GetByteCount(Left(htmlStructureStr, htmlStructureStr.IndexOf("<!--EndFragment-->"))) 
     htmlStructureStr = htmlStructureStr.Replace("0000000003", value.ToString("D10")) 

     value = byteCounter.GetByteCount(Left(htmlStructureStr, htmlStructureStr.IndexOf("</html>"))) 
     htmlStructureStr = htmlStructureStr.Replace("0000000004", value.ToString("D10")) 
#End Region 

     '----put data to clipboard 

     My.Computer.Clipboard.SetText(htmlStructureStr, TextDataFormat.Html) 

     Return True 

    End Function 

回答

0

MSDN说的是,

通过剪贴板支持的唯一字符集是Unicode中它的UTF-8编码。由于UTF-8和ASCII的第一个字符匹配,所以描述始终是ASCII,但是上下文的字节(从StartHTML开始)可以使用以UTF-8编码的任何其他字符。

使用UTF8编码如下

Dim byteCounter As System.Text.UTF8Encoding = System.Text.Encoding.UTF8 
+0

嗨,是有是bytecounting前行(我错过了第一个在这里写): 'htmlStructure.Replace( “#tableHtml#”,tableContent。 ToString)' – norbre

+0

我无法重现你的错误。你能举一些例子tableContent吗? – TRiNE

+0

下面是一个例子... 这是一个调试打印'htmlStructureStr'之前提出到剪贴板: [链接](http://sandbox.onlinephpfunctions.com/code/b8f3b953a1e531f6bc72775472a092181c05d85e) 这是怎么回事看起来像粘贴到Excel后: [链接](http://37.220.129.55/owncloud/index.php/s/j5o1qv2wmrkCIna) – norbre