mirror of
https://github.com/dfinke/ImportExcel.git
synced 2025-12-06 00:23:20 +00:00
44 lines
1.0 KiB
PowerShell
44 lines
1.0 KiB
PowerShell
Add-Type -Path "$($PSScriptRoot)\EPPlus.dll"
|
|
|
|
function Import-Excel {
|
|
param(
|
|
[Parameter(ValueFromPipelineByPropertyName)]
|
|
$FullName,
|
|
$Sheet=1,
|
|
[string[]]$Header
|
|
)
|
|
|
|
Process {
|
|
|
|
$FullName = (Resolve-Path $FullName).Path
|
|
write-debug "target excel file $($FullName)"
|
|
|
|
$xl = New-Object OfficeOpenXml.ExcelPackage $FullName
|
|
|
|
$workbook = $xl.Workbook
|
|
|
|
$worksheet=$workbook.Worksheets[$Sheet]
|
|
$dimension=$worksheet.Dimension
|
|
|
|
$Rows=$dimension.Rows
|
|
$Columns=$dimension.Columns
|
|
|
|
if(!$Header) {
|
|
$Header = foreach ($Column in 1..$Columns) {
|
|
$worksheet.Cells[1,$Column].Text
|
|
}
|
|
}
|
|
|
|
foreach ($Row in 2..$Rows) {
|
|
$h=[Ordered]@{}
|
|
foreach ($Column in 0..($Columns-1)) {
|
|
$Name = $Header[$Column]
|
|
$h.$Name = $worksheet.Cells[$Row,($Column+1)].Text
|
|
}
|
|
[PSCustomObject]$h
|
|
}
|
|
|
|
$xl.Dispose()
|
|
$xl = $null
|
|
}
|
|
} |