vb.net如何读取HTML表格中的单元格颜色

huangapple go评论108阅读模式
英文:

vb.net How to read cell color in HTML table

问题

Dim htmlDoc As HtmlDocument = WebBrowser1.Document
Dim table As HtmlElement = htmlDoc.GetElementById("Table1")
Dim rows As HtmlElementCollection = table.GetElementsByTagName("tr")
Dim excelApp As Excel.Application = New Excel.Application
Dim workbook As Excel.Workbook = excelApp.Workbooks.Add()
Dim worksheet As Excel.Worksheet = workbook.Worksheets(1)

For i As Integer = 0 To rows.Count - 1
Dim cols As HtmlElementCollection = rows(i).GetElementsByTagName("td")
For j As Integer = 0 To cols.Count - 1
worksheet.Cells(i + 1, j + 1) = cols(j).InnerText
Next
Next

The code above works fine.

I'd like now to copy the cells color so I have tried this code:

Dim color As String = cols(j).Style("background-color")
Dim cell As Excel.Range = worksheet.Cells(i + 1, j + 1)
cell.Interior.Color = ColorTranslator.FromHtml(color)

And unfortunately it does not work.
The line cols(j).Style("background-color") returns an exception error: cannot convert color value to integer value.

Any idea?

英文:

I need to export an HTML table from a webbrowser to an Excel file.

    Dim htmlDoc As HtmlDocument = WebBrowser1.Document
    Dim table As HtmlElement = htmlDoc.GetElementById("Table1")
    Dim rows As HtmlElementCollection = table.GetElementsByTagName("tr")
    Dim excelApp As Excel.Application = New Excel.Application
    Dim workbook As Excel.Workbook = excelApp.Workbooks.Add()
    Dim worksheet As Excel.Worksheet = workbook.Worksheets(1)

    
    For i As Integer = 0 To rows.Count - 1
        Dim cols As HtmlElementCollection = rows(i).GetElementsByTagName("td")
        For j As Integer = 0 To cols.Count - 1
            worksheet.Cells(i + 1, j + 1) = cols(j).InnerText
        Next
    Next

The code above works fine.

I'd like now to copy the cells color so I have tried this code :

Dim color As String = cols(j).Style(backcolor)
Dim cell As Excel.Range = worksheet.Cells(i + 1, j + 1)
cell.Interior.Color = ColorTranslator.FromHtml(color)

And unfortunately it does not work.
The line cols(j).Style(backcolor) returns an exception error : cannot convert color value to integer value

Any idea ?

答案1

得分: 0

你只能使用htmlElement.style函数从htmlElement中获取完整的样式字符串。结果看起来像这样:

BORDER-TOP: #aaaaaa 1px solid; BORDER-RIGHT: #aaaaaa 1px solid; BORDER-BOTTOM: #aaaaaa 1px solid; BORDER-LEFT: #aaaaaa 1px solid; BACKGROUND-COLOR: #b0c4de

所以你必须自己从中提取样式元素。
使用split函数的示例,因为元素是由';'分隔的:

Dim StrStyle As String = cols(1).Style
            Dim color As String
            For Each param As String In StrStyle.Split(";")
                If param.Contains("BACKGROUND-COLOR") Then
                    color = param.Split(":")(1)
                    Dim c As Color = ColorTranslator.FromHtml(color)
                End If
            Next
英文:

you can only get full style string from htmlElement with htmlElement.style function. result looks like

BORDER-TOP: #aaaaaa 1px solid; BORDER-RIGHT: #aaaaaa 1px solid; BORDER-BOTTOM: #aaaaaa 1px solid; BORDER-LEFT: #aaaaaa 1px solid; BACKGROUND-COLOR: #b0c4de

so you must extract style element from it by your own.
Example with split function as elements are separated by ';':

Dim StrStyle As String = cols(1).Style
            Dim color As String
            For Each param As String In StrStyle.Split(";")
                If param.Contains("BACKGROUND-COLOR") Then
                    color = param.Split(":")(1)
                    Dim c As Color = ColorTranslator.FromHtml(color)
                End If
            Next

huangapple
  • 本文由 发表于 2023年3月7日 17:13:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75659972.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定