问题描述
任何一个是否知道如何使用package.Saveas功能?
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:
为
的最佳重载方法匹配'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.
编辑:
我试过这样:
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对象已关闭和处置,因此不能进行操作这个对象或任何流对这个包的一部分开了。
Package object was closed and disposed, so cannot carry out operations on this object or any stream opened on a part of this package.
推荐答案
包将被关闭和放大器;处置你调用任何功能 GetAsByteArray
,保存
,另存为后code>。这就是为什么你得到消息的原因
The package will be closed & disposed after you call any of functions GetAsByteArray
, Save
, SaveAs
. That is the reason why you got message
的 Package对象被关闭和处置,因此不能进行操作这个对象或任何有关流关于这个软件包的一部分开了。的
解决的办法是,保存后你叫加载
函数继续对excel文件的处理。或者,如果你只是想获得两者的ByteArray&放大器; FileOutput,我敢肯定你他们都相同。
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);
或者你也可以将数据保存到磁盘上获得的ByteArray后:
Or you can save data to disk after get the ByteArray:
byte[] data = package.GetAsByteArray();
string path = @"C:\test1.xlsx";
File.WriteAllBytes(path, data);
这篇关于除使用EPPlus?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!