Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error saving file (on Ubuntu) #69

Open
Roberto-1981 opened this issue May 22, 2018 · 1 comment
Open

Error saving file (on Ubuntu) #69

Roberto-1981 opened this issue May 22, 2018 · 1 comment

Comments

@Roberto-1981
Copy link

Hi there,
I successfully used this great module on WS2012R2 and I tried to do the same on an Azure VM running Ubuntu where I installed PowerShell version 6.1

PSExcel was successfully installed with the Install-Module command and all module methods used so far were working as expected... until I tried to save the current $Excel object as shown below:

$Excel | Close-Excel -Save

Close-Excel : Error saving file. Will not close this ExcelPackage: Exception calling "Save" with "0" argument(s): "Error saving file /myfolder/TemplateTest.xlsx"
At line:1 char:11

  • $Excel | Close-Excel -Save
  •       ~~~~~~~~~~~~~~~~~
    
  • CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
  • FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Close-Excel

Maybe there is something in the way relative/absolute paths are handled that should be adapted to run against the Linux file system?
Please let me know whether additional details are required to help fixing this issue..

Thanks!

Roberto

@JohnLBevan
Copy link

For others hitting this issue, a few solutions to this error message (note: this is not an exhaustive list):

Empty File

If you create a new excel file with no conent, you won't be able to save it:

if (Test-Path 'c:\temp\empty.xlsx') {
    Remove-Item 'c:\temp\empty.xlsx'
}
$xl = New-Excel -Path 'c:\temp\empty.xlsx'
$xl | Close-Excel -Save

Close-Excel: Error saving file. Will not close this ExcelPackage: Exception calling "Save" with "0" argument(s): "Error saving file C:\temp\empty.xlsx"

The fix here: ensure you output some content to your file. If you just needed a blank file you wouldn't be using this cmdlet, so presumably this is done in error; check your code to ensure the data you expect to be output, is.

Existing Non-Excel File

If you create an excel file using a filename which already exists but is not an Excel file, then you open the non-excel file by using New-Excel instead of just overwriting the blank file by using the save method in Export-Xlsx you'll hit issues when saving.

$tempFn = New-TemporaryFile | Select-Object -ExpandPropety FullName
Test-Path -Path $tempFn  # note: the above command doesn't just give you a filename, but creates that file
$xl = New-Excel -path $tempFn 
1..10 | %{[PSCustomObject]@{Item=$_;AnotherCol = "Hello $_"}} | Export-Xlsx -Excel $xl
$xl | Close-Excel -Save

Close-Excel: Error saving file. Will not close this ExcelPackage: Exception calling "Save" with "0" argument(s): "Error saving file C:\Users\username\AppData\Local\Temp\tmpFDD3.tmp"

In this case you can get around the issue in a couple of ways. Either delete the file created by New-TemporaryFile before calling New-Excel, or don't call New-Excel and instead use Export-Xlsx` to create the new file with content, overwriting the existing file. Full example of this second approach below:

$tempFn = New-TemporaryFile | Select-Object -ExpandPropety FullName
$xl = 1..10 | %{[PSCustomObject]@{Item=$_;AnotherCol = "Hello $_"}} | 
    Export-Xlsx -Excel $xl -Force -ReplaceSheet -PassThru
$xl | Close-Excel -Save

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants