This project has moved and is read-only. For the latest updates, please go here.

Error With DataTable

Aug 18, 2012 at 9:26 PM
Edited Aug 18, 2012 at 9:28 PM

Hello!  I am a happy, happy new user of ClosedXML.  All I'm doing is passing datatables, and seeing the Excel results.  No special formatting, no special handling, nada.

Everything works perfectly on the nineteen or so tables my app passes, but I'm having difficulty with just one and I cannot see what's different.  I am executing "internal static memorystream WriteExcelOpenXML(DataTable ThisTable)".  On the statement "workbook.worksheets.Add(ThisTable);" I puke with the error message "Worksheet names cannot be empty".

I saw this before, when I passed a datatable with no rows, but that's not the case here.  The only thing different about this table is that it comes from passing my table (derived via recordset) into a PivotTable routine that returns a new table.  But still, the new table has simple rows and columns.

I looked at some of the columns that were defined as Int32 and saw cell value (ToString) of String.Empty.  Thought maybe that was it so I revised those values to DBNull.  NO HELP.  Then I changed them to zero.  NO HELP.

Can anybody please suggest what I need to do, or to check with this table to get past this error?  Production implementation deadline is looming!!

Thanks in advance. 

Aug 18, 2012 at 10:00 PM

When you add a worksheet with a data table it uses the table's name by default. You have to set the table.TableName. I'll add an overload so you can do workbook.AddWorksheet(dataTable, tableName) too.

Aug 18, 2012 at 11:37 PM

Thank you, thank you, THANK YOU!!!

I haven't had time to pull down your latest version with the overload, but it was a simple matter to locate the one single spot in the code where I execute all OpenXMLProcesses.WriteExcel(MyTable) and, just before it, give the table a name so it has one for sure.

Works Perfectly!  Thanks again.