Getting extra columns in excel spreadsheet not in query
I don't think this is an issue but I could not see where to mark this as a question.
I am getting 5 additional columns that are not in my query. RowError RowState Table ItemArray HasErrors
I can't seem to find where these are coming from. I am doing a basic query and export. So any assistance would be welcomed. Thanks
Yes, what data type are you piping to the Export-Excel? It grabs all the properties on the incoming object. For now unfortuantely, as a workaround, you have to exclude those properties and then pipe it to create the spreadsheet
I have this problem as well. The data comes from Invoke-SqlCmd and when piping to Export-Csv I don't get these extra properties that I get when I pipe to Export-Excel.
I get the same extra columns when exporting data using Export-Excel cmdlet. The data is also from an sql query but a custom function that uses the System.Data.SqlClient.SqlConnection to connect and execute a query, the data returned and being sent to Export-Excel is of type System.Data.DataRow. I'm using ImportExcel 4.0.8 and PowerShell 5.1 on Windows 7.
Before I used version 2.0.x and did not get the extra columns.
You can keep them from showing up by adding this at the end:
-ExcludeProperty ItemArray, RowError, RowState, Table, HasErrors
@dfinke could you exclude those additional columns as default? This is an unexpected behavior when the | Format-Table and | Export-Excel give different results.
Hi @santanachia how are you able to fix this?
Example on how to exclude default properties:
Invoke-DbaQuery -SqlInstance 'myssname' -Database mydbname -Query 'SELECT FirstName, LastName, Email FROM dbo.Employees' | Select * -ExcludeProperty RowError, RowState, Table, ItemArray, HasErrors | Export-Excel -Now
Define SQL Server details
$server = "" $database = "AdventureWorksDW2022" $query = "SELECT * FROM dbo.DimCustomer" $outputFile = "E:\study\preperation\SQLQuestion\xlsxthroughgui\usingPowershell\newCustomerdim.xlsx"
Execute SQL Query, remove extra columns, and export to Excel
Invoke-Sqlcmd -ServerInstance $server -Database $database -Query $query | Select * -ExcludeProperty ItemArray, RowError, RowState, Table, HasErrors | Export-Excel -Path $outputFile -AutoSize
Thanks @Subha1234a - correct!