英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论