title: How to create Excel file in asp.net
body: <p>I want to create Excel Sheet in asp.net. I have already done code for this and it is working fine in local system but when I am testing it on server then it gives the following error.</p>
  
  <p><strong>Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 80070005.</strong></p>
  
  <p>My code is.</p>
  
  <pre><code>private static System.Data.DataTable GetDataSet()
      {
          System.Data.DataTable dt = new System.Data.DataTable("Table");
          dt.Columns.Add("Name", Type.GetType("System.String"));
          dt.Columns.Add("Address", Type.GetType("System.String"));
          dt.Columns.Add("Phone", Type.GetType("System.String"));
  
          DataRow dr = dt.NewRow();
          dr["Name"] = "Balaji Selvarajan";
          dr["Address"] = "Reddiyur";
          dr["Phone"] = "000-000-0000";
          dt.Rows.Add(dr);
  
          dr = dt.NewRow();
          dr["Name"] = "Balaji S";
          dr["Address"] = "Kattumannar Koil";
          dr["Phone"] = "000-000-0000";
          dt.Rows.Add(dr);
          return dt;
      }
  
  
      private static void DataSetToExcel(System.Data.DataTable dt, Boolean generateIdentity)
      {
          try
          {
              string Filename = HttpContext.Current.Server.MapPath("~/Doc/") + "Test.xls";
              string imagepath1 = HttpContext.Current.Server.MapPath("~/Doc/") + "Test.xls";
              FileInfo fi = new FileInfo(HttpContext.Current.Server.MapPath("~/Doc/") + "Test.xls");
              if (fi.Exists)
              {
                  fi.Delete();
              }
              Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
              xlApp.Visible = false;
              Microsoft.Office.Interop.Excel.Workbook wb = xlApp.Workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
              object misValue = System.Reflection.Missing.Value;
              Microsoft.Office.Interop.Excel.Worksheet ws = (Microsoft.Office.Interop.Excel.Worksheet)wb.Worksheets.Add(misValue, misValue, misValue, misValue);
              ws.Name = "Test";
  
              for (int i = 0; i &lt; dt.Rows.Count; i++)
              {
                  for (int j = 0; j &lt; dt.Columns.Count; j++)
                  {
                      if (i == 0)
                          ws.Cells[1, j + 1] = dt.Columns[j].ColumnName;
                      ws.Cells[i + 2, j + 1] = dt.Rows[i][j].ToString();
                  }
                  ws.Protect("1235", true, true, true, true, true, true, true, true, true, true, true, true, true, true, true);
              }
              wb.Protect("my", true, true);
              wb.Password = "1235";
              wb.SaveAs(Filename, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, "1235", misValue, misValue, misValue, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
              wb.Close(true, misValue, misValue);
              xlApp.Visible = true;
              xlApp.Quit();
              System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);
              HttpContext.Current.Response.ContentType = "Application/Excel";
              string FilePath = imagepath1;
              HttpContext.Current.Response.WriteFile(FilePath);
              HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=Test.xls");
              HttpContext.Current.Response.End();
          }
          catch (Exception ex)
          {
              throw ex;
          }
      }
      protected void BtnDn_Click(object sender, EventArgs e)
      {
          DataSetToExcel(GetDataSet(), false);
      }
  </code></pre>
  
  <p>I am not able to solve this problem please help me. and give me solution as soon as possible please.</p>
  
  <p>I am using Asp.net C#. </p>