Merge branch 'FAQ_Docs' of https://github.com/DavisHenckel/ImportExcel into FAQ_Docs_2

This commit is contained in:
Davis Henckel
2021-11-26 10:55:43 -08:00

View File

@@ -1,12 +1,40 @@
# How to Read an existing Excel File # How to Read an Existing Excel File
```powershell ```powershell
Import-Module ImportExcel Import-Module ImportExcel
#Loads the Excel file into a is a custom PS Object #Loads the Excel file into a PSCustomObject
$ExcelFile = Import-Excel "C:\Test\file.xlsx" -WorksheetName "Sheet1" $ExcelFile = Import-Excel "C:\Test\file.xlsx" -WorksheetName "Sheet1"
``` ```
## Visual of Data Structure ## Visual of Data Structure
The File C:\Test\file.xlsx contains The File C:\Test\file.xlsx contains
![ExcelFileContents](/images/FAQ_Images/ExcelFileContents.png)
After Loading this data into ```$ExcelFile``` the data is stored like: After loading this data into ```$ExcelFile``` the data is stored like:
![ExcelFileDebugImg](/images/FAQ_Images/ExcelFileDebugImg.jpg)
## Other Common Operations
### Load a Column
```powershell
$SpecificColumn = $ExcelFile."anotherHeader" #loads column with the header "anotherHeader" -- data stored in an array
```
### Load a Row
```powershell
$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's data by:
```powershell
$HashTable = @{}
$SpecificRow= $ExcelFile[2]
$SpecificRow.psobject.properties | ForEach-Object {$HashTable[$_.Name] = $_.Value}
```
To then iterate through the enumerated Hashtable:
```powershell
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
}
```