2017-02-16 13 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