Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
C#
string attachment = "attachment; filename=detail.xls;
Response.ClearContent();
Response.AddHeader("password", "1234");
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/ms-excel";
StringWriter sw = new StringWriter();

HtmlTextWriter htw = new HtmlTextWriter(sw);
HtmlForm frm = new HtmlForm();
DataList1.Parent.Controls.Add(frm);
frm.Attributes["runat"] = "server";
frm.Controls.Add(DataList1);
frm.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();



[Edit member="Tadit"]
Added pre tags.
[/Edit]
Posted
v4
Comments
What is the problem here?
Member 10921356 3-Jul-14 4:42am    
I want that..newly generated excel file will be open by password only... But above code is not working for password protection..

Refer - Re: Export Excel with password protected sheet in asp.net c#[^].
Quote:
The way you are generating excel file is not actully a excel file. it's a html file which is displayed in excel and nothing wrong in it, as it fullfil most of the requriements. But if you want to generate the password protected excel file, then you have to generate the real excel file.

These links might be usefull to you:

Implementing Password Security on Excel Workbooks Using Visual Studio .NET[^]
http://social.msdn.microsoft.com/Forums/en-US/os_specifications/thread/7021768e-4562-43dd-9d5e-89c10970bf33/[^]

or just search internet for any third party paid/free component.
 
Share this answer
 
Comments
Member 10921356 3-Jul-14 7:58am    
Thank U Sir.....
Welcome. :)
 
Share this answer
 
Comments
OP is asking about Excel, not PDF. See my answer. :)

Thanks,
Tadit

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