can anyone help me with my issue when downloading an excel file using excel interop in c#. My problem is that when I deploy my code in environment I experience this error after I click the download excel button. But in my local machine it is working as expected with no error.
Here is my code:
void View_ExtractJobsCalibrationForm(object sender, EventArgs e)
{
try
{
using (Data.DataContexts.IDataContext objContext = Data.DataContexts.DataContext.CreateDataContext())
{
GridViewRow row = (((ImageButton)((System.Web.UI.WebControls.GridViewCommandEventArgs)(e)).CommandSource).NamingContainer as GridViewRow);
IQueryable<Data.JobSummary> objJobs = objContext.Jobs.GetJobSummaries().Where(j => !j.IsDeleted);
IQueryable<Data.EquipmentSummary> objEquipment = objContext.Equipment.GetEquipmentSummaries();
oXL = new Microsoft.Office.Interop.Excel.Application();
oXL.Visible = true;
oXL.DisplayAlerts = false;
string path = HttpContext.Current.Server.MapPath("/Reports/ExcelTemplate/Calibration_Form_ARS-FORM-CL1_Template.xlsx");
string destPath = System.IO.Path.Combine(System.IO.Path.GetTempPath(), string.Format(System.IO.Path.GetFileNameWithoutExtension(path) + "_{0}_{1}{2}", ((HyperLink)row.Cells[1].Controls[0]).Text, DateTime.UtcNow.ToString("yyyy-MM-dd"), ".xlxs"));
//Create a copy from the Template to save the data.
System.IO.File.Copy(path, destPath, true);
//Open the copied template.
mWorkBook = oXL.Workbooks.Open(destPath, 0, false, 5, "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false);
//Get all the sheets in the workbook
mWorkSheets = mWorkBook.Worksheets;
//Get the sheet to be use
mWSheet = (Microsoft.Office.Interop.Excel.Worksheet)mWorkSheets.get_Item("Pre calibration Check in");
mWorkBook.Save();
mWSheet = null;
mWorkBook = null;
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
}
}
Thanks in advance :)