bowemc
asked on
Transfer System.Windows.Forms.DataGrid contents to MS Excel
Hi,
I have created my own sligtly customised grid. It inherits from System.Windows.Forms.DataG rid . I know the changes I have made change it from what the System.Windows.Forms.DataG rid but my changes are nto that dastic.
However, the key part to this question is - how can I easily and efficiently tranfer the contents of all my grid (assue if you like that it is a System.Windows.Forms.DataG rid object) to MS Excell (2002)
Hope You can help
BTW I'm using .Net 1.1 to create my winform app
Thanks
I have created my own sligtly customised grid. It inherits from System.Windows.Forms.DataG
However, the key part to this question is - how can I easily and efficiently tranfer the contents of all my grid (assue if you like that it is a System.Windows.Forms.DataG
Hope You can help
BTW I'm using .Net 1.1 to create my winform app
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi
I'm trying to use the below code from http://www.codeproject.com/office/export.asp
to export .
What reference do i need to add to use the excel stuff?
THANKS
Excel.ApplicationClass excel = new ApplicationClass();
excel.Application.Workbook s.Add(true );
DataTable table = (Dataset)grid;
int ColumnIndex=0;
foreach(Datacolumn col in table.Columns)
{
ColumnIndex++;
excel.Cells[1,ColumnIndex] =col.Colum nName;
}
int rowIndex=0;
foreach(DataRow row in table.Row)
{
rowIndex++;
ColumnIndex=0;
foreach(DataColumn col in table.Columns)
{
ColumnIndex++;
excel.Cells[rowIndex+1,Col umnIndex]= row.Cells[ col.Column Name].Text ;
}
}
excel.Visible = true;
Worksheet worksheet = (Worksheet)excel.ActiveShe et;
worksheet.Activate();
I'm trying to use the below code from http://www.codeproject.com/office/export.asp
to export .
What reference do i need to add to use the excel stuff?
THANKS
Excel.ApplicationClass excel = new ApplicationClass();
excel.Application.Workbook
DataTable table = (Dataset)grid;
int ColumnIndex=0;
foreach(Datacolumn col in table.Columns)
{
ColumnIndex++;
excel.Cells[1,ColumnIndex]
}
int rowIndex=0;
foreach(DataRow row in table.Row)
{
rowIndex++;
ColumnIndex=0;
foreach(DataColumn col in table.Columns)
{
ColumnIndex++;
excel.Cells[rowIndex+1,Col
}
}
excel.Visible = true;
Worksheet worksheet = (Worksheet)excel.ActiveShe
worksheet.Activate();
ASKER
Also - I've noticed my dataset has more information than what my grid displays. THe grid is configurable so I only show my user what they want. Hense I only want to export what is in the actual grid compared to what is in the DataSet.
THANKS
THANKS
private void ExportLinkButton_Click(obj
{
string strTitle = “put the grid title here”;
new DataGridExcelExporter(this
}
where,
strTitle is the title of the DataGrid.
DataGridExcelExporter is the name of main class.
this.YourGridName is the DataGrid object.
this.Page is the current Page object.
ref link : http://www.codeproject.com/csharp/export2excel.asp