Neptune443

Get-ExcelSheetInfo

Jan 7th, 2016
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Function Get-ExcelSheetInfo {
  2.     <#
  3.     .SYNOPSIS
  4.         Get worksheet names and their indices of an Excel workbook.
  5.  
  6.     .DESCRIPTION
  7.         The Get-ExcelSheetInfo cmdlet gets worksheet names and their indices of an Excel workbook.
  8.  
  9.     .PARAMETER Path
  10.         Specifies the path to the Excel file. This parameter is required.
  11.              
  12.     .EXAMPLE
  13.         Get-ExcelSheetInfo .\Test.xlsx
  14.  
  15.     .NOTES
  16.         CHANGELOG
  17.         2016/01/07 Added Created by Johan Akerstrom (https://github.com/CosmosKey)
  18.  
  19.     .LINK
  20.         https://github.com/dfinke/ImportExcel
  21.  
  22.     #>
  23.    
  24.     [CmdletBinding()]
  25.     param(
  26.         [Alias("FullName")]
  27.         [Parameter(ValueFromPipelineByPropertyName=$true, ValueFromPipeline=$true, Mandatory=$true)]
  28.         $Path
  29.     )
  30.     process {
  31.         $Path = (Resolve-Path $Path).Path
  32.         write-debug "target excel file $Path"
  33.         $stream = New-Object -TypeName System.IO.FileStream -ArgumentList $Path,"Open","Read","ReadWrite"
  34.         $xl = New-Object -TypeName OfficeOpenXml.ExcelPackage -ArgumentList $stream
  35.         $workbook  = $xl.Workbook
  36.         if($workbook -and $workbook.Worksheets) {
  37.             $workbook.Worksheets |
  38.                 Select-Object -Property name,index,hidden,@{
  39.                     Label = "Path"
  40.                     Expression = {$Path}
  41.                 }
  42.         }
  43.     }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment