Cannot set background color to cell

Hello, i'm trying to set background color of cell but nothing works. Text is nicely in cell, backrgound color stays white. I'm using Visual Studio 2010 c#, aspose.cells version is 4.7.1.0. Template was made in Excel 2010, but cannot create xlsx file, xls only. Here is the code:

Cells celice = Worksheets[List].Cells;
Style celicaStil = null;

celice["A6"].PutValue("TEst");

celicaStil = celice["A6"].GetStyle();
celicaStil.BackgroundColor = Worksheets[List].Workbook.Colors[23];
celicaStil.Pattern = BackgroundType.Solid;

celice["A6"].SetStyle(celicaStil);

Can you help me.

Thanks, Marko

Hi,

Thank you for using Aspose.Cells.

Please have a look at the following code. It will fulfill your requirement. I have also attached the output excel file as well for your reference. I hope this code sample helps you get your desired results.

Workbook workbook = new Workbook();

Cell cell = workbook.Worksheets[0].Cells[“A1”];

cell.PutValue(“Hello”);

Style style = cell.GetStyle();

style.Pattern = BackgroundType.Solid;

style.ForegroundColor = Color.Red;

cell.SetStyle(style);

workbook.Save(“K://TT.xls”);

ProgSkupinaZRSZ:

Hello, i'm trying to set background color of cell but nothing works. Text is nicely in cell, backrgound color stays white. I'm using Visual Studio 2010 c#, aspose.cells version is 4.7.1.0. Template was made in Excel 2010, but cannot create xlsx file, xls only. Here is the code:

Cells celice = Worksheets[List].Cells;
Style celicaStil = null;

celice["A6"].PutValue("TEst");

celicaStil = celice["A6"].GetStyle();
celicaStil.BackgroundColor = Worksheets[List].Workbook.Colors[23];
celicaStil.Pattern = BackgroundType.Solid;

celice["A6"].SetStyle(celicaStil);

Can you help me.

Thanks, Marko

Hi,

Thanks for your posting and using Aspose.Cells for .NET.

I have looked into your code and find it is correct. But you need to use Style.ForegroundColor property and not the Style.BackgroundColor property.

Please change your following lines of code

celicaStil.BackgroundColor = Worksheets[List].Workbook.Colors[23];
celicaStil.Pattern = BackgroundType.Solid;

into this.

celicaStil.Pattern = BackgroundType.Solid;
celicaStil.ForegroundColor = Worksheets[List].Workbook.Colors[23];

It should fix your problem. If you still encounter any issue, please feel free to post, we will help you asap.

Below is a sample code and screenshot for your reference. You can get the output.xlsx file from attachment.

C#
Workbook workbook = new Workbook();

Worksheet worksheet = workbook.Worksheets[0];

Cell cell = worksheet.Cells["C3"];

Style style = cell.GetStyle();
style.Pattern = BackgroundType.Solid;
style.ForegroundColor = Color.Green;
cell.SetStyle(style);


workbook.Save(@"F:\Shak-Data-RW\Downloads\output.xlsx");

Screenshot:

That was very quick, thanks again.

I tryed,

Cells celice = Worksheets[List].Cells;
Style celicaStil = null;

celice["A1"].PutValue("TEst");

celicaStil = celice["A1"].GetStyle();
celicaStil.ForegroundColor = Color.Black; //Worksheets[List].Workbook.Colors[17];
celicaStil.Pattern = BackgroundType.Solid;

celicaStil.Font.Color = Color.White;

celice["A1"].SetStyle(celicaStil);

and worked fine.

Best regards, Marko

Hi,

Thanks for your feedback.

It’s good to know that Style.ForegroundColor property resolves your problem.

If you encounter any other issue or have some other questions to be asked regarding Aspose.Cells, please feel free to post, we will be glad to help you.

Hi,


Moreover, you should be familiar with what ForegroundColor and BackgroundColor properties do or are used for and you should not get confused with ForegroundColor and BackgroundColor attributes. Well, there are three types of color normally applied to a cell,
i.e.,
1) ForegroundColor – > it refers to outline color that is used to fill color or set cell shading color
2) BackgroundColor —> it refers to background color, occasionally used for customized pattern types.
3) Font Color --> it refers to font text color


When you want to set a solid fill color (fill/shading color) of a cell you may use ForgroundColor property. The BackgoundColor normally applied when you choose to set pattern’s BackgroundType (enum) to other than Solid/None.

Hope, you understand now.

Thank you.
1 Like