Added Export-ExcelAsSQLInsert

This commit is contained in:
dfinke
2017-06-14 22:44:11 -04:00
parent dc67012590
commit 26f6df7168
3 changed files with 40 additions and 1 deletions

View File

@@ -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
}
}

View File

@@ -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'

View File

@@ -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