diff --git a/ClosedXML/Excel/Cells/XLCell.cs b/ClosedXML/Excel/Cells/XLCell.cs index 09b87ab..90e0637 100644 --- a/ClosedXML/Excel/Cells/XLCell.cs +++ b/ClosedXML/Excel/Cells/XLCell.cs @@ -691,7 +691,8 @@ public IXLTable InsertTable(DataTable data, string tableName, bool createTable) { - if (data == null) return null; + if (data == null || data.Columns.Count == 0) + return null; if (createTable && this.Worksheet.Tables.Any(t => t.Contains(this))) throw new InvalidOperationException(String.Format("This cell '{0}' is already part of a table.", this.Address.ToString())); diff --git a/ClosedXML_Tests/Excel/Tables/TablesTests.cs b/ClosedXML_Tests/Excel/Tables/TablesTests.cs index adbe14b..25eeb50 100644 --- a/ClosedXML_Tests/Excel/Tables/TablesTests.cs +++ b/ClosedXML_Tests/Excel/Tables/TablesTests.cs @@ -52,6 +52,20 @@ } [Test] + public void PreventAddingOfEmptyDataTable() + { + using (var wb = new XLWorkbook()) + { + var ws = wb.AddWorksheet("Sheet1"); + + var dt = new DataTable(); + var table = ws.FirstCell().InsertTable(dt); + + Assert.AreEqual(null, table); + } + } + + [Test] public void CanSaveTableCreatedFromSingleRow() { using (var wb = new XLWorkbook())