TotalsRowFunction issue
See original GitHub issue- Bug
Version of ClosedXML
Latest from Nuget: 0.94.2
What is the current behavior?
It seems like TotalsRowFunction fails to create a valid cell reference when these two conditions are met:
- The table column name has spaces (e.g., “AB CD”)
- The name of the sheet where the table exists also has spaces (e.g., “Two words”)
When you open the generated file in Excel you are prompted to accept the repair actions. The XML report includes a message like “Repaired Records: Table from /xl/tables/table2.xml part (Table)”
The resulting formula in the Totals cell is “=SUBTOTAL(109,Two #REF!)”
What is the expected behavior or new feature?
The formula in the Totals cell should be: “=SUBTOTAL(109,[AB CD])”
Did this work in previous versions of our tool? Which versions?
As far as I know it was working fine with version 0.92.
Reproducibility
This issue seems to be related to case #773 (Bug with TotalsRowFunction). I adapted the code provided by healkar01 to reproduce the problem. Look at the “Two words2” sheet in the generated XLS file to see the issue in the Grand Total cell (B11)
Code to reproduce problem:
void Main()
{
XLWorkbook wb = new XLWorkbook();
InsertDataTable(wb, "OneWord", GenerateData(useMultiWordColumn: false));
InsertDataTable(wb, "OneWord2", GenerateData(useMultiWordColumn: true));
InsertDataTable(wb, "Two words", GenerateData(useMultiWordColumn: false));
// the line below reproduces the issue
InsertDataTable(wb, "Two words2", GenerateData(useMultiWordColumn: true));
wb.SaveAs($@"C:\{DateTime.Now.ToString("yyyyMMdd-HHmmss")}-ClosedXml-Issue.xlsx");
}
static DataTable GenerateData(bool useMultiWordColumn)
{
var columnName = useMultiWordColumn ? "AB CD" : "ABCD";
DataTable dt = new DataTable();
dt.Clear();
dt.Columns.Add("Service", typeof(String));
dt.Columns.Add(columnName, typeof(Int32));
for (int i = 1; i < 10; i++)
{
DataRow dtRow = dt.NewRow();
dtRow["Service"] = "Service " + i;
dtRow[columnName] = 500;
dt.Rows.Add(dtRow);
}
return dt;
}
static void InsertDataTable(XLWorkbook wb, string sheetName, DataTable dt)
{
var ws = wb.Worksheets.Add(sheetName);
// Insert our datatable into the data sheet at cell 1,1
var table = ws.Cell(1, 1).InsertTable(dt, sheetName, true);
ws.Columns().AdjustToContents();
table.ShowTotalsRow = true;
foreach (var field in table.Fields)
{
if (field.Index > 0 && field.Name != "")
{
field.TotalsRowFunction = XLTotalsRowFunction.Sum;
}
}
table.Field(0).TotalsRowLabel = "Grand Total";
}
Thanks! This is a great tool, thank you for providing it. Let me know if you need more information.
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (2 by maintainers)
Top GitHub Comments
@tegrit-joe-trupiano The bug is that ClosedXML isn’t generating the second table’s name correctly. So both tables end up with
Table1
as their names. You can easily workaround this by manually specifying the table names in theInsertTable()
overload. It’s arguably better practice always to do that anyway. I’ll prepare a fix for this though.Hi igitur, thanks for the reply I was able to reproduce this bug and have the code sample attached with just what is need to reproduce. The bug seems to require a second tab as with one tab I can not reproduce the bug.
Description - Class of items for displaying in the excel sheet
Description - Main in console app. Outputs the excel to bin. The issue is in the renaming of the field in the second tab. Issue does not happen when just one tab is present and that tab contains the same renaming.
I can open a new bug if needed. Sorry if I came off as pushy or impatient above. New to github so just learning.
Thanks
Edit - Added more detail