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