0

I have this code to try to set the background color of a cell (among other things):

private static readonly Color CONTRACT_ITEM_COLOR = Color.FromArgb(255, 255, 204);
. . .
cell = pivotTableSheet.Cells[4, 0];
cell.PutValue(AnnualContractProductsLabel);
style = cell.GetStyle();
style.HorizontalAlignment = TextAlignmentType.Center;
style.VerticalAlignment = TextAlignmentType.Center;
style.Font.IsBold = true;
pivotTableSheet.Cells.SetRowHeight(4, 25);
style.BackgroundColor = CONTRACT_ITEM_COLOR;
pivotTableSheet.Cells[4, 0].SetStyle(style);

The setting of horizontal and vertical alignment works, as does bold and height - everything but color:

enter image description here

What is yet needed? I have even tried setting ForegroundColor as well as Background colors, to :

style.ForegroundColor = Color.Red;
style.BackgroundColor = Color.Blue;

...but neither does anything - the cell still looks exactly the same as the screenshot above.

B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862

3 Answers3

5

Please change your code segment to (see the highlighted lines): e.g Sample code:

. . .
cell = pivotTableSheet.Cells[4, 0];
cell.PutValue(AnnualContractProductsLabel);
style = cell.GetStyle();
style.HorizontalAlignment = TextAlignmentType.Center;
style.VerticalAlignment = TextAlignmentType.Center;
style.Font.IsBold = true;
pivotTableSheet.Cells.SetRowHeight(4, 25);
**style.ForegroundColor = CONTRACT_ITEM_COLOR;
style.Pattern = BackgroundType.Solid;**
pivotTableSheet.Cells[4, 0].SetStyle(style);

..........

it should work fine.

I am working as Support developer/ Evangelist at Aspose.

Amjad Sahi
  • 1,813
  • 1
  • 10
  • 15
1

Sometimes setting background works, sometimes it doesn't. Aspose is full of bugs -- ClosedXML is much more reliable but harder to use. Wish I would not have spent the money on a product that was developed by third party in third world county.

var cells = worksheet.Cells;     //get cells collection from active worksheet <br>
var srcCells = workbook.Worksheets[1].Cells;<br>

for (int i = 0; i<rowCount; i++)<br>
{<br>
    var srcStyle = srcCells[i, 0].GetStyle();<br>
    var destStyle = cells[i, 0].GetStyle();<br>
    destStyle.Pattern = BackgroundType.Solid;<br>
    destStyle.ForegroundColor = Color.FromArgb(srcStyle.ForegroundArgbColor);<br>
    cells[i, 0].SetStyle(destStyle);<br>
}<br>

Above Code does not work. srcStyle Foreground color argb is 170,215,255.
Debugging code destStyle ForegroundColor is set to 170,215,255 but when saved as xlsx all the cell backgrounds are white.

In ClosedXML code the following code works perfectly

worksheet.Row(i).Cell(0).Style.BackgroundColor = Color.FromArgb(argb)

Conclusion: Save $$$$$ and use ClosedXML

Peanut
  • 3,753
  • 3
  • 31
  • 45
OldGeek
  • 111
  • 1
  • We need to evaluate the file first. OldGeek, could you share your sample file, we can check it. You may upload the sample file to dropbox or Google drive, etc. and share download link. PS. I am working as Support developer/ Evangelist at Aspose – Amjad Sahi Sep 12 '19 at 19:10
0

enter image description here

all is fine only ForegroundColor is not showing

  • Please add a line to your code segment: style.Pattern = BackgroundType.Solid; it should work fine. – Amjad Sahi Oct 24 '22 at 18:22
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 25 '22 at 14:41
  • Thank you very much. Thanks very much. I disabled that code - @ Amjad Sahi – Duy Trần Oct 26 '22 at 01:35