Click here to Skip to main content
Licence 
First Posted 19 Apr 2007
Views 45,074
Downloads 1,973
Bookmarked 29 times

Data Grid To Excel Export

By | 19 Apr 2007 | Article
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 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

Technical Lead

Bangladesh Bangladesh

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,Netherlands.His point of interest is Design Pattern.
 
web: http://www.imgalib.com

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralPlease explain this line: private AxOWC10.AxSpreadsheet ExcelControl1; PinmemberWalaza7:50 16 Mar '08  
GeneralRe: Please explain this line: private AxOWC10.AxSpreadsheet ExcelControl1; Pinmemberlarno5:20 8 May '10  
QuestionMS Excel installed? Pinmembermaizhiming12:44 17 Jan '08  
QuestionDataset with multiple tables in separate sheets Pinmembermaeehma26:25 29 Jun '07  
Hi,
 
I tested your code and it worked fine but I was wondering if I have a Dataset with multiple tables and I want to export each table to a separate sheet ,is it possible and how ?
appreciate you feedback
 
Mohamed Hamed
AnswerRe: Dataset with multiple tables in separate sheets Pinmemberprivacy space22:26 23 Aug '07  
Questionhi Pinmemberehsan_nrw23:35 10 May '07  

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.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120529.1 | Last Updated 19 Apr 2007
Article Copyright 2007 by Shahriar Iqbal Chowdhury
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid