How to set color or background with "excelpackage"
Asked Answered
V

2

17

I use this package: ExcelPackage though I can't figure out how to set the background color for the cell. I tried to use this:

ws.Cells["A1"].Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;

But it shows that the properties are not found.

enter image description here

Sounds like I should use something similar to this:

worksheet.Cell(5, columnIndex + 1).Style = "background-color: red";

But I am not sure how it works and I couldn't find the tutorial for it. Please help.

Vo answered 17/6, 2013 at 20:19 Comment(1)
Does anyone know how to do it?Vo
N
45

Try something along these lines (Taken from the EPPlus sample files provided):

using (var range = worksheet.Cells[1, 1, 1, 5]) 
    {
        range.Style.Fill.PatternType = ExcelFillStyle.Solid;
        range.Style.Fill.BackgroundColor.SetColor(Color.DarkBlue);
    }
Nickerson answered 17/6, 2013 at 22:5 Comment(1)
@SharpC The numbers represent a range: (FromRow, FromCol, ToRow, ToCol).Monroy
S
0

For ExcelPackage

workSheet.Cells["A1:B1"].Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.LightTrellis;
workSheet.Cells["A1:B1"].Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.LightSeaGreen);
var allCells = workSheet.Cells["A1:B1"];
var cellFont = allCells.Style.Font;
Select answered 5/3, 2018 at 7:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.