Link to home
Start Free TrialLog in
Avatar of bowemc
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.DataGrid . I know the changes I have made change it from what the System.Windows.Forms.DataGrid 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.DataGrid object) to MS Excell (2002)

Hope You can help

BTW I'm using .Net 1.1 to create my winform app

Thanks

ASKER CERTIFIED SOLUTION
Avatar of SystemExpert
SystemExpert
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
see this also

private void ExportLinkButton_Click(object sender, System.EventArgs e)
{
  string strTitle = “put the grid title here”;
  new DataGridExcelExporter(this.YourGridName , this.Page).Export(strTitle);
}
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
Avatar of bowemc
bowemc

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.Workbooks.Add(true);
                  DataTable table = (Dataset)grid;
                  int ColumnIndex=0;
                  foreach(Datacolumn col in table.Columns)
                  {  
                        ColumnIndex++;
                        excel.Cells[1,ColumnIndex]=col.ColumnName;
                  }
                  int rowIndex=0;
                  foreach(DataRow row in table.Row)
                  {        
                        rowIndex++;      
                        ColumnIndex=0;        
                        foreach(DataColumn col in table.Columns)        
                        {  
                              ColumnIndex++;                
                              excel.Cells[rowIndex+1,ColumnIndex]=row.Cells[col.ColumnName].Text;        
                        }
                  }
                  excel.Visible = true;
                  Worksheet worksheet = (Worksheet)excel.ActiveSheet;
                  worksheet.Activate();
Avatar of bowemc

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