Free Support Forum - aspose.com

Background colour of style not working

I have tried the following code to set a background colour on a cell:

Style titleStyle = workbook.createStyle();
Font titleFont = titleStyle.getFont();
titleFont.setName("Arial");
titleFont.setSize(18);
titleFont.setBold(true)
titleStyle.setPattern(com.aspose.cells.BackgroundType.SOLID);
titleFont.setColor(com.aspose.cells.Color.getWhite());
titleStyle.setBackgroundColor(com.aspose.cells.Color.fromArgb(137,17,127));
titleStyle.setHorizontalAlignment(com.aspose.cells.TextAlignmentType.CENTER);
Cells cells = worksheet.getCells();
Cell cell = cells.get("A1");
cell.setStyle(titleStyle);

When I view the saved spreadsheet the cell has a solid white background with white text.

2018-03-16_13-10-55.png (11.8 KB)

If I change the code to

Style titleStyle = workbook.createStyle();
Font titleFont = titleStyle.getFont();
titleFont.setName("Arial");
titleFont.setSize(18);
titleFont.setBold(true)
titleStyle.setPattern(com.aspose.cells.BackgroundType.VERTICAL_STRIPE);
titleFont.setColor(com.aspose.cells.Color.getWhite());
titleStyle.setBackgroundColor(com.aspose.cells.Color.fromArgb(137,17,127));
titleStyle.setHorizontalAlignment(com.aspose.cells.TextAlignmentType.CENTER);

I get a cell with a vertically striped background with the correct colour.

2018-03-16_13-10-02.png (12.6 KB)

How do I get a solid background of the colour I want?

Thanks
Steve

@SteveHawes,

Thanks for the sample code segments and screenshots.

Please use Style.setForegroundColor() method instead of Style.setBackgroundColor(), it will work accordingly for your needs:
e.g
Sample code:

Style titleStyle = workbook.createStyle();
Font titleFont = titleStyle.getFont();
titleFont.setName("Arial");
titleFont.setSize(18);
titleFont.setBold(true)
titleStyle.setPattern(com.aspose.cells.BackgroundType.SOLID);
titleFont.setColor(com.aspose.cells.Color.getWhite());
titleStyle.setForegroundColor(com.aspose.cells.Color.fromArgb(137,17,127));
titleStyle.setHorizontalAlignment(com.aspose.cells.TextAlignmentType.CENTER);
Cells cells = worksheet.getCells();
Cell cell = cells.get("A1");
cell.setStyle(titleStyle);

Hope, this helps a bit.

That works perfectly but makes no sense whatsoever!!

What exactly does SetBackgroundColor do if SetForegroundColor actually sets the background color?

Thanks
Steve

@SteveHawes

Yes, it seems confusing. However, background color property is only used, when there are two colors. For example, if you are filling the cell with two color gradient fill, then first color will be specified via foreground color and second will be specified via background color.

Also, when fill color is not solid fill, then background color property is also used.

Besides that when you format pivot table cells, background color property is used.

In short, Foreground Color is only used when fill type is SOLID.

e.g.

The following line specifies that the fill type is Solid

titleStyle.setPattern(com.aspose.cells.BackgroundType.SOLID);

OK - I understand how they are used now but in my opinion, a more logical and intuitive way of doing it would have been to use a single method that took two arguments, one of which is optional. ie

setBackgroundColor(Color.getRed()); // this would be the equivalent of setForgroundColor(Color.getRed())
setBackgroundColor(Color.getRed(), Color.getYellow()); // this would be the equivalent of setForgroundColor(Color.getRed()) and setBackgroundColor(Color.getYellow())

Just my thoughts on it.

Thanks
Steve

@SteveHawes

Thanks for your suggestions. We will look into them and employ them if they are feasible for us. We have logged them in our database for analysis. Once, we will have some resolution or other news for you, we will update you asap.

This issue has been logged as

  • CELLSJAVA-42565 - Foreground Color vs Background Color vs Fill Color - Use a single method that takes two arguments

@SteveHawes

Thanks for using Aspose APIs.

Please download and try the following fix and let us know your feedback.


Please try Style.setPatternColor(BackgroundType pattern, Color color1, Color color2) method added in this fix.

The issues you have found earlier (filed as CELLSJAVA-42565) have been fixed in this update. This message was posted using BugNotificationTool from <a href=“https://#{request.env[“HTTPS_HOST”]}”>Downloads module by Amjad_Sahi

The issues you have found earlier (filed as CELLSJAVA-42565) have been fixed in Aspose.Cells for Java 18.4. Please also see the document for your reference:
https://docs.aspose.com/display/cellsjava/Installation