dfinke / ImportExcel

PowerShell module to import/export Excel spreadsheets, without Excel

Home Page:https://www.powershellgallery.com/packages/ImportExcel/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Error saving after using Add-Worksheet with this specific file

mavhc opened this issue · comments

scienceTemplate.xlsx
test.xlsx
Version 7.8.6

Using the 2 files attached, the follow code gives an error


$template = Open-ExcelPackage "scienceTemplate.xlsx"
$wsScience = $template.Workbook.Worksheets["Science"]

$destination = "test.xlsx"

$excel = Open-ExcelPackage $destination

$ws = Add-Worksheet -ExcelPackage $excel -WorkSheetname "Science" -ClearSheet -CopySource $wsScience

Close-ExcelPackage $excel

Results in:

At C:\Program Files\WindowsPowerShell\Modules\ImportExcel\7.8.6\Public\Close-ExcelPackage.ps1:29 char:20
+             else { $ExcelPackage.Save() }

Using a simpler template file works fine, tried to find a minimal case, and ended up deleting everything, so possibly something about the formatting is the problem?

possible. please post a simpler repro with data, and the scritp and I can take a look

So this works. I'm wondering if the issue is that the science sheet you have has formatting in it.

$xlSrc = "$PSScriptRoot\ImportExcelExample.xlsx"
$xlDst = "$PSScriptRoot\myTest.xlsx"

Remove-Item $xlSrc -ErrorAction Ignore
Remove-Item $xlDst -ErrorAction Ignore

1..10 | Export-Excel $xlSrc 

$src = Open-ExcelPackage $xlSrc
$dst = Open-ExcelPackage $xlDst -Create

$null = Add-Worksheet -ExcelPackage $dst -WorksheetName Test -CopySource $src.Sheet1 -ClearSheet

Close-ExcelPackage $src
Close-ExcelPackage $dst -Show