简体   繁体   English

在c#中使用epplus读取受密码保护的excel

[英]Reading password protected excel using epplus in c#

I am downloading the excel file with password using epplus library.我正在使用 epplus 库下载带有密码的 excel 文件。 The downloading part is working fine.下载部分工作正常。

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

    }
}

But whenever I am trying to upload this password protected file it is giving me below error但是每当我尝试上传这个受密码保护的文件时,它都会给我以下错误

The file is not an valid Package file.该文件不是有效的包文件。 If the file is encrypted, please supply the password in the constructor.如果文件已加密,请在构造函数中提供密码。

So, I have resolved the above error my providing the required password to read the file.所以,我已经解决了上述错误,我提供了读取文件所需的密码。 Below is the code for the same.下面是相同的代码。

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.");
    }
}

However, after providing the password still I am not able to read/upload the excel file due to below error但是,在提供密码后,由于以下错误,我仍然无法读取/上传 excel 文件

The stream must be read/write流必须是读/写的

So my question is how can i read the password protected file using Stream.所以我的问题是如何使用 Stream 读取受密码保护的文件。 I am using this code in web api.我在 web api 中使用此代码。

Any help on this appreciated !对此的任何帮助表示赞赏!

Is it a XLS or XLSX file?它是 XLS 还是 XLSX 文件? EPplus cannot works with XLS files. EPplus 不能处理 XLS 文件。

Here how I simply do with EPPLUS :在这里,我如何简单地使用 EPPLUS :

using (ExcelPackage xlPackage = new ExcelPackage(new FileInfo(fileName), "password"))
{}

When I tried to upload excel file and it shown the code error for "The file is not an valid Package file. If the file is encrypted, please supply the password in the constructor".当我尝试上传 excel 文件时,它显示“该文件不是有效的包文件。如果文件已加密,请在构造函数中提供密码”的代码错误。

But my excel file was not encrypted and also was not password protected.但是我的excel文件没有加密,也没有密码保护。

I find out a solution by using try catch like I just opened my excel file and then try to save that file from .xls to .xlsx format.我通过使用 try catch 找到了一个解决方案,就像我刚刚打开我的 excel 文件一样,然后尝试将该文件从 .xls 保存为 .xlsx 格式。

After doing this, file upload was successful and EPPlus dll did not give any error and working well!这样做后,文件上传成功,EPPlus dll 没有出现任何错误并且运行良好!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM