24

Does any one know how to use the package.Saveas function?

package.SaveAs(tempFolderPathAlt + saveas + ".xlsx");

At the moment this is underlined in red with the following error:

The best overloaded method match for 'OfficeOpenXml.ExcelPackage.SaveAs(System.IO.Stream)' has some invalid arguments

At the moment i'm saving the file in the following way.

FileStream aFile = new FileStream(tempFolderPathAlt + saveas + ".xls",    FileMode.Create);
byte[] byData = package.GetAsByteArray();
aFile.Seek(0, SeekOrigin.Begin);
aFile.Write(byData, 0, byData.Length);
aFile.Close();

But this way the package remains open and i cant work with files it has used.

The save as will close the package properly, but its not accepting my file path.


Edit

I tried this:

using (FileStream aFile = new FileStream(tempFolderPathAlt + saveas + ".xlsx", FileMode.Create))
{
    byte[] byData = package.GetAsByteArray();
    aFile.Seek(0, SeekOrigin.Begin);
    package.SaveAs(aFile);
    //aFile.Write(byData, 0, byData.Length);
    aFile.Close();
}

But Get the following error?

Package object was closed and disposed, so cannot carry out operations on this object or any stream opened on a part of this package.

5 Answers 5

38

The package will be closed & disposed after you call any of functions GetAsByteArray, Save, SaveAs. That is the reason why you got message

Package object was closed and disposed, so cannot carry out operations on this object or any stream opened on a part of this package.

The solution is that after the saving you call Load function to continue processing on excel file. Or if you just want to get both ByteArray & FileOutput, I'm sure with you they both are same.

You can read data after have saved file to the disk:

string path = @"C:\test1.xlsx";
Stream stream = File.Create(path);
package.SaveAs(stream);
stream.Close();

byte[] data = File.ReadAllBytes(path);

Or you can save data to disk after get the ByteArray:

byte[] data = package.GetAsByteArray();

string path = @"C:\test1.xlsx";
File.WriteAllBytes(path, data);
9

I came looking for the answer to this but the existing answers were not clear to me. Here is what I did using EPPlus and System.Windows.Forms:

ExcelPackage xlPackage = new ExcelPackage(xlsTmpFileName)

// Populate the Excel spreadsheet here.

SaveFileDialog sfd = new SaveFileDialog();
using (FileStream fs = new FileStream(sfd.FileName, FileMode.Create))
{
    xlPackage.SaveAs(fs);
}
4

I dont know from which version onwards but EPPlus's SaveAs method accepts a FileInfo. So you could do something like this:

using (var app = new ExcelPackage(new FileInfo(inputPath)))
{
    //process
    app.SaveAs(new FileInfo(outputPath));
}

Unlike the Save method SaveAs method overwrites file as well in case file name already exists.

1

SaveAs would be accepting your aFile Stream.

You can find out such things yourself by looking at the function signature: SaveAs(System.IO.Stream). It takes a Stream. Passing a string cannot possibly compile so you have to somehow make up a useful Stream (which you did).

3
  • Can i set the save as function to save to a file path?
    – Pomster
    Commented Oct 16, 2012 at 11:25
  • No because you cannot give it a path. You can give it your FileStream though.
    – usr
    Commented Oct 16, 2012 at 11:32
  • Try removing "package.GetAsByteArray()". Did you close the package somewhere before saving it? Anyway, the code you posted is the right approach. You might want to ask a new question concerning the new error message.
    – usr
    Commented Oct 16, 2012 at 13:38
1

Get rid of the surplus package.GetAsByteArray call and you should solve it.

I just ran:

using (FileStream aFile = new FileStream(@"C:\Temp\asdf.xlsx", FileMode.Create))
{
    aFile.Seek(0, SeekOrigin.Begin);
    package.SaveAs(aFile);
    aFile.Close();
}

// See here - I can still work with the spread sheet.
var worksheet = package.Workbook.Worksheets.Single(); 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.