diff --git a/ExportedCommands/Export-Excel.ps1 b/ExportedCommands/Export-Excel.ps1 index 0c5cfa3..0ab635f 100644 --- a/ExportedCommands/Export-Excel.ps1 +++ b/ExportedCommands/Export-Excel.ps1 @@ -153,8 +153,10 @@ } #if we did not get a table name but there is a table which covers the active part of the sheet, set table name to that, and don't do anything with autofilter - if ($null -eq $TableName -and $ws.Tables.Where({$_.address.address -eq $ws.dimension.address})) { - $TableName = $ws.Tables.Where({$_.address.address -eq $ws.dimension.address},'First', 1).Name + $existingTable = $ws.Tables.Where({$_.address.address -eq $ws.dimension.address},'First', 1) + if ($null -eq $TableName -and $existingTable) { + $TableName = $existingTable.Name + $TableStyle = $existingTable.Tablestyle $AutoFilter = $false } #if we did not get $autofilter but a filter range is set and it covers the right area, set autofilter to true @@ -197,28 +199,41 @@ else { $setNumformat = ($Numberformat -ne $ws.Cells.Style.Numberformat.Format) } } catch {throw "Failed preparing to export to worksheet '$WorksheetName' to '$Path': $_"} - #region Special case -inputobject passed a dataTable object + #region Special case -inputobject passed a dataTable object <# If inputObject was passed via the pipeline it won't be visible until the process block, we will only see it here if it was passed as a parameter if it is a data table don't do foreach on it (slow) - put the whole table in and set dates on date columns, set things up for the end block, and skip the process block #> if ($InputObject -is [System.Data.DataTable]) { - #Change TableName if $TableName is non-empty; don't leave caller with a renamed table! - $orginalTableName = $InputObject.TableName - if ($TableName) { - $InputObject.TableName = $TableName + if ($Append) { + $row ++ + $null = $ws.Cells[$row,$StartColumn].LoadFromDataTable($InputObject, $false ) + if ($TableName) { + Add-ExcelTable -Range $ws.Cells[$ws.Dimension] -TableName $TableName -TableStyle $TableStyle + } } - while ($InputObject.TableName -in $pkg.Workbook.Worksheets.Tables.name) { - Write-Warning "Table name $($InputObject.TableName) is not unique, adding '_' to it " - $InputObject.TableName += "_" + else { + #Change TableName if $TableName is non-empty; don't leave caller with a renamed table! + $orginalTableName = $InputObject.TableName + if ($PSBoundParameters.ContainsKey("TableName")) { + $InputObject.TableName = $TableName + } + while ($InputObject.TableName -in $pkg.Workbook.Worksheets.Tables.name) { + Write-Warning "Table name $($InputObject.TableName) is not unique, adding '_' to it " + $InputObject.TableName += "_" + } + #Insert as a table, if Tablestyle didn't arrive as a default, or $TableName non-null - even if empty + if ($null -ne $TableName -or $PSBoundParameters.ContainsKey("TableStyle")) { + $null = $ws.Cells[$row,$StartColumn].LoadFromDataTable($InputObject, (-not $noHeader),$TableStyle ) + # Workaround for EPPlus not marking the empty row on an empty table as dummy row. + if ($InputObject.Rows.Count -eq 0) { + ($ws.Tables | Select-Object -Last 1).TableXml.table.SetAttribute('insertRow', 1) + } + } + else { + $null = $ws.Cells[$row,$StartColumn].LoadFromDataTable($InputObject, (-not $noHeader) ) + } + $InputObject.TableName = $orginalTableName } - #Insert as a table, if Tablestyle didn't arrive as a default, or $TableName non-null - even if empty - if ($null -ne $TableName -or $PSBoundParameters.ContainsKey("TableStyle")) { - $null = $ws.Cells[$row,$StartColumn].LoadFromDataTable($InputObject, (-not $noHeader),$TableStyle ) - } - else { - $null = $ws.Cells[$row,$StartColumn].LoadFromDataTable($InputObject, (-not $noHeader) ) - } - $InputObject.TableName = $orginalTableName foreach ($c in $InputObject.Columns.where({$_.datatype -eq [datetime]})) { Set-ExcelColumn -Worksheet $ws -Column ($c.Ordinal + $StartColumn) -NumberFormat 'Date-Time' }