To export Gridview data to Excel using devexpress XtraGrid have a look at the sample below.


C# Sample

using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Columns;

namespace de.Fesslersoft.ExportXtraGridToExcel
    public partial class ExportForm : Form
        private DataTable _dataTable;

        public ExportForm()
        private void ExportForm_Load(object sender, EventArgs e)
            gridViewExport.OptionsBehavior.AutoPopulateColumns = true;
            gridViewExport.OptionsBehavior.ReadOnly = true;
            gridViewExport.OptionsBehavior.Editable = false;
            gridControlExport.DataSource = _dataTable;

        private void FillDataTable()
            SqlConnectionStringBuilder connectionStringBuilder = new SqlConnectionStringBuilder
                DataSource = "localhost",
                IntegratedSecurity = true,
                InitialCatalog = "Northwind"
            using (SqlConnection connection = new SqlConnection(connectionStringBuilder.ToString()))
                string queryCommand = "SELECT TOP 100 [ProductID],[ProductName],[SupplierID],[CategoryID],[QuantityPerUnit],[UnitPrice],[UnitsInStock],[UnitsOnOrder],[ReorderLevel],[Discontinued] FROM [Northwind].[dbo].[Products]";
                using (SqlDataAdapter adapter = new SqlDataAdapter(queryCommand, connection))
                    _dataTable = new DataTable();

        private void ExportToExcel()
            //storing current layout
            gridViewExport.SaveLayoutToXml(@"C:\Users\mf\Desktop\Neuer Ordner\tempLayout.xml");
            foreach (GridColumn column in gridViewExport.Columns)
                //make all export columns visible
                column.Visible = true;
            gridViewExport.ExportToXls(@"C:\Users\mf\Desktop\Neuer Ordner\export.xls");
            //restoring the layout, the layout file needs to be deleted manually
            gridViewExport.RestoreLayoutFromXml(@"C:\Users\mf\Desktop\Neuer Ordner\tempLayout.xml");

        private void buttonExportToExcel_Click(object sender, EventArgs e)
            XtraMessageBox.Show("Export successfull!", this.Text, MessageBoxButtons.OK,MessageBoxIcon.Information,MessageBoxDefaultButton.Button1);

VB.NET Sample

Imports System.Data.SqlClient
Imports DevExpress.XtraEditors
Imports DevExpress.XtraGrid.Columns

Public Partial Class ExportForm
    Inherits Form
    Private _dataTable As DataTable

    Public Sub New()
    End Sub

    Private Sub ExportForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        gridViewExport.OptionsBehavior.AutoPopulateColumns = True
        gridViewExport.OptionsBehavior.[ReadOnly] = True
        gridViewExport.OptionsBehavior.Editable = False
        gridControlExport.DataSource = _dataTable
    End Sub

    Private Sub FillDataTable()
        Dim connectionStringBuilder As New SqlConnectionStringBuilder() With { _
                .DataSource = "localhost", _
                .IntegratedSecurity = True, _
                .InitialCatalog = "Northwind" _
        Using connection As New SqlConnection(connectionStringBuilder.ToString())
            Dim queryCommand As String = "SELECT TOP 100 [ProductID],[ProductName],[SupplierID],[CategoryID],[QuantityPerUnit],[UnitPrice],[UnitsInStock],[UnitsOnOrder],[ReorderLevel],[Discontinued] FROM [Northwind].[dbo].[Products]"
            Using adapter As New SqlDataAdapter(queryCommand, connection)
                _dataTable = New DataTable()
            End Using
        End Using
    End Sub

    Private Sub ExportToExcel()
        'storing current layout
        gridViewExport.SaveLayoutToXml("C:\Users\mf\Desktop\Neuer Ordner\tempLayout.xml")
        For Each column As GridColumn In gridViewExport.Columns
            'make all export columns visible
            column.Visible = True
        gridViewExport.ExportToXls("C:\Users\mf\Desktop\Neuer Ordner\export.xls")
        'restoring the layout, the layout file needs to be deleted manually
        gridViewExport.RestoreLayoutFromXml("C:\Users\mf\Desktop\Neuer Ordner\tempLayout.xml")
    End Sub

    Private Sub buttonExportToExcel_Click(sender As Object, e As EventArgs) Handles SimpleButton1.Click
        XtraMessageBox.Show("Export successfull!", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1)
    End Sub
End Class

If you have any questions or suggestions feel free to rate this snippet, post a comment or Contact Us via Email.

Related links:

5 thought on “How to export Gridview data to Excel using devexpress XtraGrid”

Leave a Reply