dotnetcore / NPOI

A .NET library for reading and writing Microsoft Office binary and OOXML file formats.
Apache License 2.0
1.92k stars 413 forks source link

Cell Formula #18

Closed Taha-Di-Nero closed 7 years ago

Taha-Di-Nero commented 7 years ago

Hi, When I set the cell Formula, an exception arise: "System.ArgumentNullException: 'Value cannot be null.'"

formula = "SUM(" + range.FormatAsString() + ")"; cell.SetCellType(CellType.Formula); cell.CellFormula = formula;

StackTrace "
at System.IO.StreamReader..ctor(Stream stream, Encoding encoding, Boolean detectEncodingFromByteOrderMarks, Int32 bufferSize, Boolean leaveOpen)\r\n
at NPOI.SS.Formula.Function.FunctionMetadataReader.CreateRegistry()\r\n
at NPOI.SS.Formula.Function.FunctionMetadataRegistry.GetInstance()\r\n
at NPOI.SS.Formula.Function.FunctionMetadataRegistry.LookupIndexByName(String name)\r\n
at NPOI.SS.Formula.PTG.AbstractFunctionPtg.IsBuiltInFunctionName(String name)\r\n
at NPOI.SS.Formula.FormulaParser.Function(String name)\r\n
at NPOI.SS.Formula.FormulaParser.ParseRangeExpression()\r\n
at NPOI.SS.Formula.FormulaParser.PercentFactor()\r\n
at NPOI.SS.Formula.FormulaParser.PowerFactor()\r\n
at NPOI.SS.Formula.FormulaParser.Term()\r\n
at NPOI.SS.Formula.FormulaParser.AdditiveExpression()\r\n
at NPOI.SS.Formula.FormulaParser.ConcatExpression()\r\n
at NPOI.SS.Formula.FormulaParser.ComparisonExpression()\r\n
at NPOI.SS.Formula.FormulaParser.UnionExpression()\r\n
at NPOI.SS.Formula.FormulaParser.Parse()\r\n
at NPOI.SS.Formula.FormulaParser.Parse(String formula, IFormulaParsingWorkbook workbook, FormulaType formulaType, Int32 sheetIndex)\r\n
at NPOI.XSSF.UserModel.XSSFCell.SetFormula(String formula, FormulaType formulaType)\r\n

Any help??

Taha-Di-Nero commented 7 years ago

Hi, Upgrading to Savorboard.NPOI.Core.Ooxml 1.1.4 (was using DotNetCore.NPOI 1.0.0), the problem change to the line when formula are evaluated

WorkbookFactory.CreateFormulaEvaluator(wb).EvaluateAll(); At this line an exception is thrown:

$exception {System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary. at System.ThrowHelper.ThrowKeyNotFoundException() at System.Collections.Generic.Dictionary`2.get_Item(TKey key) at Npoi.Core.SS.Formula.WorkbookEvaluator.GetSheetIndex(IEvaluationSheet sheet) at Npoi.Core.SS.Formula.WorkbookEvaluator.Evaluate(IEvaluationCell srcCell) at Npoi.Core.XSSF.UserModel.XSSFFormulaEvaluator.EvaluateFormulaCellValue(ICell cell) at Npoi.Core.XSSF.UserModel.XSSFFormulaEvaluator.EvaluateFormulaCell(ICell cell) at Npoi.Core.HSSF.UserModel.HSSFFormulaEvaluator.EvaluateAllFormulaCells(IWorkbook wb, IFormulaEvaluator evaluator)

Removing that line everything work fine and somehow when I open the generated xlsx file formula are already evaluated. Nb: I had ported an export using java apache poi code to .net core and in java was a must eveluting formula before closing WorkBook

Thanks for saving me a lot of time.