protected void btnExcelDownload_Click(object sender, EventArgs e) { try { excelApp = new Excel.ApplicationClass(); // Creates a new Excel Application excelApp.Visible = false; // Makes Excel visible to the user. // The following code opens an existing workbook string workbookPath =Server.MapPath("~/Format/RiTE_Template.xlsm"); //string workbookPath = Server.MapPath(Request.ApplicationPath) + @"\Format\RiTE_Template.xlsm"; // Add your own path here excelWorkbook = excelApp.Workbooks.Open(workbookPath, 0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false); // The following gets the Worksheets collection excelSheets = excelWorkbook.Worksheets; if () { //here I am filling the excel worksheets } string filename = "Rite File" + "_" + SessionVarriables.BidIdSession + "_" + SessionVarriables.RevisionNumberSession; excelWorkbook.SaveAs(Server.MapPath("~/RiteFile/") + filename + ".xlsm"); string path = Server.MapPath("~/RiteFile/") + filename + ".xlsm"; excelApp.DisplayAlerts = false; excelApp.UserControl = false; excelWorkbook.Close(); excelApp.Workbooks.Close(); excelApp.Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject(excelWorkbook); System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp); excelWorkbook = null; excelApp = null; System.Diagnostics.Process.Start(path); } } catch (Exception ex) { excelApp.DisplayAlerts = false; excelApp.UserControl = false; excelApp.Workbooks.Close(); excelApp.Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp); excelApp = null; }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)