Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi anybody know,

how to import gridview data to excel in asp.net?
Posted
Comments
saud_a_k 8-Nov-12 1:07am    
Dude, GOOGLE it
http://www.codeproject.com/Articles/21341/Import-and-Export-Data-from-GridView

1 solution

C#
DataTable DataTabledt = new DataTable("Employee");
       Microsoft.Office.Interop.Excel.Application excel = null;
       Microsoft.Office.Interop.Excel.Workbook workbook1 = null;
       object missing = Type.Missing;
       Microsoft.Office.Interop.Excel.Worksheet worksheet1 = null;
       Microsoft.Office.Interop.Excel.Range range = null;
private void cmdExport_Click(object sender, RoutedEventArgs e)
     {
         string p = "C:\\Documents and Settings\\divyar\\My Documents\\Visual Studio 2010\\Projects\\prjCTM\\WpfApplication1\\Excel\\ex.xlsx";
             //System.Reflection.Assembly.GetExecutingAssembly().Location;
             //System.IO.Directory.GetCurrentDirectory();
         try
         {
             excel = new Microsoft.Office.Interop.Excel.Application();
             workbook1 = excel.Workbooks.Add();
             worksheet1 = (Microsoft.Office.Interop.Excel.Worksheet)workbook1.ActiveSheet;
             for (int index = 0; index < DataTabledt.Columns.Count; index++)
             {
                 worksheet1.Range["A1"].Offset[0, index].Value = DataTabledt.Columns[index].ColumnName;
             }
             for (int index = 0; index < DataTabledt.Rows.Count; index++)
             {
                 worksheet1.Range["A2"].Offset[index].Resize[1, DataTabledt.Columns.Count].Value = DataTabledt.Rows[index].ItemArray;
             }
             workbook1.Activate();
             workbook1.SaveAs(p);
             excel.Quit();
             MessageBox.Show(p);
         }
         catch (Exception ex)
         {
             MessageBox.Show("Error" + ex.ToString());
         }


     }


Thats it
 
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