C# 保存为使用 EPPlus?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12912912/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 00:57:05  来源:igfitidea点击:

Save as using EPPlus?

c#epplus

提问by Pomster

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

有谁知道如何使用 package.Saveas 函数?

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

“OfficeOpenXml.ExcelPackage.SaveAs(System.IO.Stream)”的最佳重载方法匹配有一些无效参数

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.

save as 将正确关闭包,但它不接受我的文件路径。



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.

包对象已关闭并已释放,因此无法对此对象或在此包的一部分上打开的任何流执行操作。

采纳答案by Han

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

在您调用任何函数GetAsByteArray, Save,后,该包将被关闭和处理SaveAs。这就是你收到消息的原因

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 Loadfunction 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.

解决方法是保存后调用Load函数继续处理excel文件。或者,如果您只想同时获得 ByteArray 和 FileOutput,我相信它们都是相同的。

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:

或者您可以在获取 ByteArray 后将数据保存到磁盘:

byte[] data = package.GetAsByteArray();

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

回答by usr

SaveAswould be accepting your aFileStream.

SaveAs会接受你的aFile流。

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

您可以通过查看函数签名找出自己这样的事情:SaveAs(System.IO.Stream)。它需要一个Stream. 传递 astring不可能编译,所以你必须以某种方式弥补一个有用的Stream(你做了)。

回答by Maximus

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:

我是来寻找答案的,但现有的答案对我来说并不明确。这是我使用 EPPlus 和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);
}

回答by nawfal

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

我不知道从哪个版本开始,但 EPPlus 的SaveAs方法接受FileInfo. 所以你可以做这样的事情:

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

Unlike the Savemethod SaveAsmethod overwritesfile as well in case file name already exists.

不同于Save方法SaveAs方法重写文件以及万一文件名已经存在。

回答by LosManos

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

摆脱多余的package.GetAsByteArray电话,你应该解决它。

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();