Cell Style vs Range Style

Hello Laurence, I need help on style precendence rules.

If you set a style for a range, then set the style for a cell, it looks like it applies that style to the entire range. I was expecting it to work like Excel; I can define a style for a range then change one cell and it should take precendence.

I’m confused about how this currently works. I have 2.6.1.

How would you perform the following ?

1. Set a given range of cells to background color, then make only one cell BOLD but keep the background color.

I’m assuming a cell style uses the range name and then applies its own style in an “additive” fashion. This is how Excel works but I can’t figure out how to do this.

Also, this style seems to set background to black no matter what color I choose…

Dim style2 As Style = x._xls.Styles(x._xls.Styles.Add())
style2.Name = Me.sStyleSiteVisitsRange
style2.Font.Size = 10
style2.Pattern = BackgroundType.Solid
style2.BackgroundColor = Color.Gray

’ yields black for my range for some reason and I don’t think its a palette issue - maybe it is…

thanks
Marty

Hi Marty,

Yes. Range.Style applies same style to a whole range. It saves memory and is easier and faster to process.

I will consider you request and think to make it same with MS Excel. But it will take some time to do it.

Currently you can use:

cell.Style = presetStyle

to set cells with same style.

And use

cell.Style.Font.Size = 10

to set cells with different styles.


And the background color you should change your code to

style2.ForegroundColor = Color.Gray

BackgroundColor property only takes effect when the Pattern is not BackgroundType.Solid. That’s the routine of MS Excel.


@MartyOne,
Aspose.Excel is discontinued and no more under active development. It is replaced by another product Aspose.Cells which is much more advanced and rich in terms of feature and performance. Aspose.Cels also contains features for formatting cells using GetStyle and SetStyle methods as demonstrated in the following sample code:

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

// Create directory if it is not already present.
bool IsExists = System.IO.Directory.Exists(dataDir);
if (!IsExists)
    System.IO.Directory.CreateDirectory(dataDir);

// Instantiating a Workbook object
Workbook workbook = new Workbook();

// Obtaining the reference of the first worksheet
Worksheet worksheet = workbook.Worksheets[0];

// Accessing the "A1" cell from the worksheet
Cell cell = worksheet.Cells["A1"];

// Adding some value to the "A1" cell
cell.PutValue("Hello Aspose!");

// Defining a Style object
Aspose.Cells.Style style;

// Get the style from A1 cell
style = cell.GetStyle();

// Setting the vertical alignment
style.VerticalAlignment = TextAlignmentType.Center;

// Setting the horizontal alignment
style.HorizontalAlignment = TextAlignmentType.Center;

// Setting the font color of the text
style.Font.Color = Color.Green;

// Setting to shrink according to the text contained in it
style.ShrinkToFit = true;

// Setting the bottom border color to red
style.Borders[BorderType.BottomBorder].Color = Color.Red;

// Setting the bottom border type to medium
style.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Medium;

// Applying the style to A1 cell
cell.SetStyle(style);

// Saving the Excel file
workbook.Save(dataDir + "book1.out.xls");

More information is available here for applying style:
Format Cells using GetStyle and SetStyle Methods

Refer to the following link for the latest free trial version of this product:
Aspose.Cells for .NET (Latest Version)

Here is the link to a complete solution that can be used to test different features of this product without any effort.