nissl-lab / npoi

a .NET library that can read/write Office formats without Microsoft Office installed. No COM+, no interop.
Apache License 2.0
5.67k stars 1.42k forks source link

AVERAGEIF formula doesn't work in xls #1327

Closed mferraricloudsurfers closed 4 months ago

mferraricloudsurfers commented 4 months ago

NPOI Version

2.7.0

File Type

NPOI File

5f88c7e2-e3d7-4594-a8a6-f8d17516f177.xls

Reproduce Steps

Issue Description

var fs = new FileStream(file, FileMode.Create, FileAccess.Write)
IWorkbook workbook = new HSSFWorkbook();
IRow row = excelSheet.CreateRow(0);
row.CreateCell(1).SetCellFormula($"SUM(B5:B10)");
row.CreateCell(2).SetCellFormula($"AVERAGEIF(B5:B10,\"<> 0\")");
workbook.Write(fs);
fs.Close();

Opening the result xls file with Microsoft® Excel® per Microsoft 365 MSO (Version 2403 Build 16.0.17425.20176) 64 bit (Italian), the SUM formula is correctly translated and works. Instead, AVERAGEIF is not translated and the cell is marked in Excel how error with #NAME not recognized.

image

Bykiev commented 4 months ago

Can you please attach the Excel file produced by NPOI? In Excel file there shouldn't be any localized data

mferraricloudsurfers commented 4 months ago

Opening the file in protected view mode, will display correctly the result

image

While, if I open the file in edit mode and I replace the "AVERAGEIF" with the corresponding localized command version, it works.

image

Bykiev commented 4 months ago

can you please test with xlsx?

mferraricloudsurfers commented 4 months ago

can you please test with xlsx?

it works with xlsx

Bykiev commented 4 months ago

Seems to be a bug, I'll try to research it later

Bykiev commented 4 months ago

I did a research and AVERAGEIF formula is not supported by excel 97-2003, when you saving the workbook with MS Excel it warns that this function is not supported by versions prior 2007. When opening such file with MS Excel 2007+ it is opened in compatibility mode. I believe it's not supported by POI and the workbook always saved in EXCEL97 format for xls and it won't be fixed in NPOI.

@tonyqus, what do you think?

tonyqus commented 4 months ago

Since AverageIf function is not supported in Old Excel 97-2003, this bug is invalid.