Unexpected Font color in cell


#1

Hello,

I have an issue with the code below. I expect a green color (0,140,95), but when I open excel, the color I get is a kind of blue (0,128,128). I don’t know where it comes from. Could you please help me ?

Thanks a lot !

import com.aspose.cells.Cell;
import com.aspose.cells.Cells;
import com.aspose.cells.Color;
import com.aspose.cells.FileFormatType;
import com.aspose.cells.Workbook;
import com.aspose.cells.Worksheet;

public class AsposeCellTest {

public static void main(String[] args) throws Exception {
	
	Workbook workbook = new Workbook();
	Worksheet worksheet = workbook.getWorksheets().get(0);
	Cells cells = worksheet.getCells();
	Cell cell0 = cells.get(0,0);
	cell0.setValue("Hello");
	cell0.getStyle().getFont().setColor(Color.fromArgb(0, 140, 95));
	cell0.characters(0, 5).getFont().setColor(Color.fromArgb(0, 140, 95));
	
	//Excel file byte array building
	workbook.save("C:\\Users\\c62902\\Desktop\\" + "MyBook.xls", FileFormatType.EXCEL_97_TO_2003);
}

}


#2

@bpoussin,

Thanks for the sample code segment and details.

For XLS file format, you need to add your desired custom color (other than default colors (e.g red, blue, green, yellow and so on… etc.)) to MS Excel color palette first, so that you could set your desired font color accordingly. For your information, For XLS file format, only 56 colors (0 - 55 indexed) are there on standard color palette. For advanced file formats, e.g XLSX, XLSB, XLSM, etc. you do not need to perform this step as these file formats have unlimited colors on the palette. Please see the following sample code for your reference and try it, it works fine as I tested:
e.g
Sample code:

Workbook workbook = new Workbook();
//Notice this line of code. It will add the custom color to 56 indexed position to the palette.
workbook.changePalette(Color.fromArgb(0, 140, 95),55);
Worksheet worksheet = workbook.getWorksheets().get(0);
Cells cells = worksheet.getCells();
Cell cell0 = cells.get(0,0);
cell0.setValue("Hello");
		
//Always create style object for the cell and specify your desired attribute and set the style.
//Style style = cell0.getStyle();
//style.getFont().setColor(Color.fromArgb(0, 140, 95));
//cell0.setStyle(style);

//Or
 cell0.characters(0, 5).getFont().setColor(Color.fromArgb(0, 140, 95));
		
		
//Excel file byte array building
workbook.save("f:\\files\\out1.xls", FileFormatType.EXCEL_97_TO_2003);
//When you save to XLSX file format, you won't need to use the line. "workbook.changePalette(Color.fromArgb(0, 140, 95),55);".
//workbook.save("f:\\files\\out12.xlsx", SaveFormat.XLSX);

Also, see the document for your complete reference:
https://docs.aspose.com/display/cellsjava/Cells+Formatting#CellsFormatting-ColorsandPalette

Hope, this helps a bit.


#3

Thanks a lot, it solved my problem !


#4

@bpoussin,

Good to know that your issue is sorted out by the suggested code. Feel free to contact us any time if you need further help or have some other issue or queries, we will be happy to assist you soon.