none
How export a gridview to excel using c#

All replies

  • there are a couple of options, unfortunately for us since its WinForms based, there is apperently an easier way to do it in ASP.NET if we were using ASP.NET

    in order to help better, could I ask - how are you populating data into the datagridview? Are you entering it manually or have you databound it from a dataset, using a DataAdapter to fill the dataset with records?

     

    Wednesday, September 27, 2006 2:01 AM
    Moderator
  • I'm using windows form, i am populating data into the datagridview using datatable
    Wednesday, September 27, 2006 1:26 PM
  • where do you populate your data table from?
    Wednesday, September 27, 2006 3:32 PM
  • there are a couple of ways.

    One would be to add a COM reference to Excel and add rows/items/data into the worksheet by going through each column and each row of the dataTable/datagrid and pretty much inserting it into the worksheet. The problem with this is that you dont know how much data there is so it could take a while.

    The other may be to use OleDb to connect to the spreadsheet and insert data from the dataTable to the spreadsheet. I will try to see what code I can dig up for this

    Wednesday, September 27, 2006 4:14 PM
    Moderator
  • try this: http://programmingshare-thienle.blogspot.com/2011/12/export-data-from-gridview-control-to.html
    Wednesday, December 07, 2011 3:59 PM
  •  private void simpleButtonExportFileDetails_Click(object sender, EventArgs e)
            {
                SaveFileDialog saveDialogBox = new SaveFileDialog();
                saveDialogBox.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal).ToString();
                saveDialogBox.Filter = "CSV File|*.csv";
                saveDialogBox.Title = "Save a CSV File";
                saveDialogBox.ShowDialog();
                if (saveDialogBox.FileName != "")
                {
                    try
                    {
                        gridControlFileDetails.ExportToXls(saveDialogBox.FileName);
                        MessageBox.Show(" File " + saveDialogBox.FileName + "Report Saved SuccessFully", "File's Details", MessageBoxButtons.OK);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
            }
    Software Developer
    Wednesday, December 07, 2011 6:49 PM
  • Hello,

    The following article shares a simple method to export gridview to Excel with C#.

    Maybe it is helpful for you.

    http://www.c-sharpcorner.com/UploadFile/d0cbea/6031/

    Friday, March 09, 2012 5:07 AM
  • Hi,

    you are using a DataTable as DataGridView data source, so you can easily export DataGridView to Excel with this Excel C# library using this C# code:

    // Get DataTable that is data bound to DataGridView.
    var bindingSource = (BindingSource)dataGridView.DataSource;
    var dataView = (DataView)bindingSource.List;
    var dataTable = dataView.Table;
    
    // Create new Excel file.
    var excelFile = new ExcelFile();
    
    // Add new worksheet and insert DataTable into it.
    excelFile.Worksheets.Add(dataTable.TableName).InsertDataTable(dataTable, "A1", true);
    
    // Save Excel file to XLS format.
    excelFile.SaveXls(dataTable.TableName + ".xls");

    Monday, May 14, 2012 8:06 AM