69281

Password protected excel using NPOI

i have a .net c# application in which im downloading one excel file on button click. the code im using is

using NPOI.HSSF.UserModel; using NPOI.HSSF.Util; using NPOI.SS.UserModel;

then some codes.

HSSFWorkbook book = new HSSFWorkbook(); var sheet = book.CreateSheet("StatusReport");

some code for formatting the excel,then some code for downloading the 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();

this will help me to download the excel,but i need to make that excel as a password protected one. please help.

Answer1:

This is not working in 1.2.5 may work in 2.0 Try

var sheet = book.CreateSheet("StatusReport"); sheet.ProtectSheet("Password");

Answer2:

NPOI is a .net clone of the POI-Java-Libary. So I looked at the POI-Documentation for the class "HSSFWorkbook": http://poi.apache.org/apidocs/org/apache/poi/hssf/usermodel/HSSFWorkbook.html As you can see there is a method called "writeProtectWorkbook" that you can use to password protect the workbook. Also take a look at the documentation for the class "HSSFSheet": http://poi.apache.org/apidocs/org/apache/poi/hssf/usermodel/HSSFSheet.html As you can see there is a method called "protectSheet" that you can use to password protect the sheet. I never tried it out . But may it help?

Recommend

  • Render User Controls and call their Page_Load
  • share data to LinkedIn api User’s Profile for status update
  • ASP.NET MVC 2 - Problem with Request encoding
  • MVC JsonNetResult - “dataloss” when serializing List
  • How to filter local requests in asp.net web api?
  • Reference generic url parameter in AuthorizeAttribute
  • Formatting with mako
  • Global session variable in express.js route?
  • Change Divider Color Android DatePicker Dialog
  • angular2 http.post() to local json file
  • How to center a menu using flexbox [closed]
  • Translate animation to correct position in Android
  • Is a .txt file created in VB different than one I'd randomly create?
  • Loop animation drawable
  • Netlink sockets and libnl - nl_recvmsgs_default returning -16 (EBUSY)
  • Gray line inside cardview :)?
  • Express JS Display Data By ID
  • How to resolve permission denied maybe missing internet permission?
  • I am receiving HibernateException “No Hibernate Session bound to thread, and configuration does not
  • MySQL: Update rows in table by iterating and joining with another one
  • Javascript CORS - No 'Access-Control-Allow-Origin' header is present
  • Sending cookie value via httpget but not getting the desired response
  • JBoss External Properties Files in Classpath
  • Access variable of ScriptContext using Nashorn JavaScript Engine (Java 8)
  • How Lists (specifically, RecyclerView with CardViews) in Android work
  • Code in Job's Script Block after Start-Process Does not Execute
  • ViewController With Transparent Background Entering Current ViewController With Push Transition
  • Assign variable to the value in HTML
  • Android - Material Design - NavigationView - How to put vertical scroll?
  • How to use carriage return with multiple line?
  • Jquery UI tool tip close icon
  • DirectX11 ClearRenderTargetViewback with transparent buffer?
  • SSO with signing and signature validation doesn't work
  • Validaiting emails with Net.Mail MailAddress
  • Perl system calls when running as another user using sudo
  • Timeout for blocking function call, i.e., how to stop waiting for user input after X seconds?
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Proper folder structure for lots of source files
  • How does Linux kernel interrupt the application?
  • costura.fody for a dll that references another dll