ADO/ADO.NET - Cannot implicitly convert type 'object' to 'Excel._Worksheet'. An explicit conversion exis

Asked By Bhanuprakash T.R on 20-Jun-12 06:01 AM
hello sir i am trying to convert .sdf file into excel shett. i get following error Cannot implicitly convert type 'object' to 'Excel._Worksheet'. An explicit conversion exists (are you missing a cast?)

              i try this code
  filenamesdf = textBox2.Text.ToString();
        SqlCeConnection con = new SqlCeConnection("Data Source=" + filenamesdf + "");
        SqlCeDataAdapter da = new SqlCeDataAdapter("select * from purchaseOrder", con);
        DataTable dt1 = new DataTable();
        // creating Excel Application
        Excel.ApplicationClass excel = new Excel.ApplicationClass();
        Excel.Workbook workbook = excel.Application.Workbooks.Add(true);
        // creating new Excelsheet in workbook
        Excel._Worksheet worksheet = null;
        // see the excel sheet behind the program
        excel.Visible = false;
        // get the reference of first sheet. By default its name is Sheet1.
        // store its reference to worksheet
        worksheet = workbook.Sheets["Sheet1"];(in this line i get error)
        worksheet = workbook.ActiveSheet;

        // changing the name of active sheet
        worksheet.Name = "Exported from gridview";

        // storing header part in Excel
        for (int i = 1; i < dt1.Columns.Count + 1; i++)
          worksheet.Cells[1, i] = dt1.Columns[i - 1].Caption;
        // storing Each row and column value to excel sheet
        for (int i = 0; i < dt1.Rows.Count - 1; i++)
          for (int j = 0; j < dt1.Columns.Count; j++)
            worksheet.Cells[i + 2, j + 1] = dt1.Rows[i][j].ToString();
        // save the application
        workbook.SaveAs("d:\\output.xls", Type.Missing, Type.Missing, Type.Missing,
   Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive,
   Type.Missing, Type.Missing, Type.Missing, Type.Missing);
        MessageBox.Show("Excel file created , you can find the file d:\\output.xls");

please tell me how to solve this problem
Ravi Maurya replied to Bhanuprakash T.R on 20-Jun-12 06:02 AM
This error mostly throws up when there is no Explicit casting

Excel.Worksheet oWS =oWB.Worksheets[1];
Excel.Worksheet oWS = (Excel.Worksheet)oWB.Worksheets[1];

Bhanuprakash T.R replied to Ravi Maurya on 20-Jun-12 06:10 AM
please take my code change what ever the necessary .please i newer of .net
Ravi Maurya replied to Bhanuprakash T.R on 20-Jun-12 07:07 AM
Refer this article

or else you can easily export your gridview as

HttpContext.Current.Response.ClearContent(); HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment;filename={0}.xls", fileName)); HttpContext.Current.Response.ContentType = "application/excel"; System.IO.StringWriter sw = new System.IO.StringWriter(); HtmlTextWriter htw = new HtmlTextWriter(sw); gv.RenderControl(htw); HttpContext.Current.Response.Write(sw.ToString()); HttpContext.Current.Response.End(); where gv is the name of your gridview