使用NPOI密码保护excel
我有一个.NET的C#应用程序,其中即时下载button点击一个Excel文件。 我使用的代码是
using NPOI.HSSF.UserModel; using NPOI.HSSF.Util; using NPOI.SS.UserModel;
那么一些代码。
HSSFWorkbook book = new HSSFWorkbook(); var sheet = book.CreateSheet("StatusReport");
一些用于格式化excel的代码,然后是用于下载excel的一些代码。
HttpContext.Current.Response.Clear(); HttpContext.Current.Response.Charset = "utf-16"; HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("windows-1250"); HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "MpiDischargeReport.xls")); HttpContext.Current.Response.ContentType = "application/ms-excel"; book.Write(HttpContext.Current.Response.OutputStream); HttpContext.Current.ApplicationInstance.CompleteRequest();
这将帮助我下载excel,但是我需要把这个excel作为一个密码保护的。 请帮忙。
这在1.2.5中不起作用可能在2.0中工作尝试
var sheet = book.CreateSheet("StatusReport"); sheet.ProtectSheet("Password");
NPOI是POI-Java-Libary的一个.net克隆。 所以我查看了“HSSFWorkbook”类的POI文档:
http://poi.apache.org/apidocs/org/apache/poi/hssf/usermodel/HSSFWorkbook.html
正如您所看到的,有一种名为“writeProtectWorkbook”的方法,您可以使用它来密码保护工作簿。
另外请看“HSSFSheet”类的文档:
http://poi.apache.org/apidocs/org/apache/poi/hssf/usermodel/HSSFSheet.html
正如您所看到的,有一种名为“protectSheet”的方法,您可以使用它来密码保护表单。
我从来没有尝试过。 但可能有帮助吗?