C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » DataGridView »

Exporting DataGridView into Excel


Posted Date: 07 Aug 2009    Resource Type: Code Snippets    Category: DataGridView
Author: K'ranMember Level: Gold    
Rating: 1 out of 5Points: 5



Exporting DatagridView into Excel



Call this function by passing Datagridview parameter into this function




public void exporttoexcel(DataGridView grid)
{
Microsoft.Office.Interop.Excel.ApplicationClass excelSheet = new Microsoft.Office.Interop.Excel.ApplicationClass();
excelSheet.Application.Workbooks.Add(true);
int columnIndex = 0;
//Columns Heading of Datagridview
foreach (DataGridViewColumn column in grid.Columns)
{
columnIndex++;
excelSheet.Cells[1, columnIndex] = column.HeaderText;
}
int rowIndex = 0;
//get all rows by column wise
foreach (DataGridViewRow row in grid.Rows)
{
rowIndex++;
columnIndex = 0;
foreach (DataGridViewColumn column in grid.Columns)
{
columnIndex++;
excelSheet.Cells[rowIndex + 1, columnIndex] = row.Cells[column.Name].FormattedValue;
}
}
excelSheet.Visible = true;
Microsoft.Office.Interop.Excel.Worksheet workSheet = (Microsoft.Office.Interop.Excel.Worksheet)excelSheet.ActiveSheet;
workSheet.Activate();
}



Responses

Author: Filip    12 Aug 2009Member Level: Silver   Points : 1
Hi, nice code, but Excel Interop is not best way to do this. It has many issues like speed and compatibility with Excel versions. I advice you to try GemBox .NET Excel component. It is much faster and very easy to use.


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Exporting Datagridview into Excel  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Handling Errors in Gridview
Previous Resource: Move GridView Rows with up and down keys
Return to Discussion Resource Index
Post New Resource
Category: DataGridView


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use