This project has moved. For the latest updates, please go here.

Getting a range of columns for background color

Oct 17, 2011 at 8: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
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 9: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
        Case 1
        Case 2
    End Select