protected void Btn_Xls_Click(object sender, ImageClickEventArgs e)
{
string attachment = "attachment; filename=MonthlyAttendanceReport.xls";
string sheetProtectPassword = "123";
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
// Create a form to contain the grid
HtmlForm frm = new HtmlForm();
rptOrder.Parent.Controls.Add(frm);
frm.Attributes["runat"] = "server";
frm.Controls.Add(rptOrder);
frm.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
//Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
// Microsoft.Office.Interop.Excel.Sheets objSheets;
// Microsoft.Office.Interop.Excel._Worksheet objSheet;
// string sheetProtectPassword = "123";
// string workbookPath = "Path where excel file is stored";
// Microsoft.Office.Interop.Excel.Workbook m_objBook = excelApp.Workbooks.Open(workbookPath, 0, false, 5, "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false);
// objSheets = m_objBook.Worksheets;
// objSheet = (Excel._Worksheet)objSheets.get_Item(1);
// objSheet.Protect(sheetProtectPassword, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true);
}
Asked
Active
Viewed 898 times
0

gengisdave
- 1,969
- 5
- 21
- 22

sachin
- 1
- 2
-
i have done export excel from gridview , but want to add password also in excel on export time...Help me – sachin Sep 09 '15 at 08:30
-
possible duplicate of [Excel File Password Protection with Open XML SDK](http://stackoverflow.com/questions/15168011/excel-file-password-protection-with-open-xml-sdk) – Ondrej Svejdar Sep 09 '15 at 09:05