Click here to Skip to main content
12,404,395 members (74,616 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# C#4.0 MS-Excel
hi anybody know,

how to import gridview data to excel in asp.net?
Posted 7-Nov-12 19:04pm
velmahesh1.6K
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

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160721.1 | Last Updated 27 May 2015
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100