Cellformat question

Hi, I test the following program:

Dim e As New Excel()
Dim s As Style = e.Styles.GetAt(e.Styles.Add())
s.BackgroundColor = Color.Brown
s.ForegroundColor = Color.Yellow
e.Worksheets.GetAt(0).Cells().GetAt(0, 0).PutValue(“Test text in A1”)
e.Worksheets.GetAt(0).Cells().GetAt(0, 0).Style = s

e.Worksheets.GetAt(0).Cells().GetAt(1, 0).PutValue(“Test text in B1”)
e.Worksheets.GetAt(0).Cells().GetAt(1, 0).Style = s

’e.Worksheets.GetAt(0).Cells().GetAt(1, 0).Format.HorizontalAlignment = TextAlignmentType.Center

e.Save(“test.xls”, FileFormatType.Default)


If I descomment the horizontal alignment setting instruction I lost the background and foreground color setted previously in the style of those cells.

Also I think the background and foreground color are inverted.

Why when I set a horizontal alignment to B1 cell I lost the style also in A1 ?

Why when I set any value in cellformat of a cell, this and other cells lost the style their had set?

Which is the relation of cellformat and styles?, is there also a limit of cellformat objects?


Thank You
Alejandro

Dear Alejandro,

Currently, Aspose.Excel cannot combine style set by different ways.

I suggest to use Style to set the styles of cells contain have same styles, and use Format to set seperate cell’s style. The style set by Format will override the style set by Style.

So I suggest you change your code to:


Dim e As New Excel()
Dim s1 As Style = e.Styles.GetAt(e.Styles.Add())
Dim s2 As Style = e.Styles.GetAt(e.Styles.Add())

s1.BackgroundColor = Color.Brown
s1.ForegroundColor = Color.Yellow
e.Worksheets.GetAt(0).Cells().GetAt(0, 0).PutValue(“Test text in A1”)
e.Worksheets.GetAt(0).Cells().GetAt(0, 0).Style = s1

s2.BackgroundColor = Color.Brown
s2.ForegroundColor = Color.Yellow
s2.HorizontalAlignment = TextAlignmentType.Center

e.Worksheets.GetAt(0).Cells().GetAt(1, 0).PutValue(“Test text in B1”)
e.Worksheets.GetAt(0).Cells().GetAt(1, 0).Style = s2


e.Save(“test.xls”, FileFormatType.Default)

Hi Laurence,
why there isn’t borders line properties in CellFormat ?

Thank You.
Alejandro

Another question, is possible to change the background color of a cell using CellFormat?,
or I must define a style for this purpose?, in that case, is possible to combine cellFormat with style?

Thank You
Alejandro

Dear Alejandro,

You should define a style for setting background color. Currently, you can set a cell style by CellFormat or Style.

I find there are some confusion to use CellFormat and Style. Maybe I will cancel CellFormat in future release. Users can only use Cell.Style to set cell style. How do you think about my idea?

I think CellFormat and Cell.Style was designed for different purpose.

I think CellFormat have to be used when you want set the style properties for one particular cell, and Cell.Style when you want to define a “set” of properties you want to apply to many cells. Is that correct?


In the case CellFormat and Cell.Style are the same, I think your idea is good.

Thank You
Alejandro

Dear Alejandro,

Thanks for your suggestion.

When I designed Aspose.Excel, CellFormat and Cell.Style was designed for different purpose as you thought. But from feedback of users, I think that caused confusion.

So in next release I will remove CellFormat. Users can use Cell.Style to set a “single” style propreties or a “set” of style propreties to one particular cell or many cells.