Xam*_*Dev 0 c# epplus asp.net-web-api asp.net-core-webapi
我正在使用 epplus 库下载带有密码的 Excel 文件。下载部分工作正常。
public ActionResult DownloadExcel()
{
string sFileName = string.Empty;
using (ExcelPackage package = projectBL.DownloadExcel(ID, id1, id3, id4, id5))
{
System.Net.Mime.ContentDisposition cd = new System.Net.Mime.ContentDisposition
{
FileName = package.File.Name,
};
Response.Headers.Add("Content-Disposition", cd.ToString());
return File(package.GetAsByteArray("password"), "application/vndopenxmlformats-officedocument.spreadsheetml.sheet");
}
}
Run Code Online (Sandbox Code Playgroud)
但每当我尝试上传此受密码保护的文件时,都会出现以下错误
该文件不是有效的包文件。如果文件已加密,请在构造函数中提供密码。
因此,我通过提供读取文件所需的密码解决了上述错误。下面是相同的代码。
public ActionResult UploadExcel()
{
if (Request.Form.Files != null && Request.Form.Files.Count > 0)
{
var fileObject = Request.Form.Files[0];
string fileName = ContentDispositionHeaderValue.Parse(fileObject.ContentDisposition).FileName.Trim('"');
var Stream = fileObject.OpenReadStream();
ExcelPackage package = new ExcelPackage(Stream,"password");
projectBL.SaveData(package);
return Json("Records Saved Succesfully.");
}
}
Run Code Online (Sandbox Code Playgroud)
但是,在提供密码后,由于以下错误,我仍然无法读取/上传 Excel 文件
流必须是读/写的
所以我的问题是如何使用 Stream 读取受密码保护的文件。我在 web api 中使用这段代码。
对此有任何帮助表示赞赏!
它是 XLS 或 XLSX 文件吗?EPplus 无法处理 XLS 文件。
这是我简单地使用 EPPLUS 的方法:
using (ExcelPackage xlPackage = new ExcelPackage(new FileInfo(fileName), "password"))
{}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13797 次 |
| 最近记录: |