From fa64299760b66dd3a05b3bbc4588a09ac5696cdc Mon Sep 17 00:00:00 2001 From: Aaron Nelson Date: Thu, 4 Oct 2018 14:29:33 -0400 Subject: [PATCH 1/2] Added Example for SQL Server Added an example for running a query from a SQL Server database using the Invoke-Sqlcmd cmdlet, and then exporting the results to Excel. --- Export-Excel.ps1 | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Export-Excel.ps1 b/Export-Excel.ps1 index cea61c2..ca86ed2 100644 --- a/Export-Excel.ps1 +++ b/Export-Excel.ps1 @@ -381,6 +381,16 @@ 0..360 | ForEach-Object {[pscustomobject][ordered]@{X=$_; Sinx="=Sin(Radians(x)) "} } | Export-Excel -now -LineChart -AutoNameRange Creates a line chart showing the value of Sine(x) for values of X between 0 and 360 degrees. + + .EXAMPLE + > + PS> Invoke-Sqlcmd -ServerInstance localhost\DEFAULT -Database AdventureWorks2014 -Query "select * from sys.tables" -OutputAs DataRows | + Export-Excel -Path .\SysTables_AdventureWorks2014.xlsx -WorksheetName Tables + + Runs a query against a SQL Server database and outputs the resulting rows DataRows using the -OutputAs parameter. + The results are then piped to the Export-Excel function. + NOTE: You need to install the SqlServer module from the PowerShell Gallery in oder to get the -OutputAs parameter for the Invoke-Sqlcmd cmdlet. + .LINK https://github.com/dfinke/ImportExcel #> From 3c18af50b96f46b1a58aa4aaa1d8dd9e8ec42266 Mon Sep 17 00:00:00 2001 From: Aaron Nelson Date: Thu, 4 Oct 2018 15:36:42 -0400 Subject: [PATCH 2/2] Added Example for SQL Server to Import-Excel Added an example for INSERTing rows into a SQL Server table from the Import-Excel function. --- ImportExcel.psm1 | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/ImportExcel.psm1 b/ImportExcel.psm1 index 107b8ec..bcb440c 100644 --- a/ImportExcel.psm1 +++ b/ImportExcel.psm1 @@ -221,7 +221,7 @@ function Import-Excel { Notice that empty rows and empty columns are not imported. -.EXAMPLE + .EXAMPLE Import data from an Excel worksheet. One object is created for each row. The property names are provided with the ‘-HeaderName’ parameter. The import will start from row 2 and empty columns and rows are not imported. ---------------------------------------------------------- @@ -241,6 +241,16 @@ function Import-Excel { Notice that only 1 object is imported with only 3 properties. Column B and row 2 are empty and have been disregarded by using the switch '-DataOnly'. The property names have been named with the values provided with the parameter '-HeaderName'. Row number 1 with ‘Chuck Norris’ has not been imported, because we started the import from row 2 with the parameter ‘-StartRow 2’. + .EXAMPLE + > + PS> ,(Import-Excel -Path .\SysTables_AdventureWorks2014.xlsx) | + Write-SqlTableData -ServerInstance localhost\DEFAULT -Database BlankDB -SchemaName dbo -TableName MyNewTable_fromExcel -Force + + Imports data from an Excel file and pipe the data to the Write-SqlTableData to be INSERTed into a table in a SQL Server database. + The ",( ... )" around the Import-Excel command allows all rows to be imported from the Excel file, prior to pipelining to the Write-SqlTableData cmdlet. This helps prevent a RBAR scenario and is important when trying to import thousands of rows. + The -Force parameter will be ignored if the table already exists. However, if a table is not found that matches the values provided by -SchemaName and -TableName parameters, it will create a new table in SQL Server database. The Write-SqlTableData cmdlet will inherit the column names & datatypes for the new table from the object being piped in. + NOTE: You need to install the SqlServer module from the PowerShell Gallery in oder to get the Write-SqlTableData cmdlet. + .LINK https://github.com/dfinke/ImportExcel