Skip to content

Adding a worksheet to existing file #1476

Answered by cristtos
MarianiVentura asked this question in Q&A
Discussion options

You must be logged in to vote

I believe that if the Excel file $FinalFilePath exists and the worksheet in -WorksheetName does not exist - it will be just added in the existing file.
In my case I prepare a $DataTable extract from SQL and copy an existing "template" file into the working directory as $FinalFilePath
Then I run:
$DataTable | Export-Excel $FinalFilePath -WorksheetName "NewSheet"
This adds the new NewSheet and populates it.
Once this is done I use

$workbook = Open-ExcelPackage -Path $FinalFilePath
$CurrentPage = $workbook.Workbook.Worksheets["NewSheet"]

to manipulate elements of the NewSheet (format, values, formulae):

$range = $CurrentPage.Cells["F1"]
$range.Value = ("Configuration as of Today " + (Get-Da…

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
1 reply
@MarianiVentura
Comment options

Answer selected by MarianiVentura
Comment options

You must be logged in to vote
1 reply
@MarianiVentura
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
3 participants