Free Support Forum - aspose.com

How to Chnage Font color

Hi, I am trying to change font color to Black


Here is the Code :

Workbook workbook = new Workbook();
FileInputStream fstream = new FileInputStream(“C:\abc.xls”);
workbook.open(fstream, FileFormatType.EXCEL97TO2003);
Worksheet worksheet = workbook.getWorksheets().getSheet(1);
Cells cells = worksheet.getCells();
int num=cells.getMaxDataRow();
int num1=cells.getMaxDataColumn();
System.out.println(cells.getMaxDataColumn());
System.out.println(cells.getMaxDataRow());
for(int n1=0;n1<num;n1++)
{ for(int Colno=0;Colno<=num1;Colno++)
{
if (cells.getCell(n1,Colno).getValue()!=null)
{ Cell cell12=cells.getCell(n1,Colno);
System.out.println(cell12.getStyle().toString());
Style style = cell12.getStyle();
Font font = style.getFont();
font.setBold(true);
cell12.setStyle(style);
font.setColor(Color.BLACK);
font.setBold(true);
}
}
}
Attached Sample file for your reference.

Hi,


If you are using Aspose.Cells for Java version that is older than v7.0.0, please update your code to:

Workbook workbook = new Workbook();
FileInputStream fstream = new FileInputStream(“C:\abc.xls”);
workbook.open(fstream, FileFormatType.EXCEL97TO2003);
Worksheet worksheet = workbook.getWorksheets().getSheet(1);
Cells cells = worksheet.getCells();
int num=cells.getMaxDataRow();
int num1=cells.getMaxDataColumn();
System.out.println(cells.getMaxDataColumn());
System.out.println(cells.getMaxDataRow());
for(int n1=0;n1<num;n1++)
{ for(int Colno=0;Colno<=num1;Colno++)
{
if (cells.getCell(n1,Colno).getValue()!=null)
{ Cell cell12=cells.getCell(n1,Colno);
System.out.println(cell12.getStyle().toString());
Style style = cell12.getStyle();
Font font = style.getFont();
font.setBold(true);
font.setColor(Color.BLACK);
font.setBold(true);
style.setFont(font);
cell12.setStyle(style);

}
}
}


If you are using latest versions e.g >= v7.0.0, please change/update your code according as pasted below:

FileInputStream fstream = new FileInputStream(“C:\abc.xls”);
Workbook workbook = new Workbook(fstream);
Worksheet worksheet = workbook.getWorksheets().get(1);
Cells cells = worksheet.getCells();
int num=cells.getMaxDataRow();
int num1=cells.getMaxDataColumn();
System.out.println(cells.getMaxDataColumn());
System.out.println(cells.getMaxDataRow());
for(int n1=0;n1<num;n1++)
{ for(int Colno=0;Colno<=num1;Colno++)
{
if (cells.get(n1,Colno).getValue()!=null)
{ Cell cell12=cells.get(n1,Colno);
System.out.println(cell12.getStyle().toString());
Style style = cell12.getStyle();
Font font = style.getFont();
font.setBold(true);
font.setColor(Color.getBlack());
font.setBold(true);
cell12.setStyle(style);
}
}
}


workbook.save(“out.xls”);