0

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.

enter image description here

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 :)

mjwills
  • 23,389
  • 6
  • 40
  • 63
g0y
  • 73
  • 1
  • 14
  • Read [ask], try searching before asking a new question. – CodeCaster Oct 19 '17 at 11:07
  • CodeCaster this is not exact duplicate. Read error first then then mark as duplicate. – MartinS Oct 19 '17 at 11:11
  • g0y: Try to reinstall office on server if you can. It should register interop libraries again. We had this error too and it helped. – MartinS Oct 19 '17 at 11:14
  • @Martin errors should be posted as text, not as screenshots. – CodeCaster Oct 19 '17 at 11:52
  • Sounds like there is maybe a different version of Excel installed than the version where you added the interop assembly reference to your project. – blins Oct 19 '17 at 12:42
  • Hi, @CodeCaster. I don't think there's something wrong with what I did on the error message. it's still display the same error I'm encountering. As MartinŠevic advise they too experience this error. Thanks :) – g0y Oct 20 '17 at 01:13
  • Yes, there's something wrong with that, read [Meta: Discourage screenshots of code and/or errors](https://meta.stackoverflow.com/questions/303812/discourage-screenshots-of-code-and-or-errors). – CodeCaster Oct 20 '17 at 07:15

0 Answers0