如何在 VB.NET 中读取 HTML 表格单元格的背景色并同步到 Excel
技术百科
花韻仙語
发布时间:2026-01-26
浏览: 次 本文详解如何从 webbrowser 控件中的 html 表格提取单元格背景色(background-color),并通过 colortranslator 正确转换为 excel 单元格填充色,避免因样式字符串解析错误导致的运行时异常。
在使用 WebBrowser.Document 解析 HTML 表格并导出至 Excel 时,HtmlElement.Style 属性返回的是完整 CSS 样式字符串(如 "BACKGROUND-COLOR: #b0c4de; BORDER-TOP: #aaaaaa 1px solid;"),不能直接用 cols(j).Style("backcolor") 访问——该语法非法,VB.NET 不支持通过索引器读取样式属性,故会抛出“无法将颜色值转换为整数”的异常。
正确做法是:先获取完整的 Style 字符串,再手动解析 BACKGROUND-COLOR 值。注意以下关键点:
- Style 属性返回的 CSS 属性名统一为大写、带连字符(如 BACKGROUND-COLOR),且值前可能含空格;
- 需对 : 和分号 ; 做健壮分割,并 Trim 空格;
- ColorTranslator.FromHtml() 仅接受标准十六进制格式(如 #b0c4de 或 #B0C4DE),不支持 rgb(176, 196, 222) 或命名色(如 lightsteelblue)——若需兼容,应额外添加 RGB 解析逻辑。
以下是整合进原导出逻辑的完整示例代码(含错误防护):
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
Dim cellText As String = If(cols(j).InnerText, "")
Dim excelCell As Excel.Range = worksheet.Cells(i + 1, j + 1)
excelCell.Value = cellText
' ✅ 安全读取背景色并应用到 Excel 单元格
If Not String.IsNullOrEmpty(cols(j).Style) Then
Dim styleParts() As String = cols(j).Style.Split(";"c)
For Each part As String In styleParts
Dim trimmedPart As String = part.Trim()
If trimmedPart.StartsWith("BACKGROUND-COLOR:", StringComparison.OrdinalIgnoreCase) Then
Dim colorValue As String = trimmedPart.Substring("BACKGROUND-COLOR:".Length).Trim()
' 移除末尾可能的分号(虽已分割,但防冗余)
colorValue = colorValue.Trim(";"c, " "c)
Try
Dim c As Color = ColorTranslator.FromHtml(colorValue)
excelCell.Interior.Color = c.ToArgb() ' 注意:Excel 使用 ARGB 整数(而非 Color 对象)
Catch ex As ArgumentException
' 忽略无效颜色值(如 transparent、inherit 或 rgb())
Continue For
End Try
Exit For ' 找到即退出,避免重复处理
End If
Next
End If
Next
Next? 重要注意事项:
- Excel 的 Interior.Color 属性接收 32 位 ARGB 整数(Color.ToArgb()),而非 System.Drawing.Color 对象,务必调用 .ToArgb();
- 若 HTML 中使用 transparent、initial 或 rgb(r,g,b) 等非十六进制格式,FromHtml() 会抛异常,建议用 Try...Catch 包裹并跳过;
- WebBrowser 渲染引擎(Trident/IE)可能将内联样式与 CSS 类合并后输出 Style,但 Style 属性仅返回内联样式(style="..."),不包含外部或
- 导出后建议调用 excelApp.Visible = True 方便调试,正式部署时设为 False 并记得 workbook.Close(SaveCha
nges:=True) 和 excelApp.Quit() 释放资源。
通过上述方法,即可精准还原 HTML 表格的视觉样式至 Excel,提升数据导出的专业性与一致性。
# 的是
# excel
# 能将
# 而非
# 如需
# 单元格
# app
# css
# 设为
# 不支持
# win
# 对象
# html
# 字符串
# .net
# try
# catch
# 抛出
# border
# background
# 转换为
# 背景色
# 字符串解析
相关栏目:
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
AI推广<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
SEO优化<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
技术百科<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
谷歌推广<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
百度推广<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
网络营销<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
案例网站<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
精选文章<?muma echo $count; ?>
】
相关推荐
- c++ unordered_map怎么用 c++哈
- Win11怎么更改任务栏颜色_Windows11个
- Win10路由器怎么隐藏ssid Win10隐藏w
- Win11怎么查看显卡温度 Win11任务管理器查
- Windows 10自带杀毒软件在哪_Window
- 如何在Golang中使用log包输出不同级别日志_
- Mac的“预览”如何合并多个PDF_Mac文件处理
- Mac系统更新下载慢或失败怎么办_解决macOS升
- php485返回空数组怎么回事_php485数据接
- Mac自带的词典App怎么用_Mac添加和使用多语
- Windows10如何删除Windows.old_
- PHP 中 require() 语句返回值的用法详
- Win10怎样安装Excel数据分析工具_Win1
- C#怎么使用委托和事件 C# delegate与e
- MAC怎么设置程序窗口永远最前_MAC窗口置顶插件
- 短链接还原php提示内存不足_调整PHP内存限制设
- c++的STL算法库find怎么用 在容器中查找指
- php做exe支持多线程吗_并发处理实现方式【详解
- 如何将竖排文本文件转换为横排字符串
- php打包exe如何加密代码_防反编译保护方法【技
- Win11怎么格式化U盘_Win11系统U盘格式化
- Golang如何测试HTTP中间件_Golang
- Win11怎么开启远程桌面连接_Windows11
- Win11怎么设置鼠标宏_Win11鼠标按键自定义
- WindowsUSB驱动安装异常怎么办_USB驱动
- Win11声音太小怎么办_Windows 11开启
- c++怎么使用std::filesystem遍历文
- Windows11怎样开启游戏模式_Windows
- Python路径拼接规范_跨平台处理说明【指导】
- 如何使用Golang实现容器安全扫描_Golang
- Python文件管理规范_工程实践说明【指导】
- Win11怎么清理C盘虚拟内存_Win11清理虚拟
- c# 如何用c#实现一个支持优先级的任务队列
- Go 中 defer 在 goroutine 内部
- 如何在 Go 项目开发中正确处理本地包导入与远程模
- Windows服务持续崩溃怎样修复_系统服务保护机
- Windows10无法连接到Internet_Wi
- php删除数据怎么清空表_truncate与del
- 短链接怎么用php递归还原_多层加密链接的处理法【
- 如何在Golang中解压文件_Golang com
- Win11怎么设置按流量计费_Win11限制后台流
- c# Task.Yield 的作用是什么 它和Ta
- Win10任务栏天气和资讯怎么关闭 Win10禁用
- XML的“混合内容”是什么 怎么用DTD或XSD定
- MySQL 中使用 IF 和 CASE 实现查询字
- Windows10电脑怎么设置虚拟光驱_Win10
- php文件怎么变mp4保存_php输出视频流保存为
- 如何在Golang中写入XML文件_生成符合规范的
- Win11怎么设置默认PDF阅读器 Win11修改
- Win11怎么检查TPM2.0模块_Windows


QQ客服