C# .NET - datas export to excel from datagridview

Asked By Chinnarasu .M on 20-Oct-12 08:30 AM
 hi, i am working now windows application , i need to datagridview datas export to excel but i am set checkbox in the datagridview . that checkbox rows to checked is true that rows only export to excel. i am use below code headertext is not export how to use please help me very urgent...

 Microsoft.Office.Interop.Excel.ApplicationClass excel = new Microsoft.Office.Interop.Excel.ApplicationClass();
            excel.Application.Workbooks.Add(Type.Missing);
            excel.Columns.ColumnWidth = 20;

            string billno = "";                          
            int i = 0;
            int j = 0;
            int s = 0;
           
            for (i = 0; i <= dataGridView1.RowCount - 1; i++)
            {
              if (Convert.ToBoolean(dataGridView1.Rows[i].Cells[0].Value) == true)
              {   
                for (j = 0; j <= dataGridView1.ColumnCount - 1; j++)
                {                  
                  DataGridViewCell cell = dataGridView1[j, i];                  
                  excel.Cells[i + 1, j + 1] = cell.Value;
                }   
              }
            }
            
            // excel.Visible = true;
            Worksheet worksheet = (Worksheet)excel.ActiveSheet;
            worksheet.Activate();

            string ss = System.DateTime.Now.ToString("dd MM yyyy-hh mm");
            string fileName = selectPath + String.Format("\\Excel-{0}.xls", ss);
            excel.ActiveWorkbook.SaveCopyAs(fileName);
            excel.ActiveWorkbook.Saved = true;
            excel.Quit();

Danasegarane Arunachalam replied to Chinnarasu .M on 20-Oct-12 10:20 PM
Instead going for Excel Automation, go for the OLEDB.net. The Excel Automation requires excel to be installed on the client computer where OLEDB.net requires only drivers.

Here are few examples

http://blueraiden.exofire.net/blog/import-excel-spreadsheet-data-to-datagridview-using-vb-net

http://csharp.net-informations.com/datagridview/csharp-datagridview-load-excel.htm

Go for the First One
White Ashlin replied to Danasegarane Arunachalam on 22-Oct-12 03:23 AM
You can see this article, it uses a dataexport component and use 9 solutions to export data to excel with C#, including datagridview, please chack, this article will helpful to you:
http://www.codeproject.com/Articles/164582/8-Solutions-to-Export-Data-to-Excel-for-ASP-NET