Solved

Transfer System.Windows.Forms.DataGrid contents to MS Excel

Posted on 2007-03-29
4
262 Views
Last Modified: 2008-03-17
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

0
Comment
Question by:bowemc
  • 2
  • 2
4 Comments
 
LVL 10

Accepted Solution

by:
SystemExpert earned 500 total points
ID: 18821707
Hi,
the following link has complete flow of code to export the datagrid data to Excel

http://www.dotnetjohn.com/articles.aspx?articleid=78

this link has details that what can be the problem whem you export data to excel

http://geekswithblogs.net/azamsharp/archive/2005/08/14/50166.aspx

Thanks & Regards
0
 
LVL 10

Expert Comment

by:SystemExpert
ID: 18821723
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
0
 

Author Comment

by:bowemc
ID: 18823501
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();
0
 

Author Comment

by:bowemc
ID: 18823596
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
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now