Click here to Skip to main content
15,894,106 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more: , +
Hi, I am trying to export to excel from sql server database using Microsoft.Office.Interop.Excel;

But I am getting an error "microsoft.office.interop.excel.application does not contain a definition for 'workbooks' and no extension method 'workbooks' "

I have added all the required references too. Please let me know how can I solve the issue.

P.S- also getting same error for SaveAs.

C#
void ExportToExcel(string sqlquery, string filename)
        {
            Cursor.Current = Cursors.WaitCursor;
            SqlConnection cnn;
            string connectionString = null;
            string sql = null;
            string data = null;
            int i = 0;
            int j = 0;

            Excel.Application xlApp;
            Excel.Workbook xlWorkBook;
            Excel.Worksheet xlWorkSheet;
            object misValue = System.Reflection.Missing.Value;

            xlApp = new Excel.ApplicationClass();
            xlWorkBook = xlApp.Workbooks.Add(misValue);
            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

            connectionString = "Data Source=HULMOSSPROD;Initial Catalog=" + cbx_Settings.Text + ";User Id=mosssa;Password=Unilever123;";
            cnn = new SqlConnection(connectionString);
            cnn.Open();
            sql = sqlquery;
            SqlDataAdapter dscmd = new SqlDataAdapter(sql, cnn);
            DataSet ds = new DataSet();
            dscmd.Fill(ds);


            //==============================================================================================================
            string strLine;

            SqlCommand cmd = new SqlCommand(sqlquery, cnn);
            SqlDataReader dr;
            dr = cmd.ExecuteReader();

            //Initialize the string that is used to build the file.
            strLine = "";

            //Enumerate the field names and the records that are used to build 
            //the file.
            for (int m = 0; m < 1; m++)
            {
                for (int k = 0; k <= dr.FieldCount - 1; k++)
                {

                    strLine = dr.GetName(k).ToString();
                    //xlWorkSheet.Cells[m + 1, k + 1] = strLine;

                }
            }
            //==============================================================================================================


            for (i = 1; i <= ds.Tables[0].Rows.Count; i++)
            {
                for (j = 0; j <= ds.Tables[0].Columns.Count - 1; j++)
                {
                    data = ds.Tables[0].Rows[i - 1].ItemArray[j].ToString();
                    //xlWorkSheet.Cells[i + 1, j + 1] = data;
                }
            }



            xlWorkBook.SaveAs("C:\\80IB_Reports\\" + filename, Excel.XlFileFormat.xlWorkbookDefault, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
            xlWorkBook.Close(true, misValue, misValue);
            xlApp.Quit();
            cnn.Close();

            releaseObject(xlWorkSheet);
            releaseObject(xlWorkBook);
            releaseObject(xlApp);
            Cursor.Current = Cursors.Default;

            MessageBox.Show("Export Successful. You can find the file at C:\\80IB_Reports");

        }
Posted
Updated 6-Jul-15 21:29pm
v3

Why interop? This might not be the best way. How about using Microsoft Open XML SDK instead? Please see this CodeProject article: Creating basic Excel workbook with Open XML[^].

See also:
http://support.microsoft.com/default.aspx?scid=kb;EN-US;q257757#kb2[^],
http://support.microsoft.com/kb/257757/en-us[^],
Microsot office Interop[^].

—SA
 
Share this answer
 
It is very strange as this line
C#
xlApp = new Excel.ApplicationClass();

should give you a 'There is no constructor defined for Microsoft.Office.Interop.Excel.ApplicationClass type.' error...
You are defined xlApp as Excel.Application but try to create it as ApplicationClass...change the line to
C#
xlApp = new Excel.Application();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900