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 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改