Setting Hexadecimal color code values to the com.aspose.cells.Font

Hi All,

I need to set the Hexadecimal color code values to the com.aspose.cells.Font .

If I use below code

String strColor="#FFFFFF";

Style style=cell.getStyle();

Font font = style.getFont();

font.setColor(strColor);

I am getting error like The method setColor(Color) in the type Font is not applicable for the arguments (String) .

So setColor() only takes Color class methods.

Is there any alternative to set the hexadecimal color value to the com.aspose.cells.Font ? Please suggest me.

Hi,

Thanks for your posting and using Aspose.Cells.

You can convert your Hex string to Integer and then create a Color object from that Integer.

Please see the following code. It converts Hexadecimal color (strColor) into Integer and then create a Color object from it and then it sets the font color of the cell A1 with this color.

I have attached the output.xlsx file generated by the code and screenshot showing the output for your reference.

Java


//This is your Hex string color

String strColor="#FF00FF";

strColor=strColor.replace("#", “”);


//We need to convert Hex string to Integer

Integer intColor = Integer.parseInt(strColor, 16);


//Create Color object from Integer Color

Color clr = Color.fromArgb(intColor);


//Create an empty workbook

Workbook workbook = new Workbook();


//Access the first worksheet

Worksheet worksheet = workbook.getWorksheets().get(0);


//Access the cell A1

Cell cell = worksheet.getCells().get(“A1”);


//Write some value inside it

cell.putValue(“Welcome to Aspose!”);


//Set the font color of this cell with your Hex color

Style style = cell.getStyle();

style.getFont().setColor(clr);

cell.setStyle(style);


//Save the workbook

workbook.save(“output.xlsx”);