[英]How to password protect an excel generated from a byte[]?
我必須將數據加載到字節數組中的excel文件中,並且需要對此應用密碼保護。 我曾嘗試將byte []轉換為數據表/列表,並嘗試使用Excelpackage應用密碼保護,但是我無法正確將byte []數組中的數據轉換為任何形式。我的文件正在下載但包含一些奇怪的數據。 誰能分享您的知識?
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();
編碼后遇到一些麻煩。 為什么response
mime鍵入PDF? 我想你會希望成為
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
因為我看不到彼此之間的引用,所以response
實際上與MemoryStream
有關系嗎?
無論如何,如果您有可用的ExcelPackage
,並且想要使用密碼寫入Stream
,則可以調用重載:
pack.SaveAs(ms, "MyPassword")
以下是更多信息: 使用EPPLUS下載受密碼保護的Excel
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.