We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Problem when creating a background color

Hi,
Below is my code.

Dim rowcolorstyl As Aspose.Cells.Style = workbook.Styles(workbook.Styles.Add())
rowcolorstyl.ForegroundColor = Color.Red
rowcolorstyl.Pattern = BackgroundType.Solid
Dim rg As Range
Dim stlflag As Aspose.Cells.StyleFlag = New Aspose.Cells.StyleFlag()
stlflag.CellShading = True

For k As Integer = 0 To 144
If (cells(12 + k, 10).Value.ToString() = “-1” ) Then
rg = workbook.Worksheets(0).Cells.CreateRange(12 + k, 1, 12 + k, 10)
rg.ApplyStyle(rowcolorstyl, stlflag)
End If
Next

According to the code above the background of few rows should be RED.
But that’s not the case here.

the for loop above should iterarte for 144 times and based on the value in the 10 column it should paint the foreground as RED, the first for rows have different value other than -1 and it’s not painting those 4 rows RED , but once it start painting the rows RED there is no break in between, the same continued for more than 144 rows. I have debugged my code and it’s entering the if statement as expected but the foreground color of few other rows are also RED irreespective of the value in the 10th column.

Hi,

Thank you for considering Aspose.

As per my understanding of your issue, you need to change the color of the cells of a row to “Red” when the 10th column value is -1. You need to change the parameters for the CreateRange method. Following is the details of the parameters of the CreateRange API.

public Range CreateRange(int firstRow,int firstColumn,int rowNumber,int columnNumber);

Parameters
firstRow

First row of this range

firstColumn

First column of this range

rowNumber

Number of rows (This parameter sets the number of rows not the row index)

columnNumber

Number of columns

Please modify you code as below to get you desired result:

Dim rowcolorstyl As Aspose.Cells.Style = workbook.Styles(workbook.Styles.Add())

rowcolorstyl.ForegroundColor = Color.Red

rowcolorstyl.Pattern = BackgroundType.Solid

Dim rg As Range

Dim stlflag As Aspose.Cells.StyleFlag = New Aspose.Cells.StyleFlag()

stlflag.CellShading = True

For k As Integer = 0 To 144

If (Cells(12 + k, 10).Value.ToString() = “-1”) Then

rg = workbook.Worksheets(0).Cells.CreateRange(12 + k, 1, 1, 10)

rg.ApplyStyle(rowcolorstyl, stlflag)

End If

Next

Thank You & Best Regards,