mirror of
https://github.com/dfinke/ImportExcel.git
synced 2025-12-06 00:23:20 +00:00
Merge pull request #1110 from DavisHenckel/FAQ_Docs
This commit is contained in:
6
FAQ/How to Create an Empty Excel File.md
Normal file
6
FAQ/How to Create an Empty Excel File.md
Normal 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"
|
||||
```
|
||||
41
FAQ/How to Read an Existing Excel File.md
Normal file
41
FAQ/How to Read an Existing Excel File.md
Normal 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
|
||||

|
||||
|
||||
After loading this data into ```$ExcelFile``` the data is stored like:
|
||||

|
||||
|
||||
## 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
|
||||
}
|
||||
```
|
||||
34
FAQ/How to Write to an Existing Excel File.md
Normal file
34
FAQ/How to Write to an Existing Excel File.md
Normal 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:
|
||||

|
||||
### 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:
|
||||

|
||||
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:
|
||||

|
||||
### Load Value at Specific Index
|
||||
```powershell
|
||||
$ValueAtIndex = $WorkSheet[2,1].Value #Loads the value at row 2, column A
|
||||
```
|
||||
```$ValueAtIndex``` now contains: 
|
||||
### 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.
|
||||
|
||||
BIN
images/FAQ_Images/DataStructureExcelPkg.png
Normal file
BIN
images/FAQ_Images/DataStructureExcelPkg.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 3.8 KiB |
BIN
images/FAQ_Images/ExcelFileContents.png
Normal file
BIN
images/FAQ_Images/ExcelFileContents.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 4.3 KiB |
BIN
images/FAQ_Images/ExcelFileContentsPostAdd.png
Normal file
BIN
images/FAQ_Images/ExcelFileContentsPostAdd.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 5.1 KiB |
BIN
images/FAQ_Images/ExcelFileDebugImg.jpg
Normal file
BIN
images/FAQ_Images/ExcelFileDebugImg.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 12 KiB |
BIN
images/FAQ_Images/ValueAtIndexData.png
Normal file
BIN
images/FAQ_Images/ValueAtIndexData.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.4 KiB |
Reference in New Issue
Block a user