C#、Vb.Net获取Excel单元格背景色示例

C#、Vb.Net获取Excel单元格背景色示例,运行电脑需要安装过office,同时在项目中引用com类型库microsoft excel 版本号 object Libray ,如下图。

在项目中引用com类型库microsoft excel 版本号 object Libray

主要是通过获取Range.Interior.Color,而不是Ceil.Style.Interior.Color。

C#获取Excel单元格背景色示例如下

using System;
namespace ConsoleApp1
{
    class Program
    {
        public static string RGB(int color)
        {
            int r = 0xFF & color;
            int g = 0xFF00 & color;
            g >>= 8;
            int b = 0xFF0000 & color;
            b >>= 16;
            return  $"RGB({r}, {g}, {b})";
        }
        static void Main(string[] args)
        {
            Microsoft.Office.Interop.Excel.Application application = new Microsoft.Office.Interop.Excel.Application();
            //打开文件,n.FullPath是文件路径  
            Microsoft.Office.Interop.Excel.Workbook workbook = application.Workbooks.Open(@"E:\Demos\CSharp\ConsoleApp\Demo\d.xlsx", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
            Microsoft.Office.Interop.Excel.Worksheet worksheet = workbook.Worksheets[1];
            Microsoft.Office.Interop.Excel.Range range = worksheet.Range[worksheet.Cells[1, 1], worksheet.Cells[1, 1]];
   
            string color = range.Interior.Color.ToString();
            Console.WriteLine($"{color}==>{RGB(int.Parse(color))}");//红
            range = worksheet.Range[worksheet.Cells[2, 1], worksheet.Cells[2, 1]]; 
            color = range.Interior.Color.ToString();
            Console.WriteLine($"{color}==>{RGB(int.Parse(color))}");//绿
            range = worksheet.Range[worksheet.Cells[3, 1], worksheet.Cells[3, 1]];
            color = range.Interior.Color.ToString();
            Console.WriteLine($"{color}==>{RGB(int.Parse(color))}");//蓝
            
            application.Quit();
            Console.ReadKey();
        }
    }
}

C#获取Excel单元格背景色示例如下

 

Vb.Net获取Excel单元格背景色示例

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

C#、Vb.Net获取Excel单元格背景色示例

加支付宝好友偷能量挖...


原创文章,转载请注明出处:C#、Vb.Net获取Excel单元格背景色示例

评论(0)Web开发网
阅读(120)喜欢(0)Asp.Net/C#/WCF