Update specific fonts to the required fonts in the workbook fonts

Hi Team,

My requirement is to update certain fonts that are already there in the workbook to my required font.

For eg., if I’ve the below fonts in my workbook

Aspose.Cells.Font [ Arial; 10.0; Regular; com.aspose.cells.Color@ff000000 ]
Aspose.Cells.Font [ Arial; 1.0; Regular; com.aspose.cells.Color@ffffffff ]

I would like to modify the font-family to Calibri

First I read the existing fonts in the workbook :

Font[] fontList = rebrandingSample.getFonts();

And updated the font to “Calibri” if I’ve “Arial” :

for (Font fontNew : fontList)
{
  if (fontNew.getName().equals("Arial"))
  {
    fontNew.setName("Calibri");
  }

}

This is changing the fonts in the fontList to “Calibri” as required.

But how to apply the modified fontList to the entire workbook before saving the workbook ?

Can you please help with sample code ?

Note : Not sharing any excel file sample as this setting can be done and checked in any sample excel file.

Regards,
Sanjeev

@sanjeevkumarambti,

You may try using DefaultStyle attribute to get/set your desired font for the whole workbook. See the sample code for your reference:
e.g.
Sample code:

    Workbook workbook = new Workbook("e:\\test2\\Book1.xlsx");

            Style style = workbook.getDefaultStyle();
            style.getFont().setName("Calibri");
            workbook.setDefaultStyle(style); 
          ..........

Hope, this helps a bit.

To update all fonts used by all cells in the workbook, another way is to update styles in global pool directly. Code example:

        for(int i=workbook.getCountOfStylesInPool()-1;i>-1;i--)
        {
            Style style = workbook.getStyleInPool(i);
            if (style.getFont().getName().equals("Arial"))
            {
                style.getFont().setName("Calibri");
            }
        }

Hi Johnson,

We dont have a specific method to update the Arial styles to WorkBook.

Only copy(style) method is available. This is adding the updated styles to the existing styles/

UpdateWorkBookFontFamilyArialToCalibri.zip (612 Bytes)

I need to see the Arial fonts updated to Calibri after applying the logic to modify the styles.

But in the code sample I attached, for the existing workbook fonts the newly updated fonts (meaning Arial updated to Calibri) are appended.

Is there a way to just update the existing workbook fonts family Arial to Calibri ?

Thanks.
Sanjeev

@sanjeevkumarambti,

Could you please zip the input and output Excel files and attach it here. Also, could you skip the line if it works for your needs:
e.g.
Sample code:

testWorkBook.getStyleInPool(i).copy(style);