diff --git a/.gitignore b/.gitignore index 96374c4..8995bc2 100644 --- a/.gitignore +++ b/.gitignore @@ -41,3 +41,4 @@ $RECYCLE.BIN/ Network Trash Folder Temporary Items .apdisk +InstallModule.ps1 diff --git a/EPPlus.dll b/EPPlus.dll new file mode 100644 index 0000000..2bfd08a Binary files /dev/null and b/EPPlus.dll differ diff --git a/ImportExcel.psm1 b/ImportExcel.psm1 new file mode 100644 index 0000000..cc305b3 --- /dev/null +++ b/ImportExcel.psm1 @@ -0,0 +1,44 @@ +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 + } +} \ No newline at end of file diff --git a/TryEPPlus.ps1 b/TryEPPlus.ps1 new file mode 100644 index 0000000..60e3707 --- /dev/null +++ b/TryEPPlus.ps1 @@ -0,0 +1,7 @@ +#cls + +import-module +#Import-Excel .\Book1.xlsx |ft +#Import-Excel .\TestRead.xlsx +#Import-Excel .\testImport.xlsx 1 (echo a b c d) | Out-String +#Import-Excel .\testImport.xlsx 2 (echo e f g h) | Out-String \ No newline at end of file diff --git a/testImport.xlsx b/testImport.xlsx new file mode 100644 index 0000000..0ab462d Binary files /dev/null and b/testImport.xlsx differ