New-KFExcelFile
Syntax
New-KFExcelFile
-Input <list-of-hashtables>
-SheetName <string>
-AsStream
[-InFile <template-excel-file>]
[-Fields <list-of-strings>]
[-BeforeWritingCell <scriptblock>]
[-ThrowErrors[:$true]]
New-KFExcelFile
-Input <list-of-hashtables>
-SheetName <string>
-AsBase64
[-InFile <template-excel-file>]
[-Fields <list-of-strings>]
[-BeforeWritingCell <scriptblock>]
[-ThrowErrors[:$true]]
New-KFExcelFile
-Input <list-of-hashtables>
-SheetName <string>
-OutFile <path>
[-InFile <template-excel-file>]
[-Fields <list-of-strings>]
[-BeforeWritingCell <scriptblock>]
[-ThrowErrors[:$true]]
Support
>= kenaflow 4.0.10
Return
In case of using -AsStream it returns a System.IO.MemoryStreaam object.
In case of using -AsBase64 it returns a string.
In case of using -OutFile it returns nothing.
Description
This cmdlet can be used to convert a list of PowerShell hashtables to an MS Excel file.
This is a such a list of hashtables:
@(
@{FieldA = "kenaflow"; FieldB = 1; FieldC = [DateTime]::Now}
@{FieldA = "is" ; FieldB = 2 ; FieldD = 10.101}
@{FieldA = "cool"; FieldB = 3;
)
There are three different output options:
- as System.IO.MemoryString by using
-AsStream - as Base64 encoded byte array using
-AsBase64 - or as file on disk using
-OutFile <path>
You can only use one output option.
You have to specify the input object with -Input and the name of the workbook sheet using -SheetName.
If not using -Infile the cmdlet creates a new file as output.
If you specify an existing Excel file with -InFile <template-excel-file> you have to 3 options:
- Specify the input file content as Base64 encoded string or
- specify the input file as System.IO.Sream object or
- speficy a full or relative path to an existing file on disk. It's relative to the current workflow folder.
With the input file you can do two things:
If the worksheet named with
-SheetNamealready exists the cmdlet will take the field names from the 1st row. Than it will take the formatting for the data rows from the 2nd row. Everything starting with row 2 will be removed from the excel file. But no other worksheet is modified! The cell formats are not modified.If the worksheet named with
-SheetNamedoes not exist it will be added using the default styling.
Parameters
-Input <list-of-hashtables>
The inpupt object: a List of PowerShell hashtables
-SheetName <string>
The name of the Excel worksheet.
-AsStream
The result is a System.IO.MemoryStream object
-AsBase64
The result is a Base64 encoded byte array that represents the Excel file.
-OutFile <path>
The result is written to a file on disk. The path can be relative to the current workflow folder.
[-InFile <template-excel-file>]
An Excel file as template. The new worksheet will be added or the existing worksheet will be modified.
- The template can be specified as Base64 encoded string or
- as System.IO.Sream object or
- as a full or relative path to an existing file on disk. It's relative to the current workflow folder.
[-Fields <list-of-strings>]
The field order can be specified here. If used only the specified fields are used as keys and read from the hastable objects in the list specified with
-Input.
[-BeforeWritingCell <scriptblock>]
A script block that is called for every data cell before it is written. It can change the value. The script block gets these arguments (in this order):
- the current row as hashtable (an element of
-Input)- the field name (column key)
- the row number in the worksheet (the header is row 1, so the first data row is 2)
- the column number (starting with 1)
- the current value
The first output of the script block is used as the new cell value. If it returns nothing (``), an empty cell is written. The script block is not called for fields that do not exist in the row hashtable.
New-KFExcelFile -Input $data -SheetName "Data" -OutFile "out.xlsx" -BeforeWritingCell { param($row, $field, $rowNum, $colNum, $value) if ($field -eq "Amount") { [math]::Round($value, 2) } else { $value } }
[-ThrowErrors[:$true]]
This is a default parameter for all kenaflow cmdlets. If set to
$true(default!) the engine will pass exception within the cmdlet to the script for further handling.
Example
New-KFExcelFile -input @(
@{FieldA = "kenaflow"; FieldB = [DateTime]::Now; FieldC = 10.101}
@{FieldA = "is"; FieldB = [DateTime]::Now.AddDays(1); FieldC = 10.101}
@{FieldA = "cool"; FieldB = [DateTime]::Now.addmonths(1); FieldC = 10.101}
@{FieldA = "!"; FieldB = [DateTime]::Now.addyears(1); FieldC = 10.101}
) -OutFile "test1.xlsx" -SheetName "test1"
Result:
New-KFExcelFile -input @(
@{FieldA = 1; FieldB = [DateTime]::Now; FieldC = 10.101; FieldD = "Hi"}
@{FieldA = 1; FieldB = [DateTime]::Now.AddDays(1); FieldC = 10.101; FieldD = "Hey"}
@{FieldA = 1; FieldB = [DateTime]::Now.AddMonths(1); FieldC = 10.101; FieldD = "Salut"}
@{FieldA = 1; FieldB = [DateTime]::Now; FieldC = 10.101; FieldD = "Hallo"}
) -Fields @("FieldA", "FieldB", "FieldD") -OutFile "test2.xlsx" -SheetName "Test2"
Result:
New-KFExcelFile -input @(
@{FieldA = 1; FieldB = [DateTime]::Now; FieldC = 10.101; FieldD = "Hi"}
@{FieldA = 1; FieldB = [DateTime]::Now.AddDays(1); FieldC = 10.101; FieldD = "Hey"}
@{FieldA = 1; FieldB = [DateTime]::Now.AddMonths(1); FieldC = 10.101; FieldD = "Salut"}
@{FieldA = 1; FieldB = [DateTime]::Now.AddYears(1); FieldC = 10.101; FieldD = "Hallo"}
) -Fields @("FieldA", "FieldB", "FieldE") -OutFile "test3.xlsx" -InFile "$PsscriptRoot\inp.xlsx" -SheetName "test1"
Template file:
Result:
New-KFExcelFile -input @(
@{FieldA = 1; FieldB = [DateTime]::Now; FieldC = 10.101; FieldD = "Hi"}
@{FieldA = 1; FieldB = [DateTime]::Now.AddDays(1); FieldC = 10.101; FieldD = "Hey"}
@{FieldA = 1; FieldB = [DateTime]::Now.AddMonths(1); FieldC = 10.101; FieldD = "Salut"; FieldG="some value"}
@{FieldA = 1; FieldB = [DateTime]::Now.AddYears(1); FieldC = 10.101; FieldD = "Hallo"}
) -OutFile "test4.xlsx" -InFile "$PsscriptRoot\inp2.xlsx" -SheetName "test1"
Template File
Result





