mirror of
https://github.com/dfinke/ImportExcel.git
synced 2025-12-06 00:23:20 +00:00
Added Export-ExcelAsSQLInsert
This commit is contained in:
38
Export-ExcelAsSQLInsert.ps1
Normal file
38
Export-ExcelAsSQLInsert.ps1
Normal 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
|
||||
}
|
||||
}
|
||||
@@ -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'
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user