Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > .NET > .NET 2.0 and Visual Studio. 2005 > .NET Framework 2.0
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
.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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old September 11th, 2004, 12:58 PM
Registered User
 
Join Date: Aug 2004
Location: Kolkata, West Bengal, India.
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
Reply With Quote
  #2 (permalink)  
Old January 5th, 2007, 11:22 AM
Registered User
 
Join Date: Dec 2006
Location: Mumbai, Maharashtra, India.
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.
Reply With Quote
  #3 (permalink)  
Old March 3rd, 2014, 04:25 AM
Registered User
Points: 21, Level: 1
Points: 21, Level: 1 Points: 21, Level: 1 Points: 21, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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);
    }
}
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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 09:55 AM
Change Cell Color phungleon HTML Code Clinic 2 April 9th, 2006 01: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 08:11 AM



All times are GMT -4. The time now is 05:14 PM.


Powered by vBulletin®
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.