New-Excel.ps1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
function New-Excel { <# .SYNOPSIS Create an OfficeOpenXml ExcelPackage to work with .DESCRIPTION Create an OfficeOpenXml ExcelPackage to work with .PARAMETER Path Path to an xlsx file to open .EXAMPLE $Excel = New-Excel -Path "C:\Excel.xlsx" $Excel.Workbook #Open C:\Excel.xlsx, view the workbook .NOTES Thanks to Doug Finke for his example: https://github.com/dfinke/ImportExcel/blob/master/ImportExcel.psm1 Thanks to Philip Thompson for an expansive set of examples on working with EPPlus in PowerShell: https://excelpslib.codeplex.com/ .LINK https://github.com/RamblingCookieMonster/PSExcel .FUNCTIONALITY Excel #> [OutputType([OfficeOpenXml.ExcelPackage])] [cmdletbinding()] param( [parameter( Mandatory=$false, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)] [validatescript({ $Parent = Split-Path $_ -Parent if( -not (Test-Path -Path $Parent -PathType Container) ) { Throw "Specify a valid path. Parent '$Parent' does not exist: $_" } $True })] [string]$Path ) Process { if($path) { #Resolve relative paths... Thanks Oisin! http://stackoverflow.com/a/3040982/3067642 $Path = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($Path) Write-Verbose "Creating excel object with path '$path'" New-Object OfficeOpenXml.ExcelPackage $Path } else { Write-Verbose "Creating excel object with no specified path" New-Object OfficeOpenXml.ExcelPackage } } } |