Wrox Programmer Forums
|
.NET Framework 2.0 For discussion of the Microsoft .NET Framework 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the .NET Framework 2.0 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 11th, 2004, 11:58 AM
Registered User
 
Join Date: Aug 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to populate Send a message via Yahoo to populate
Default How to get Excel Cell color

Hi Gurus,

I have a very simple question. How can I get one cel color in excel.

I read the Excel file, and want to get one Cell color. My code is below:
========================CODE====================

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim objExcel As New Excel.Application

        Dim objWrkBk As Excel.Workbook

        Dim objSht As Excel.Worksheet

        Dim objRng As Excel.Range

        Dim strCol, strCell As String

        Dim maxCol, maxRow As Integer

        Dim iRow, iCol As Integer
        Dim objColor As Excel.Font

        maxRow = 2

        maxCol = 2

        objWrkBk = objExcel.Workbooks.Open("K:\LaPonce\test.xls")

        objSht = objWrkBk.Worksheets(1)

        objExcel.Visible = True

        For iCol = 1 To maxCol

            For iRow = 1 To maxRow
                strCol = Chr(Asc(iRow) + 16)
                strCell = strCol + iCol.ToString

                objRng = objSht.Range(strCell)

                MsgBox(objRng.Value)

                objColor = objSht.Cells.Font
                MsgBox(objColor.Color.GetType.GetProperty("color", Reflection.BindingFlags.ExactBinding))
                objColor = Nothing
            Next

        Next



    End Sub


=================================================

THE CODE IN vb.net, BUT C# CODE IS ALSO HIGHLY ACCEPTABLE.

THANKS FOR UR HELP.
HIMADRISH

Thanks!
Populate
 
Old January 5th, 2007, 11:22 AM
Registered User
 
Join Date: Dec 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,
I won't be of any help to you, but your code has helped me to some extent. Thanks for that.
But I have a few doubts . In this code you call open method which opens a new excel sheet. But in my case an excel sheet is already open, I just want to read the excel file.

Another issue is that in your code you have hard coded the number of columns & rows but in my case there can be any number of rows. So I want to read till the end of the file.

Is there a way to tackle the above mentioned problems.
 
Old March 3rd, 2014, 04:25 AM
Registered User
 
Join Date: Feb 2014
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default RE: How to get Excel Cell color

Check this Stackoverflow answer.
So in short Color value can be retrieved from excel's Range object:
Code:
objColor = objRng.Interior.Color
To be honest this solution has its drawbacks.
For few cells it will work great, but if you have to retrieve Color value from more cells than this approach is an overkill. This is because for each individual cell, from which you want to get Color, you will have to call its Range.
Here is an alternative you can try, with this .NET Excel component:
Code:
ExcelFile objWrkBk = ExcelFile.Load(@"K:\LaPonce\test.xls");
ExcelWorksheet objSht = objWrkBk.Worksheets[0];

int maxRow = objSht.Rows.Count;
int maxCol = objSht.CalculateMaxUsedColumns();

for (int iCol = 0; iCol < maxCol; iCol++)
{
    for (int iRow = 0; iRow < maxRow; iRow++)
    {
        string strCell = CellRange.RowColumnToPosition(iRow, iCol);
        ExcelCell cell = objSht.Cells[strCell];

        MsgBox(cell.Value);
        MsgBox(cell.Style.Font.Color);
    }
}





Similar Threads
Thread Thread Starter Forum Replies Last Post
Filling a Oval in a Excel Cell with Color sinajs Excel VBA 0 January 22nd, 2007 01:47 PM
change cell color martin1 Visual Basic 2005 Basics 2 October 4th, 2006 08:55 AM
Change Cell Color phungleon HTML Code Clinic 2 April 9th, 2006 12:36 AM
Listview Cell Color leontun Pro VB 6 2 December 15th, 2004 04:14 PM
Fill color in Excel Cell Anantsharma Excel VBA 14 October 25th, 2004 07:11 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.