Style Not Working in Do Loop

The following style is not being applied inside a Do Loop:

style2.Font.Color = Color.White

style2.Font.IsBold = True

style2.ForegroundColor = Color.Navy

style2.BackgroundColor = Color.Navy

style2.HorizontalAlignment = TextAlignmentType.Center

range = sheets(0).Cells.CreateRange(x - 1, 2, 2, 3)

range.Style = style2

range = sheets(0).Cells.CreateRange(x - 1, 10, 2, 3)

range.Style = style2

Any idea why? using 3.6.1.0

Could you please post your whole code here? And please add this line of code:

style2.ForegroundColor = Color.Navy

style2.BackgroundColor = Color.Navy

style2.Pattern = BackgroundType.Solid

style2.HorizontalAlignment = TextAlignmentType.Center

I have attached a text file with the code. I truncated some of my queries to protect private data.

Thank you.

David

What's the problem? I test with the following sample code and don't find any problem:

Dim count As Integer = 0
Dim range As Range
Do While count < 5

Dim excel As Excel = New Excel()
Dim style = excel.DefaultStyle
Dim sheets As Worksheets = excel.Worksheets
style.Font.Name = "Arial Narrow"
excel.DefaultStyle = style
Dim cells As Cells = excel.Worksheets(0).Cells
Dim cell As Cell = cells(0, 0)
Dim style1 As Style = excel.Styles(excel.Styles.Add())
Dim style2 As Style = excel.Styles(excel.Styles.Add())
Dim style3 As Style = excel.Styles(excel.Styles.Add())

excel.ChangePalette(Color.Navy, 55)


style2.Font.Name = "Arial Narrow"
style2.Font.Color = Color.White
style2.Font.IsBold = True
style2.ForegroundColor = Color.Navy
style2.BackgroundColor = Color.Navy
style2.Pattern = BackgroundType.Solid
style2.HorizontalAlignment = TextAlignmentType.Center

range = sheets(0).Cells.CreateRange(0, 2, 2, 3)
Range.Style = style2
range = sheets(0).Cells.CreateRange(0, 10, 2, 3)
range.Style = style2

style1.Font.Name = "Arial Narrow"
style1.Font.Size = 9
style1.Custom = "#,##0;[Red]-#,##0"
range = sheets(0).Cells.CreateRange(0, 0, 1, 22)
range.Style = style1

excel.Save("d:\test\abc" + count.ToString() + ".xls")

count = count + 1
Loop

I think your problem may be caused by:

1. The Navy color is not added into the palette.

excel.ChangePalette(Color.Navy, 55)

2. style2 setting is overwritten by style1

And please try this attached fix. I add some new features and enhancement in it.