[英]How to password protect an excel generated from a byte[]?
I have the data that has to be loaded to an excel file in a byte array and I need to apply password protection on that. 我必须将数据加载到字节数组中的excel文件中,并且需要对此应用密码保护。 I have tried converting byte[] to datatable/list and tried applying password protection using Excelpackage but I am not able to correctly convert data in byte[] array to any form.My file is getting downloaded but with some weired data.
我曾尝试将byte []转换为数据表/列表,并尝试使用Excelpackage应用密码保护,但是我无法正确将byte []数组中的数据转换为任何形式。我的文件正在下载但包含一些奇怪的数据。 Can anyone please share your knowledge?
谁能分享您的知识?
response.Clear();
response.Buffer = true;
response.ContentEncoding = System.Text.Encoding.UTF8;
response.ContentType = mimeType;
response.AddHeader("content-disposition", "attachment;filename="
+ Uri.EscapeDataString(fileName));
response.Charset = "";
response.Cache.SetCacheability(HttpCacheability.NoCache);
DataTable dt;
MemoryStream stream;
using (stream = new MemoryStream(fileBytes))
{
BinaryFormatter bin = new BinaryFormatter();
stream.Seek(0, SeekOrigin.Begin);
dt = (DataTable)formatter.Deserialize(stream);
stream.Close();
}
using (ExcelPackage pack = new ExcelPackage())
{
ExcelWorksheet ws = pack.Workbook.Worksheets.Add("heelo");
ws.Cells["A1"].LoadFromDataTable(dt, true);
pack.Save("123");
var ms = new System.IO.MemoryStream();
pack.SaveAs(ms);
ms.WriteTo(HttpContext.Current.Response.OutputStream);
ms.Close();
}
response.Flush();
response.End();
Having a little trouble following you code. 编码后遇到一些麻烦。 Why is
response
mime type a PDF? 为什么
response
mime键入PDF? I think you would want that to be 我想你会希望成为
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
Does response
actually have anything to do with the MemoryStream
s since I see no reference from one to the other? 因为我看不到彼此之间的引用,所以
response
实际上与MemoryStream
有关系吗?
In any event, if you have the ExcelPackage
available and you want to write to a Stream
with a password, you can just call the overload: 无论如何,如果您有可用的
ExcelPackage
,并且想要使用密码写入Stream
,则可以调用重载:
pack.SaveAs(ms, "MyPassword")
Here is some more info: Password Protected Excel Download using EPPLUS 以下是更多信息: 使用EPPLUS下载受密码保护的Excel
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.