npoi icon indicating copy to clipboard operation
npoi copied to clipboard

XSSFRichTextString.ApplyFont() works incorrectly

Open SergiyStoyan opened this issue 1 year ago • 0 comments

NPOI Version

2.6.2

File Type

  • [x] XLSX

Upload the Excel File

It happens for any xlsx file.

Reproduce Steps

Run the following code:

 using (var xls = new XSSFWorkbook())
{
    ISheet sheet = xls.CreateSheet("test");

    var f1 = xls.CreateFont();
    f1.FontName = "Arial";
    f1.FontHeight = 18 * 20;
    f1.IsBold = false;
    f1.Color = IndexedColors.Red.Index;

    var f2 = xls.CreateFont();
    f2.FontName = "Arial";
    f2.FontHeight = 18 * 20;
    f2.IsBold = true;
    f2.Color = IndexedColors.Blue.Index;

    XSSFRichTextString s = new XSSFRichTextString("0123");
    s.ApplyFont(0, 2, f1);
    s.ApplyFont(2, 4, f2);

    var c = sheet.CreateRow(1).CreateCell(1);
    c.SetCellValue(s);

    using (var fileData = new FileStream(@"c:\test3.xlsx", FileMode.Create))
    {
        xls.Write(fileData, true);
    }
}

Issue Description

As follows from the code the cell string must be of the same font size and the substring [0-1] must be red. It is not the case. The actual cell string appears: NPOI-bug1

It seems that the first font is not set at all and the default one is used. BTW to work this around I had to use Append() which works as expected. But as my code works for both XSSF and HSSF, it would be nice to make XSSFRichTextString satisfy its interface requirements.

SergiyStoyan avatar Dec 29 '23 18:12 SergiyStoyan