Skip to content

Commit

Permalink
Merge pull request #412 from igitur/issue410-load-table-without-name
Browse files Browse the repository at this point in the history
Fall back to display name if table name is missing.
  • Loading branch information
igitur committed Aug 23, 2017
2 parents 46b92dc + f442fba commit dbc8307
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 4 deletions.
10 changes: 7 additions & 3 deletions ClosedXML/Excel/XLWorkbook_Load.cs
Original file line number Diff line number Diff line change
Expand Up @@ -265,11 +265,15 @@ private void LoadSpreadsheetDocument(SpreadsheetDocument dSpreadsheet)

#region LoadTables

foreach (TableDefinitionPart tablePart in wsPart.TableDefinitionParts)
foreach (var tablePart in wsPart.TableDefinitionParts)
{
var dTable = tablePart.Table;
string reference = dTable.Reference.Value;
XLTable xlTable = ws.Range(reference).CreateTable(dTable.Name, false) as XLTable;
String reference = dTable.Reference.Value;
String tableName = dTable?.Name ?? dTable.DisplayName ?? string.Empty;
if (String.IsNullOrWhiteSpace(tableName))
throw new InvalidDataException("The table name is missing.");

XLTable xlTable = ws.Range(reference).CreateTable(tableName, false) as XLTable;
if (dTable.HeaderRowCount != null && dTable.HeaderRowCount == 0)
{
xlTable._showHeaderRow = false;
Expand Down
1 change: 1 addition & 0 deletions ClosedXML_Tests/ClosedXML_Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@
<EmbeddedResource Include="Resource\Misc\EmptyCellValue.xlsx" />
<EmbeddedResource Include="Resource\Misc\AllShapes.xlsx" />
<EmbeddedResource Include="Resource\Misc\TableHeadersWithLineBreaks.xlsx" />
<EmbeddedResource Include="Resource\Misc\TableWithNameNull.xlsx" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Resource\Images\ImageHandling.png" />
Expand Down
3 changes: 2 additions & 1 deletion ClosedXML_Tests/Excel/Loading/LoadingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ public void CanSuccessfullyLoadFiles()
@"Misc\ExcelProducedWorkbookWithImages.xlsx",
@"Misc\EmptyCellValue.xlsx",
@"Misc\AllShapes.xlsx",
@"Misc\TableHeadersWithLineBreaks.xlsx"
@"Misc\TableHeadersWithLineBreaks.xlsx",
@"Misc\TableWithNameNull.xlsx"
};

foreach (var file in files)
Expand Down
Binary file not shown.

0 comments on commit dbc8307

Please sign in to comment.