Hi Team,
I am trying to set a html string for a cell.
Example text – "Hello world, <b>how are you ?</b><hl>http://google.co.in</hl>"
In this cutomized text exmaple, whatever text is enclosed within <hl> and </hl>
, we are trying to make that text as hyperlink for the remaining text.
And whatever enclosed within <b>and </b>
needs to appear as bold.
And we need to apply “Avenir Next LT Pro” font name for the entire text and color should be red.
So we trying to first apply all the required styles to a cell and then setting the htmlString.
On doing the cell.SetHtmlString, we are able to get the hyperlink, bold text and font colour. But font name is always showing as “Arial” instead of “Avenir Next LT Pro” in the downloaded excel file.
Sample code which I used -
final String HYPERLINK_START_DELIMETER = "<hl>";
final String HYPERLINK_END_DELIMETER = "</hl>";
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Adding a new worksheet to the Workbook object
workbook.getWorksheets().add();
// Obtaining the reference of the newly added worksheet by passing its sheet index
Worksheet worksheet = workbook.getWorksheets().get(0);
String content = "Hello world, <b>how are you ?</b><hl>http://google.co.in</hl>";
Cell cell = worksheet.getCells().get("A5");
applyTextStyle(cell);
cell.setHtmlString(content.substring(0, content.indexOf(HYPERLINK_START_DELIMETER)));
worksheet.getHyperlinks().add(CellsHelper.cellIndexToName(4, 0), 1, 1,
content.substring(
content.indexOf(HYPERLINK_START_DELIMETER) + HYPERLINK_START_DELIMETER.length(),
content.indexOf(HYPERLINK_END_DELIMETER)));
// Saving the Excel file
workbook.save("D:\\test.xlsx");
}
public static void applyTextStyle(Cell cell) {
Style style = cell.getStyle();
style.getFont().setBold(false);
style.getFont().setUnderline(FontUnderlineType.DASH);
style.getFont().setName("Avenir Next LT Pro");
final Color color = Color.decode("#FF0000");
style.getFont().setColor(
com.aspose.cells.Color.fromArgb(255, color.getRed(), color.getGreen(), color.getBlue()));
style.getFont().setDoubleSize(10);
style.setHorizontalAlignment(TextAlignmentType.CENTER);
cell.setStyle(style);
}
And the downloaded file is test.zip (7.5 KB)
Could you please let us know why the applied font name is not coming properly in downloaded excel ? is it a bug in aspose or should I change something in my code. ?