Click here to Skip to main content
5,787,682 members and growing! (20,759 online)
Email Password   helpLost your password?
Enterprise Systems » Office Development » Microsoft Excel     Intermediate

Data Grid To Excel Export

By Shahriar Iqbal Chowdhury

An article presenting a small application that export data from datagrid to Excel Spreadsheet
C#, Windows, .NET 1.1, .NET, DBA, Dev

Posted: 19 Apr 2007
Updated: 19 Apr 2007
Views: 23,212
Bookmarked: 14 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
4 votes for this Article.
Popularity: 1.39 Rating: 2.31 out of 5
1 vote, 25.0%
1
2 votes, 50.0%
2
0 votes, 0.0%
3
1 vote, 25.0%
4
0 votes, 0.0%
5

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Shahriar Iqbal Chowdhury


Shahriar is an experienced software developer on .NET platform.He wrote applications as diverse as web and desktop applications to SMS driven applications.


Occupation: Web Developer
Company: Desme Inc
Location: Bangladesh Bangladesh

Other popular Office Development articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 5 of 5 (Total in Forum: 5) (Refresh)FirstPrevNext
GeneralPlease explain this line: private AxOWC10.AxSpreadsheet ExcelControl1;memberWalaza8:50 16 Mar '08  
QuestionMS Excel installed?membermaizhiming13:44 17 Jan '08  
QuestionDataset with multiple tables in separate sheetsmembermaeehma27:25 29 Jun '07  
AnswerRe: Dataset with multiple tables in separate sheetsmemberprivacy space23:26 23 Aug '07  
Questionhimemberehsan_nrw0:35 11 May '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 19 Apr 2007
Editor: Sean Ewington
Copyright 2007 by Shahriar Iqbal Chowdhury
Everything else Copyright © CodeProject, 1999-2009
Web17 | Advertise on the Code Project