Hi Pouya,
Thanks for your posting and using Aspose.Cells.
Please download and use the latest version: Aspose.Cells for .NET (Latest Version) and see if it makes any difference.
If your issue persist, then please provide us a runnable sample code replicating this issue with the latest version. It will help us look into your issue precisely and we will be able to update you asap.
Attached you will find a solution that contains sample code that replicates this issue.
Hi Pouya,
Thanks for your sample project and using Aspose.Cells.
Actually, worksheet.Cells.MaxRow gives the index of maximum row, this index is 0-based. It means, if your maximum row is 10, then it will give you 9. Similarly, if your maximum row is 11, then MaxRow will give you 10.
So, in order to get actual maximum row, you will have to add 1 in MaxRow.
It means, you will have to change your following code. Changes are highlighted in red.
worksheet.Cells[“B2”].Formula = string.Format("=COUNTA(B$4:B${0})", worksheet.Cells.MaxRow);
worksheet.Cells[“O2”].Formula = string.Format("=COUNTA(O$4:O${0})", worksheet.Cells.MaxRow);
worksheet.Cells[“P2”].Formula = string.Format("=COUNTA(P$4:P${0})", worksheet.Cells.MaxRow);
worksheet.Cells[“Q2”].Formula = string.Format("=COUNTA(Q$4:Q${0})", worksheet.Cells.MaxRow);
into this
worksheet.Cells[“B2”].Formula = string.Format("=COUNTA(B$4:B${0})", worksheet.Cells.MaxRow + 1);
worksheet.Cells[“O2”].Formula = string.Format("=COUNTA(O$4:O${0})", worksheet.Cells.MaxRow + 1);
worksheet.Cells[“P2”].Formula = string.Format("=COUNTA(P$4:P${0})", worksheet.Cells.MaxRow + 1);
worksheet.Cells[“Q2”].Formula = string.Format("=COUNTA(Q$4:Q${0})", worksheet.Cells.MaxRow + 1);
I have attached the output xlsx file generated after the above code change for your reference. As you can see, it has correct formulas and their values are also correct.
Yes, that fixes the count problem.
Hi Pouya,
Thanks for your feedback and using Aspose.Cells.
It is good to know that your first issue is resolved. For second issue, please explain the issue in more detail.
Do you see any problem with the attached file as it is generated by your code?
Attachment: WorkbookWithIssue.xlsx
It looks fine to me and I do not see any issue with the COUNTA formula.
I have attached another solution with the problem.
Hi Pouya,
Thanks for your sample project and using Aspose.Cells.
Actually, you are inserting Empty string in those cells, therefore the cells are not being considered empty or “no data” cells.
You should use null instead of empty string “”. It will fix your issue.
So, please change your following code.
dataRow[“Parent Org”] = “Advantage Health Solutions”;
dataRow[“Contract”] = “H5508”;
dataRow[“Opt Supp Problem Summary”] = “”;
dataRow[“Margin Problem”] = “”;
dataRow[“Retention Problem”] = “”;
_dataTable.Rows.Add(dataRow);
into this
dataRow[“Parent Org”] = “Advantage Health Solutions”;
dataRow[“Contract”] = “H5508”;
dataRow[“Opt Supp Problem Summary”] = null;
dataRow[“Margin Problem”] = null;
dataRow[“Retention Problem”] = null;
_dataTable.Rows.Add(dataRow);
Now, Aspose.Cells will insert no data in these cells. And COUNTA formula will return correct result.
Ugh…