NPOI create cell containing bold and non bold text

Nathan picture Nathan · May 9, 2011 · Viewed 32.8k times · Source

I'm using NPOI to output excel from Asp.Net MVC app and works very well with plain text but have now been requested to add formatting and am having problems where I need to have a single cell with bold text followed by non-bold text. e.g.

This text bold - this text normal

I know I can give a cell a single style but this won't help and I cannot see anyway to give a cell some pre-formatted rich text.

The only possible solution that I can think of is creating two cells separately and the merge them together but will that then mean the formatting will be lost?

Is there a way to do this that I have missed in NPOI?

Answer

Ernie Banzon picture Ernie Banzon · Aug 25, 2011

You may try this:

        var font = reportWorkbook.CreateFont();
        font.FontHeightInPoints = 11;
        font.FontName = "Calibri";
        font.Boldweight = (short)NPOI.SS.UserModel.FontBoldWeight.BOLD; 

        var cell = headerRow.CreateCell(0);
        cell.SetCellValue("Test Bold");
        cell.CellStyle = reportWorkbook.CreateCellStyle();
        cell.CellStyle.SetFont(font);