Click here to Skip to main content
6,630,586 members and growing! (15,607 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, DBA, Dev
Posted:19 Apr 2007
Views:30,878
Bookmarked:19 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
5 votes for this article.
Popularity: 1.86 Rating: 2.67 out of 5
1 vote, 20.0%
1
2 votes, 40.0%
2

3
1 vote, 20.0%
4
1 vote, 20.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


Member
Shahriar Iqbal Chowdhury is a Software Engineer working on Microsoft .NET Technology.He is responsible for developing several web/desktop application build on .NET technology which are running successfully on USA,Germany,Natherlands.His point of interest is Design Pattern.Recently he is working on WPF.
Occupation: Software Developer (Senior)
Company: Desme Inc
Location: Bangladesh Bangladesh

Other popular Office Development articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 5 of 5 (Total in Forum: 5) (Refresh)FirstPrevNext
GeneralPlease explain this line: private AxOWC10.AxSpreadsheet ExcelControl1; PinmemberWalaza8:50 16 Mar '08  
QuestionMS Excel installed? Pinmembermaizhiming13:44 17 Jan '08  
QuestionDataset with multiple tables in separate sheets Pinmembermaeehma27:25 29 Jun '07  
AnswerRe: Dataset with multiple tables in separate sheets Pinmemberprivacy space23:26 23 Aug '07  
Questionhi Pinmemberehsan_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
Web11 | Advertise on the Code Project