Click here to Skip to main content
11,629,614 members (55,957 online)
Click here to Skip to main content

Data Grid To Excel Export

, 19 Apr 2007 CPOL 60.8K 4.3K 29
Rate this:
Please Sign up or sign in to vote.
An article presenting a small application that export data from datagrid to Excel Spreadsheet

Screenshot - Main_Form.jpg

Introduction

This article describes a simple way to export the data within a DataGrid to an Excel Sheet. This approach is adequate to export data from any DataGrid, of any number of columns.

This approach is very useful because in a professional programming environment, one is often required to export the data of a DataGrid into an Excel sheet. This article need MS SQL Server 2000 for the data source (from where the DataGrid will populate). For anyone who wishes to use any database or any datsource to populate the DataGrid, this is also possible.

Using the code

To use this code, you need to add the database files to MS SQL Server 2000 attached with this article. The top of the main Form Connection String of SQL Server 2000 is provided. Depending on the server, you may have to change the server name, user name, or password.

Screenshot - Main_Form.jpg

The form above first populates the DataGrid from database IHRMS, and table EmpPersonal and then sets the Export source to the DataGrid source and calls an export function.

Screenshot - Export.jpg

The function behind the export:

public void executeExport()
{
    string col1="";
    string table_no=type;
    col1="ExportedRow";
    System.Data.DataRowCollection dr=ds.Tables[table_no].Rows;
    int cols=ds.Tables[table_no].Columns.Count; 
    ExcelControl1.Cells[1,1]=col1;
    for(int i=0;i<cols;i++)
    {
        col1=ds.Tables[table_no].Columns[i].ColumnName ; 
        ExcelControl1.Cells[2,i+1]=col1; 
    }

    int num=dr.Count; 
    for(int i=0;i<num; i++)
    {
        object[] array=dr[i].ItemArray ;
        int j;
        for(j=0;j<array.Length;j++)
        {
            col1=array[j].ToString();
            ExcelControl1.Cells[i+3,j+1]=col1; 

        }

    }

}

History

This is version 1.0

License

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

Share

About the Author

Shahriar Iqbal Chowdhury/Galib
Chief Technology Officer
Bangladesh Bangladesh
I am a Software Engineer and Microsoft .NET technology enthusiast. Professionally I worked on several business domains and on diverse platforms. I love to learn and share new .net technology and my experience I gather in my engineering career. You can find me from here

Personal Site
Personal Blog
FB MS enthusiasts group
About Me

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 1 Pin
Vlada Milicevic3-Apr-13 23:13
memberVlada Milicevic3-Apr-13 23:13 
GeneralPlease explain this line: private AxOWC10.AxSpreadsheet ExcelControl1; Pin
Walaza16-Mar-08 7:50
memberWalaza16-Mar-08 7:50 
GeneralRe: Please explain this line: private AxOWC10.AxSpreadsheet ExcelControl1; Pin
larno8-May-10 5:20
memberlarno8-May-10 5:20 
QuestionMS Excel installed? Pin
maizhiming17-Jan-08 12:44
membermaizhiming17-Jan-08 12:44 
Does this code require MS Excel to be installed? If so what is the earliest version of MS Excel needed?
QuestionDataset with multiple tables in separate sheets Pin
maeehma229-Jun-07 6:25
membermaeehma229-Jun-07 6:25 
AnswerRe: Dataset with multiple tables in separate sheets Pin
privacy space23-Aug-07 22:26
memberprivacy space23-Aug-07 22:26 
Questionhi Pin
ehsan_nrw10-May-07 23:35
memberehsan_nrw10-May-07 23:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150723.1 | Last Updated 19 Apr 2007
Article Copyright 2007 by Shahriar Iqbal Chowdhury/Galib
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid