![]() |
Enterprise Systems »
Office Development »
Microsoft Excel
Intermediate
Data Grid To Excel ExportBy Shahriar Iqbal ChowdhuryAn article presenting a small application that export data from datagrid to Excel Spreadsheet |
C#, Windows, .NET 1.1, DBA, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
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.
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.
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.
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;
}
}
}
This is version 1.0
| You must Sign In to use this message board. | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
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 |