From 26f6df71680dfdce51c0f905dd46c9561419a793 Mon Sep 17 00:00:00 2001 From: dfinke Date: Wed, 14 Jun 2017 22:44:11 -0400 Subject: [PATCH 1/2] Added Export-ExcelAsSQLInsert --- Export-ExcelAsSQLInsert.ps1 | 38 +++++++++++++++++++++++++++++++++++++ ImportExcel.psd1 | 2 +- ImportExcel.psm1 | 1 + 3 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 Export-ExcelAsSQLInsert.ps1 diff --git a/Export-ExcelAsSQLInsert.ps1 b/Export-ExcelAsSQLInsert.ps1 new file mode 100644 index 0000000..70bcfa0 --- /dev/null +++ b/Export-ExcelAsSQLInsert.ps1 @@ -0,0 +1,38 @@ +function Export-ExcelAsSQLInsert { + param( + [Parameter(Mandatory = $true)] + $TableName, + [Alias("FullName")] + [Parameter(ValueFromPipelineByPropertyName = $true, ValueFromPipeline = $true, Mandatory = $true)] + [ValidateScript( { Test-Path $_ -PathType Leaf })] + $Path, + [Alias("Sheet")] + $WorkSheetname = 1, + [int]$HeaderRow = 1, + [string[]]$Header, + [switch]$NoHeader, + [switch]$DataOnly + ) + + $null = $PSBoundParameters.Remove('TableName') + $params = @{} + $PSBoundParameters + + $data = Import-Excel @params + + + $PropertyNames = $data[0].psobject.Properties | + Where-Object {$_.membertype -match 'property'} | + Select-Object -ExpandProperty name + + $ColumnNames = "'" + ($PropertyNames -join "', '") + "'" + + foreach ($record in $data) { + $values = $(foreach ($propertyName in $PropertyNames) { + $record.$propertyName + }) + + $targetValues = "'" + ($values -join "', '") + "'" + + "INSERT INTO {0} ({1}) Values({2});" -f $TableName, $ColumnNames, $targetValues + } +} \ No newline at end of file diff --git a/ImportExcel.psd1 b/ImportExcel.psd1 index f16bbc0..36999a1 100644 --- a/ImportExcel.psd1 +++ b/ImportExcel.psd1 @@ -4,7 +4,7 @@ RootModule = 'ImportExcel.psm1' # Version number of this module. -ModuleVersion = '2.4.0' +ModuleVersion = '3.0.0' # ID used to uniquely identify this module GUID = '60dd4136-feff-401a-ba27-a84458c57ede' diff --git a/ImportExcel.psm1 b/ImportExcel.psm1 index c3f38f9..7aac47a 100644 --- a/ImportExcel.psm1 +++ b/ImportExcel.psm1 @@ -20,6 +20,7 @@ Add-Type -Path "$($PSScriptRoot)\EPPlus.dll" . $PSScriptRoot\TrackingUtils.ps1 . $PSScriptRoot\Copy-ExcelWorkSheet.ps1 . $PSScriptRoot\Set-CellStyle.ps1 +. $PSScriptRoot\Export-ExcelAsSQLInsert.ps1 if($PSVersionTable.PSVersion.Major -ge 5) { . $PSScriptRoot\plot.ps1 From 615f677b2e8124cff7b1cf4400d7a33b4be12154 Mon Sep 17 00:00:00 2001 From: dfinke Date: Thu, 15 Jun 2017 19:34:04 -0400 Subject: [PATCH 2/2] Added convert from Excel --- ConvertFromExcelData.ps1 | 50 +++++++++++++++++++++++++++++++++ ConvertFromExcelToSQLInsert.ps1 | 46 ++++++++++++++++++++++++++++++ Export-ExcelAsSQLInsert.ps1 | 38 ------------------------- ImportExcel.psm1 | 3 +- 4 files changed, 98 insertions(+), 39 deletions(-) create mode 100644 ConvertFromExcelData.ps1 create mode 100644 ConvertFromExcelToSQLInsert.ps1 delete mode 100644 Export-ExcelAsSQLInsert.ps1 diff --git a/ConvertFromExcelData.ps1 b/ConvertFromExcelData.ps1 new file mode 100644 index 0000000..bd75895 --- /dev/null +++ b/ConvertFromExcelData.ps1 @@ -0,0 +1,50 @@ +function ConvertFrom-ExcelData { + <# + .SYNOPSIS + Reads data from a sheet, and for each row, calls a custom scriptblock with a list of property names and the row of data. + + + .EXAMPLE + ConvertFrom-ExcelData .\testSQLGen.xlsx { + param($propertyNames, $record) + + $reportRecord = @() + foreach ($pn in $propertyNames) { + $reportRecord += "{0}: {1}" -f $pn, $record.$pn + } + $reportRecord +="" + $reportRecord -join "`r`n" +} + +First: John +Last: Doe +The Zip: 12345 +.... + #> + param( + [Alias("FullName")] + [Parameter(ValueFromPipelineByPropertyName = $true, ValueFromPipeline = $true, Mandatory = $true)] + [ValidateScript( { Test-Path $_ -PathType Leaf })] + $Path, + [ScriptBlock]$scriptBlock, + [Alias("Sheet")] + $WorkSheetname = 1, + [int]$HeaderRow = 1, + [string[]]$Header, + [switch]$NoHeader, + [switch]$DataOnly + ) + + $null = $PSBoundParameters.Remove('scriptBlock') + $params = @{} + $PSBoundParameters + + $data = Import-Excel @params + + $PropertyNames = $data[0].psobject.Properties | + Where-Object {$_.membertype -match 'property'} | + Select-Object -ExpandProperty name + + foreach ($record in $data) { + & $scriptBlock $PropertyNames $record + } +} \ No newline at end of file diff --git a/ConvertFromExcelToSQLInsert.ps1 b/ConvertFromExcelToSQLInsert.ps1 new file mode 100644 index 0000000..c089267 --- /dev/null +++ b/ConvertFromExcelToSQLInsert.ps1 @@ -0,0 +1,46 @@ +function ConvertFrom-ExcelToSQLInsert { + param( + [Parameter(Mandatory = $true)] + $TableName, + [Alias("FullName")] + [Parameter(ValueFromPipelineByPropertyName = $true, ValueFromPipeline = $true, Mandatory = $true)] + [ValidateScript( { Test-Path $_ -PathType Leaf })] + $Path, + [Alias("Sheet")] + $WorkSheetname = 1, + [int]$HeaderRow = 1, + [string[]]$Header, + [switch]$NoHeader, + [switch]$DataOnly + ) + + $null = $PSBoundParameters.Remove('TableName') + $params = @{} + $PSBoundParameters + + ConvertFrom-ExcelData @params { + param($propertyNames, $record) + + $ColumnNames = "'" + ($PropertyNames -join "', '") + "'" + $values = foreach ($propertyName in $PropertyNames) { $record.$propertyName } + $targetValues = "'" + ($values -join "', '") + "'" + + "INSERT INTO {0} ({1}) Values({2});" -f $TableName, $ColumnNames, $targetValues + } + # $data = Import-Excel @params + + # $PropertyNames = $data[0].psobject.Properties | + # Where-Object {$_.membertype -match 'property'} | + # Select-Object -ExpandProperty name + + # $ColumnNames = "'" + ($PropertyNames -join "', '") + "'" + + # foreach ($record in $data) { + # $values = $(foreach ($propertyName in $PropertyNames) { + # $record.$propertyName + # }) + + # $targetValues = "'" + ($values -join "', '") + "'" + + # "INSERT INTO {0} ({1}) Values({2});" -f $TableName, $ColumnNames, $targetValues + # } +} \ No newline at end of file diff --git a/Export-ExcelAsSQLInsert.ps1 b/Export-ExcelAsSQLInsert.ps1 deleted file mode 100644 index 70bcfa0..0000000 --- a/Export-ExcelAsSQLInsert.ps1 +++ /dev/null @@ -1,38 +0,0 @@ -function Export-ExcelAsSQLInsert { - param( - [Parameter(Mandatory = $true)] - $TableName, - [Alias("FullName")] - [Parameter(ValueFromPipelineByPropertyName = $true, ValueFromPipeline = $true, Mandatory = $true)] - [ValidateScript( { Test-Path $_ -PathType Leaf })] - $Path, - [Alias("Sheet")] - $WorkSheetname = 1, - [int]$HeaderRow = 1, - [string[]]$Header, - [switch]$NoHeader, - [switch]$DataOnly - ) - - $null = $PSBoundParameters.Remove('TableName') - $params = @{} + $PSBoundParameters - - $data = Import-Excel @params - - - $PropertyNames = $data[0].psobject.Properties | - Where-Object {$_.membertype -match 'property'} | - Select-Object -ExpandProperty name - - $ColumnNames = "'" + ($PropertyNames -join "', '") + "'" - - foreach ($record in $data) { - $values = $(foreach ($propertyName in $PropertyNames) { - $record.$propertyName - }) - - $targetValues = "'" + ($values -join "', '") + "'" - - "INSERT INTO {0} ({1}) Values({2});" -f $TableName, $ColumnNames, $targetValues - } -} \ No newline at end of file diff --git a/ImportExcel.psm1 b/ImportExcel.psm1 index 7aac47a..02f8b40 100644 --- a/ImportExcel.psm1 +++ b/ImportExcel.psm1 @@ -20,7 +20,8 @@ Add-Type -Path "$($PSScriptRoot)\EPPlus.dll" . $PSScriptRoot\TrackingUtils.ps1 . $PSScriptRoot\Copy-ExcelWorkSheet.ps1 . $PSScriptRoot\Set-CellStyle.ps1 -. $PSScriptRoot\Export-ExcelAsSQLInsert.ps1 +. $PSScriptRoot\ConvertFromExcelData.ps1 +. $PSScriptRoot\ConvertFromExcelToSQLInsert.ps1 if($PSVersionTable.PSVersion.Major -ge 5) { . $PSScriptRoot\plot.ps1