Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i wanna download datagridview records in excel sheet...
i have used belowing code...but i got error in that save method()

Pls any help me......

Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application(); 
            // creating Excel Application
            Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing);

            //Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application();


            Microsoft.Office.Interop.Excel.Worksheet objSheet = new Microsoft.Office.Interop.Excel.Worksheet();
            Microsoft.Office.Interop.Excel.Workbook objWorkBook = null;


            //// creating new WorkBook within Excel application

            //Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing);

 // see the excel sheet behind the program

            app.Visible = true;



            // get the reference of first sheet. By default its name is Sheet1.

            // store its reference to worksheet

            objSheet = objWorkBook.Sheets["Sheet1"] as Microsoft.Office.Interop.Excel.Worksheet;

            objSheet = objWorkBook.ActiveSheet as Microsoft.Office.Interop.Excel.Worksheet;



            // changing the name of active sheet

            objSheet.Name = "Exported from gridview";

 // storing header part in Excel

            for (int i = 1; i < dataGridView1.Columns.Count + 1; i++)
            {

                objSheet.Cells[1, i] = dataGridView1.Columns[i - 1].HeaderText;

            }


 // storing Each row and column value to excel sheet

            for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
            {

                for (int j = 0; j < dataGridView1.Columns.Count; j++)
                {

                    objSheet.Cells[i + 2, j + 1] = dataGridView1.Rows[i].Cells[j].Value.ToString();

                }

            }

// save the application

            workbook.SaveAs("c:\\output.xls", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing) ;
Posted
Updated 1-Dec-12 4:42am
v2
Comments
k@ran 1-Dec-12 10:48am    
Its simple ..

the original saveAs fun has not 11 argument to accept and u r trying to pass 11 argument..

1 solution

The documentation states it takes 12 arguments while you are passing 'only' 11), see MSDN[^].
 
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