Click here to Skip to main content
15,867,488 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Dear All,

I want to export GridView/Repeater to password protected excel file.

Currently i'm doing export without password.

What I have tried:

C#
Response.ClearContent();
Response.AddHeader("content-disposition", "attachment;filename=CandidateList.xls");
Response.AddHeader("Content-Type", "application/vnd.ms-excel");
Response.ContentEncoding = Encoding.Unicode;
Response.BinaryWrite(Encoding.Unicode.GetPreamble());

using (StringWriter sw = new StringWriter())
{
    using (HtmlTextWriter htw = new HtmlTextWriter(sw))
    {
        GridView grid = new GridView();
        grid.DataSource = exportcandidateList;
        grid.DataBind();
        string headerTable = @"<Table><tr><td colspan='12'><center>Candidates - " + ddlCenter.SelectedItem.ToString().ToUpper() + " </center></td></tr></Table>";
        Response.Write(headerTable);
        grid.RenderControl(htw);
        Response.Write(sw.ToString());
    }
}

Response.End();


Can we export password protected excel using crystal report (ASP.NET C#)?


Can anyone please help me, how can i add password protect to the excel file while exporting.


Thanks
Posted
Updated 5-Nov-17 6:21am
Comments
Mehdi Gholam 5-Nov-17 8:37am    
You are not export in Excel file format, but are exporting in a text format which does not support file passwords.
abdul subhan mohammed 5-Nov-17 9:02am    
Yes, I know. Do you know how can I export to password protected excel.

Use a library for this purpose like : NuGet Gallery | EPPlus 4.1.1[^]
 
Share this answer
 
Comments
Member 13844328 11-Jul-18 9:02am    
can we protect the excel file using password from gridview ??
Mehdi Gholam 11-Jul-18 12:00pm    
Yes, read the library documentation.
Can we export password protected excel using crystal report (ASP.NET C#)?

No

You will have to use Interop [^] or any third party library to protect the workbook.
i would suggest GitHub - ClosedXML/ClosedXML: [^] to customise the excel the way you wanted.

refer these to apply password to the file and then you can export it.
. Create password protected excel file using Closedxml library [^]
Sheet Protection · ClosedXML/ClosedXML Wiki · GitHub[^]
 
Share this answer
 
Comments
abdul subhan mohammed 6-Nov-17 2:25am    
Thanks karthik.
i have given ws.Protect("123"); but this one is only not allowing to edit/insert new values.

I want to protect whole excel file. Whenever user open the excel file, it should ask password. But in my case it is not asking the password.

Even i tried wb.protect("123"); this code is also not asking the password.

Could you please help me.

Thanks once again.
Karthik_Mahalingam 6-Nov-17 2:28am    
post the code.
Karthik_Mahalingam 6-Nov-17 2:44am    
try using EPPlus[^]

 string path =@"D:\Projects\CP\CP\bin\Debug\aa.xlsx";
            FileInfo newFile = new FileInfo(path);
            DataTable dt = new DataTable();
            dt.Columns.Add("Column 1");
            dt.Rows.Add("data1");
            using (ExcelPackage package = new ExcelPackage(newFile))
            {
                ExcelWorksheet ws = package.Workbook.Worksheets.Add("demo");
                ws.Cells["A1"].LoadFromDataTable(dt, false);
                package.Save("YourPassword");
            }
abdul subhan mohammed 6-Nov-17 5:01am    
THANK YOU V.MUCH
Karthik_Mahalingam 6-Nov-17 5:04am    
working?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900