Getting a range of columns for background color

Oct 17, 2011 at 9:49 PM

I would like to know how can I go about getting a range of columns inorder to modify the background color.

My current code is:

            Dim excelPackage = New OfficeOpenXml.ExcelPackage
            Dim excelWorksheet = excelPackage.Workbook.Worksheets.Add("Employee-Record-" & DateTime.Now.Year & DateTime.Now.Month & DateTime.Now.Day)

            Dim colIndex As Integer = 1
            Dim rowIndex As Integer = 1

            Dim xlRow = excelWorksheet.Row(rowIndex)
            Dim xlCell = excelWorksheet.Cells(rowIndex, colIndex)

            Dim CellFill = xlCell.Style.Fill
            Dim RowFill = xlRow.Style.Fill

            CellFill.PatternType = ExcelFillStyle.Solid
            RowFill.PatternType = ExcelFillStyle.Solid
            RowFill.BackgroundColor.SetColor(Drawing.Color.DarkGray)
The above code is for one cell only.
If I want to do (example) Row 2, Column A to Column E, how would I accomplish that?
Oct 17, 2011 at 10:32 PM

I assume that i answered your question recently on SO.

 

So for the sake of completeness:

For row = workSheet.Dimension.Start.Row To workSheet.Dimension.End.Row
    Dim pos = row Mod 3
    Dim rowRange = workSheet.Row(row)
    Dim RowFill = rowRange.Style.Fill
    RowFill.PatternType = ExcelFillStyle.Solid
    Select Case pos
        Case 0
            RowFill.BackgroundColor.SetColor(Drawing.Color.LightGray)
        Case 1
            RowFill.BackgroundColor.SetColor(Drawing.Color.DarkGray)
        Case 2
            RowFill.BackgroundColor.SetColor(Drawing.Color.White)
    End Select
Next