Search code examples
c#excelnpoi

Cell.SetCellFormula() Throw Exception System.ArgumentNullException


class Program
{
    static void Main(string[] args)
    {
        EditExcel();
    }

    static void EditExcel()
    {
        string path = @"test.xlsx";
        XSSFWorkbook workbook = new XSSFWorkbook(path);
        var sheet = workbook.GetSheetAt(0);

        var row = sheet.GetRow(4);
        var cell = row.GetCell(3);
        cell.SetCellType(CellType.Formula);
        cell.SetCellFormula("SUM(E4:F4)");
        FileStream fs = new FileStream(@"newTest.xlsx", FileMode.Create, FileAccess.Write);
        workbook.Write(fs);
    }
}

Above is the test code that I write. I tested it in windows10, .net core3.1 Visual Studio 2019. NPOI version is 2.5.2 But I got the fllowing error: enter image description here So How can I slove this problem? Thanks


Solution

  • It's a bug in NPOI 2.5.2. I was facing the same issue in my app and only workaround for now is to downgrade to 2.5.1. It should be fixed in the next release 2.5.3.