We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Can not background color for cell in cells-7.03

Hi,

Here is how I put background color for cell:

///////////////////////
Cells cells = myWorksheet.getCells();
Cell cell =cells.get(myCellName);
Style newStyle = new Style();
newStyle.setBackgroundColor(Color.fromArgb(192, 192,192));
cell.setStyle(newStyle);
cell.setValue(myValue);
/////////////////////////
However the text is printed on white, I intended some gray...

Regards,
Leonid.

Hi,


Please update your code as:

///////////////////////
Cells cells = myWorksheet.getCells();
Cell cell =cells.get(myCellName);
Style newStyle = new Style();
newStyle.setForegroundColor(Color.fromArgb(192, 192,192));
style.setPattern(BackgroundType.SOLID);
cell.setStyle(newStyle);
cell.setValue(myValue);
/////////////////////////

Also, see the document for your reference:
http://www.aspose.com/documentation/java-components/aspose.cells-for-java/colors-background-patterns.html
Look please,
This code just doesn't work. Instead of setting yellow background it sets cell while with no borders.

Worksheet ws = wb.getWorksheets().get(0);
Cell cell = ws.getCells().get("H10");
Style style = new Style();
style.setBackgroundColor(Color.getYellow());
style.setPattern(BackgroundType.SOLID);
cell.setStyle(style);
wb.save(xlsStream, FileFormatType.EXCEL_97_TO_2003);

Hi,


You are still doing a mistake in your code. Please use “setForegroundColor()” method.

See you updated code:

Worksheet ws = wb.getWorksheets().get(0);
Cell cell = ws.getCells().get(“H10”);
Style style = new Style();
style.setForegroundColor(Color.getYellow());
style.setPattern(BackgroundType.SOLID);
cell.setStyle(style);
wb.save(xlsStream, FileFormatType.EXCEL_97_TO_2003);

Hello,

I got the same issue as leonidp, and your code is working correctly. But I don’t understand two things:

  • why do I need to specify a Pattern now, as it was not needed at all in previous Aspose versions (I’m talking of 2.5.x)?
  • I want to set the background color of my cell, so why do I need to use setForegroundColor() method? What is the purpose of setBackgroundColor() then?

Thanks.


Hi romaintaz,


Well, you also needed to set PatternStyle in even older versions with older APIs set, e.g
see the code segment below:

//Accessing the “A2” cell from the worksheet
cell=cells.getCell(“A2”);
style = cell.getStyle();
//Setting the foreground color to blue
style.setColor(Color.BLUE);
//Setting the background pattern to vertical stripe
style.setPatternStyle(PatternType._____);
//Saving the modified style to the “A2” cell.
cell.setStyle(style);


So, pattern setting was needed even if using the older APIs set in older versions too.

Let me explain what is the difference b/w foreground and back ground colors. Well, a user should always use ForegroundColor instead of using BackgroundColor method setters to get the cell’s Shading/fill color. For your knowledge, the Foreground color is the cell’s outline color and Background color is the back color of the cell. But, keeping in mind, if you want to set cell’s shading color with solid fill/pattern type, you will use ForegroundColor setter. There is no need to use Background color then that would be only useful if there is no solid/null background pattern type set for the cell.
For complete reference, see a topic:
http://www.aspose.com/documentation/java-components/aspose.cells-for-java/colors-background-patterns.html

Also, just for your knowledge we have ported .NET code to create Java version now since the release of version v7.0.0. The new APIs set of Java version is equally matched with .NET version with tremendous enhancements and performance.



Thanks for your considerations and understanding!