Files
ImportExcel/FAQ/How to Read an Existing Excel File.md
Davis Henckel e9b437af4e Complete first version of ExistingExcelFile FAQ
Added loading a row/col, then mapping to HashTable
2021-11-26 06:56:08 -08:00

1.3 KiB

How to Read an existing Excel File

Import-Module ImportExcel
#Loads the Excel file into a is a custom PS Object
$ExcelFile = Import-Excel "C:\Test\file.xlsx" -WorksheetName "Sheet1" 

Visual of Data Structure

The File C:\Test\file.xlsx contains
ExcelFileContents

After Loading this data into $ExcelFile the data is stored like:
ExcelFileDebugImg

Other Common Operations

Load a Column

$SpecificColumn = $ExcelFile."anotherHeader" #loads column with the header "another header" store into an array

Load a Row

$SpecificRow = $ExcelFile[1] #Loads row at index 1. Index 1 is the first row instead of 0. 

Map Contents to HashTable to interpret data

Sometimes mapping to a HashTable is more convenient to have access to common Hashtable operations. Enumerate a HashTable with the Row data properties by:

$HashTable = @{}
$SpecificRow= $ExcelFile[2]
$SpecificRow.psobject.properties | ForEach-Object {$HashTable[$_.Name] = $_.Value}

To then iterate through the enumerated hash...

ForEach ($Key in ($HashTable.GetEnumerator()) | Where-Object {$_.Value -eq "x"}){ #Only grabs a key where the value is "x"
    #values accessible with $Key.Name or $Key.Value
}