Is there a way to export a datagrid to an Excel or html format file?  
Author Message
HappyTomato





PostPosted: Windows Forms General, Is there a way to export a datagrid to an Excel or html format file? Top

Hi all.. thanks for taking the time to help in advance!! :)

I've written a win application that has a datagrid which displays certain contents from a database, is there a way to allow the user to export the contents of that datagrid to maybe an excel file or html file so they can print it out themselves I've done a lot of googling and most i could find were for web applications and not winform.. can someone point me in the right direction

Thanks very much guys! :)


Windows Forms23  
 
 
higoal





PostPosted: Windows Forms General, Is there a way to export a datagrid to an Excel or html format file? Top

using System.IO;
using System.Reflection;

   DataSet ds=new DataSet();
   ds=(DataSet)(this.dataGrid1.DataSource);

   Excel.Application myExcel = new Excel.Application ( ) ;

   myExcel.Application.Workbooks.Add ( true ) ;
   myExcel.Visible=true;
    myExcel.Cells[1,4]="title";

     if(ds == null)
    return;
   for(int i=0;i<ds.Tables[0].Rows.Count;i++)
   {
    for(int j=0;j<ds.Tables[0].Columns.Count;j++)
    {
     myExcel.Cells[4+i,1+j]="'"+ds.Tables[0].RowsIdea[j];
    }
   }



 
 
HappyTomato





PostPosted: Windows Forms General, Is there a way to export a datagrid to an Excel or html format file? Top

Thanks for the help, but when I run the program there was a System.InvalidCastException' where the line of code

ds = (DataSet)(this.dataGrid1.DataSource);   is..

what should i do :'(  does this depend on the format of my datagrid or something

thanks for any help again.


 
 
GraemeWT





PostPosted: Windows Forms General, Is there a way to export a datagrid to an Excel or html format file? Top

Yes - The (DataSet) cast asssumes the DataSource for your DataGrid is a DataSet.
You can change it to the correctDataSource type or if you want to be generalized, you can probably use an IList or IListSource.

If you move to VS2005 and use the DataGridView, it has a copy to clipboard method where you can choose the format to be HTML or CSV.

Regards

 
 
Rupesh





PostPosted: Windows Forms General, Is there a way to export a datagrid to an Excel or html format file? Top

A simple way to export the datagrid values to an excel file is, create a textfile with the values seperated by comma. save it as .csv extension. You will be able to open it in excel file.

Rupesh



 
 
CoolMicheal





PostPosted: Windows Forms General, Is there a way to export a datagrid to an Excel or html format file? Top

For the Microsoft Excel and Html functionality, there is a one called Spire.DataExport I have heard that it is quite good for generating Excel and Html files. Not needing have microsoft excel installed on your machine.

see : http://www.e-iceblue.com


 
 
TallMike





PostPosted: Windows Forms General, Is there a way to export a datagrid to an Excel or html format file? Top

I'm tried this, but I'm not getting data.

I just get 'System.Data.DataRow written to eavery line. Can't figure it out. Using VS 2005 and .Net 2.0