ImportExcel icon indicating copy to clipboard operation
ImportExcel copied to clipboard

Add-WorkSheet Not Working

Open blhf opened this issue 7 years ago • 4 comments

Here is my code:

$sql = @' SELECT s.BusinessEntityID ,p.FirstName ,p.PersonType ,s.Bonus

FROM [AdventureWorks2017].[Sales].[SalesPerson] s INNER JOIN [AdventureWorks2017].[Person].[Person] p ON p.BusinessEntityID = s.BusinessEntityID '@

$FilePath = "D:/test/test.xlsx"

$x1 = Invoke-Sqlcmd -ServerInstance localhost -Database "AdventureWorks2017" -Query $sql | Export-Excel $FilePath -AutoSize -ExcludeProperty HasErrors, RowError, RowState, Table, ItemArray $excel = Open-ExcelPackage $FilePath $ws = Add-WorkSheet -WorksheetName "Summary" -ExcelPackage $excel

Can you help check what is lacking in my code? I try to run each one of the functions but it is not working.

blhf avatar Jul 31 '19 19:07 blhf

Did you close the package after adding the worksheet ?

jhoneill avatar Jul 31 '19 23:07 jhoneill

Hi @jhoneill. I am new into this module. I have a working export-excel from a query the challenge is I need to create another sheet where I need to put the result of my second query. Can you guide me on how to properly do it?

blhf avatar Aug 01 '19 06:08 blhf

Ah , OK, easier than you might think

 Invoke-Sqlcmd <<SQL1>>  | Export-Excel $FilePath -worksheetName "Query1"  <<other options>>
 Invoke-Sqlcmd <<SQL2>>  | Export-Excel $FilePath -worksheetName "Query2"  <<other options>>

jhoneill avatar Aug 01 '19 15:08 jhoneill

@jhoneill its easy now. Thanks for this. I thought there are bunch of code I need to run.

blhf avatar Aug 01 '19 15:08 blhf