Merge pull request #1110 from DavisHenckel/FAQ_Docs

This commit is contained in:
Doug Finke
2021-11-30 18:32:59 -05:00
committed by GitHub
8 changed files with 81 additions and 0 deletions

View File

@@ -0,0 +1,6 @@
# Create an Empty Excel File
Use an empty string to export to an excel file.
```powershell
#Build an Excel file named: "file.xlsx" containing a worksheet: "MyWorksheet"
"" | Export-Excel -Path "C:\Test\file.xlsx" -WorksheetName "MyWorksheet"
```

View File

@@ -0,0 +1,41 @@
# How to Read an Existing Excel File
## Enumerate the Excel File Contents
```powershell
#Load the Excel file into a PSCustomObject
$ExcelFile = Import-Excel "C:\Test\file.xlsx" -WorksheetName "Sheet1"
```
## Visual of Data Structure
The File C:\Test\file.xlsx contains
![ExcelFileContents](/images/FAQ_Images/ExcelFileContents.png)
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
}
```

View File

@@ -0,0 +1,34 @@
# Write to an Existing Excel File
### Enumerate the Excel File
The cmdlets ```Open-ExcelPackage``` and ```Close-ExcelPackage``` allow for direct modification to Excel file contents.
```powershell
$ExcelPkg = Open-ExcelPackage -Path "C:\Test\file.xlsx"
```
Contents of file.xlsx:
![ExcelFileContents](/images/FAQ_Images/ExcelFileContents.png)
### Enumerate the Worksheet to View or Modify the Data
```powershell
$WorkSheet = $ExcelPkg.Workbook.Worksheets["sheet1"].Cells #open excel worksheet cells from worksheet "sheet1"
```
Visual of data structure:
![DataStructureExcelPkg](/images/FAQ_Images/DataStructureExcelPkg.png)
A1 contains "someHeader", A2 contains "data1" etc.
### Modify a Specific Value in a File
Values can be accessed by row, column. Similar to a 2D array.
```powershell
$WorkSheet[1,4].Value = "New Column Header" #Starts at index 1 not 0
```
Contents of file.xlsx after modifying:
![ExcelFileContentsPostAdd](/images/FAQ_Images/ExcelFileContentsPostAdd.png)
### Load Value at Specific Index
```powershell
$ValueAtIndex = $WorkSheet[2,1].Value #Loads the value at row 2, column A
```
```$ValueAtIndex``` now contains: ![ValueAtIndexData](/images/FAQ_Images/ValueAtIndexData.png)
### Save File After Modifying
The changes will not display in the Excel file until Close-ExcelPackage is called.
```powershell
Close-ExcelPackage $ExcelPkg #close and save changes made to the Excel file.
```
**Note**: If the file is currently in use, Close-ExcelPackage will return an error and will not save the information.

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB