MIMIFUT 2023-02-25 09:59 采纳率: 66.7%
浏览 34
已结题

vb.net中如何获取到excel单元格的颜色

vb.net中如何获取到excel单元格的颜色
目前查了很多文章,给单元格设置颜色的很多,但获取颜色值的没查到

下面是我写的代码
但发现获取到的值都是 16777215
不知道该如何正确获取

img

    Function ReadExcel(ByVal FileName As String)
        Dim txt As String
        Dim style1 As Excel.Style
        Dim style2 As Excel.Style
        AppXls = New Microsoft.Office.Interop.Excel.Application
        AppXls.Workbooks.Open(FileName)
        AppXls.Visible = False
        AppWokBook = AppXls.Workbooks(1)
        AppSheet = AppWokBook.Sheets(1)

        style1 = AppSheet.Cells(2, 1).style
        style2 = AppSheet.Cells(2, 2).style
        Label2.Text = style1.Interior.Color
        Label3.Text = style2.Interior.Color
        mydataset = txt
        AppXls.Quit()
        AppWokBook = Nothing
        AppSheet = Nothing
        AppXls = Nothing
        Return mydataset

    End Function

  • 写回答

4条回答 默认 最新

  • CSDN专家-showbo 2023-02-25 13:24
    关注

    获取Range.Interior.Color,不是Ceil.Style.Interior.Color

    示例代码来源:https://www.w3dev.cn/article/20230121/CSharp-Vb.net-Get-Excel-Cell-BackgroudColor-Demo.aspx

    Imports System.Linq
    Imports System.Collections.Generic
    Imports Microsoft.VisualBasic
    Imports System.Data.OleDb
    Module Module1
        Function RGB(color)
            r = &HFF And color
            g = &HFF00 And color
            g >>= 8
            b = &HFF0000 And color
            b >>= 16
    
            Return $"RGB({r}, {g}, {b})"
        End Function
        Sub Main()
            AppXls = New Microsoft.Office.Interop.Excel.Application
            AppXls.Workbooks.Open("E:\Demos\CSharp\ConsoleApp\Demo\d.xlsx")
            AppXls.Visible = False
            AppWokBook = AppXls.Workbooks(1)
            AppSheet = AppWokBook.Sheets(1)
    
            range = AppSheet.Range(AppSheet.Cells(1, 1), AppSheet.Cells(1, 1))
            color = range.Interior.Color.ToString()
            Console.WriteLine($"{color}==>{RGB(Integer.Parse(color))}") 'range = AppSheet.Range(AppSheet.Cells(2, 1), AppSheet.Cells(2, 1))
            color = range.Interior.Color.ToString()
            Console.WriteLine($"{color}==>{RGB(Integer.Parse(color))}") '绿
    
    
    
            range = AppSheet.Range(AppSheet.Cells(3, 1), AppSheet.Cells(3, 1))
            color = range.Interior.Color.ToString()
            Console.WriteLine($"{color}==>{RGB(Integer.Parse(color))}") 'AppXls.Quit()
    
            Console.ReadKey()
    
        End Sub
    
    End Module
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 3月5日
  • 已采纳回答 2月25日
  • 创建了问题 2月25日

悬赏问题

  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因