ImportExcel icon indicating copy to clipboard operation
ImportExcel copied to clipboard

Getting extra columns in excel spreadsheet not in query

Open SQLAJ opened this issue 10 years ago • 9 comments

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

SQLAJ avatar Sep 23 '16 18:09 SQLAJ

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

dfinke avatar Sep 23 '16 19:09 dfinke

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.

ojamte avatar Jan 17 '17 19:01 ojamte

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.

ViperTG avatar Dec 07 '17 11:12 ViperTG

You can keep them from showing up by adding this at the end: -ExcludeProperty ItemArray, RowError, RowState, Table, HasErrors

jaquinones avatar Jan 24 '18 13:01 jaquinones

@dfinke could you exclude those additional columns as default? This is an unexpected behavior when the | Format-Table and | Export-Excel give different results.

Santanachia avatar Apr 17 '19 07:04 Santanachia

Hi @santanachia how are you able to fix this?

blhf avatar Jul 23 '19 10:07 blhf

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

ali4728 avatar Dec 01 '19 19:12 ali4728

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

Subha1234a avatar Mar 10 '25 12:03 Subha1234a

Thanks @Subha1234a - correct!

dfinke avatar Mar 10 '25 19:03 dfinke