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

Free Support Forum - aspose.com

Background color of cell is not set using aspose.cells for java

Hi,

Following code from aspose programmers guide,

If you see in the below code, below line has to set the background color of the cell. But the generated output file not setting the background color to yellow. This code only sets the font color to red.
stl.setBackgroundColor(Color.getYellow());

Please let me know why above line does not set background color which is given by Aspose only.

package programmersguide.asposecells.workingwithdata.addonfeatures.namedranges.formatranges1.java;

import com.aspose.cells.*;

public class FormatRanges1
{
public static void main(String[] args) throws Exception
{
// The path to the documents directory.
String dataDir = "src/programmersguide/asposecells/workingwithdata/addonfeatures/namedranges/formatranges1/data/";

//Instantiate a new Workbook.
Workbook workbook = new Workbook();

//Get the first worksheet in the book.
Worksheet WS = workbook.getWorksheets().get(0);

//Create a named range of cells.
com.aspose.cells.Range range = WS.getCells().createRange(1, 1, 1, 17);
range.setName("MyRange");

//Declare a style object.
Style stl;

//Create the style object with respect to the style of a cell.
stl= WS.getCells().get(1,1).getStyle();

//Specify some Font settings.
stl.getFont().setName("Arial");
stl.getFont().setBold(true);

//Set the font text color
stl.getFont().setColor(Color.getRed());

//To Set the fill color of the range, you may use ForegroundColor with
//solid Pattern setting.
stl.setBackgroundColor(Color.getYellow());
stl.setPattern(BackgroundType.SOLID);

//Apply the style to the range.
for (int r = 1;r<2;r++)
{
for (int c = 1; c<18;c++)
{
WS.getCells().get(r,c).setStyle(stl);
}

}

//Save the excel file.
workbook.save(dataDir + "rangestyles.xls");

// Print message
System.out.println("Process completed successfully");
}
}

Hi,


Please change the line of code, i.e.,

//To Set the fill color of the range, you may use ForegroundColor with
//solid Pattern setting.
stl.setBackgroundColor(Color.getYellow());
stl.setPattern(BackgroundType.SOLID);


with:
//To Set the fill color of the range, you may use ForegroundColor with
//solid Pattern setting.
stl.setForegroundColor(Color.getYellow());
stl.setPattern(BackgroundType.SOLID);


For your information, when the Pattern is either NONE or Solid, the BackgroundColor won’t be applied, so you need to set Foreground color here, so you will use setForegroundColor() method instead.

Let us know if you still find any issue.

Thank you.

Thanks Amjad for your quick response. That clarifies my question.


Referring to your statement below,
"when the Pattern is either NONE or Solid, the BackgroundColor won’t be applied, so you need to set Foreground color here, so you will use setForegroundColor() method instead"

if the patter isother than NON or Solicd, how do we set the backgroundcolor without using setForegroundColor()


thanks

Thanks faiz for the response and the output file.

Hi,


Well, if the Pattern is neither None nor Solid, you would use setBackgroundColor() method, please see the document on how to specify background color and patterns for your complete reference:
http://www.aspose.com/docs/display/cellsjava/Colors+and+Background+Patterns

Thank you.

In your previous post mentioned as below which I think is opposite to current statement "Well, if the Pattern is None or Solid, you would use setBackgroundColor() method,"

previous post:
"when the Pattern is either NONE or Solid, the BackgroundColor won't be applied, so you need to set Foreground color here, so you will use setForegroundColor() method instead"

thanks

Hi,


Sorry for the confusion.

Actually I meant: If the pattern is neither None nor Solid, then you should use setBackgroundColor() method to apply cell shading color.

I have corrected my previous post accordingly.

Thank you.