CoolBreeze
CoolBreeze

Reputation: 381

How to set text and background color in Spreadsheetgear?

In SpreadsheetGear 2012 I want to set:

I've tried variations of:

Sheet_Obj.Cells(0,0).Style.NumberFormat = "@"
Sheet_Obj.Cells(0,0).Interior.Color = Spreadsheetgear.Color.FromArgb(&H0)
Sheet_Obj.Cells(0,0).Style.Font.Color = SpreadsheetGear.Color.FromArgb(&HFFFFFF)
Sheet_Obj.Cells(0,0).Value = "Terabytes"

But have not been able to get anything to work.

Example:

Example

Upvotes: 0

Views: 2631

Answers (1)

Tim Andersen
Tim Andersen

Reputation: 3184

Your code is mixing up applying formatting directly to the cell itself versus the cell Style that the cell uses. Please see my answer here for more details on this.

If you only want to affect a particular cell with a particular format, you would need to use IRange.Interior / IRange.Font.Color / etc. Example:

Dim workbook As SpreadsheetGear.IWorkbook = SpreadsheetGear.Factory.GetWorkbook()
Dim Sheet_Obj As SpreadsheetGear.IWorksheet = workbook.ActiveWorksheet

' This modifies the format of the cell directly.
Sheet_Obj.Cells(0, 0).Interior.Color = SpreadsheetGear.Colors.Black
Sheet_Obj.Cells(0, 0).Font.Color = SpreadsheetGear.Colors.White
Sheet_Obj.Cells(0, 0).NumberFormat = "@"

If you want all cells in a workbook to have a particular format, then modifying the Style for which those cells use (which is by default the "Normal" style) would be a good way to go about it. You can access the current IStyle used by a particular cell via IRange.Style. You can access the entire collection of available IStyles via the IWorkbook.Styles collection. Example:

' This modifies whatever IStyle is currently used by this cell (probably the "Normal" 
' style).  All other cells which also use this still will be affected as well.
Sheet_Obj.Cells(0, 0).Style.Interior.Color = SpreadsheetGear.Colors.Black
Sheet_Obj.Cells(0, 0).Style.Font.Color = SpreadsheetGear.Colors.White
Sheet_Obj.Cells(0, 0).Style.NumberFormat = "@"

' Assuming you are modifying the "Normal" style, this code is equivalent to the above code.
Dim style As SpreadsheetGear.IStyle = workbook.Styles("Normal")
style.Interior.Color = SpreadsheetGear.Colors.Black
style.Font.Color = SpreadsheetGear.Colors.White
style.NumberFormat = "@"

Upvotes: 2

Related Questions