Home | Articles|Namespace|Interview Questions|Tools|Jobs|Projects|Community
Asp.net Tutorials

»Dotnet Ads
»Message Boards
Message Boards
Dotnet Books

»Member Details
Register
Login
LogOut
Submit Code
Submit Jobs
Submit Projects

»Competition
Community
Winners
Prizes
Write For Us
Members

»Other Resources
Links
Dotnet Resources

Export Gridview data to excel Using Excel Application object

For exporting to Excel using Excel Application object you have to add Reference "Microsoft Excel 11.0 Object Library" or "Microsoft Excel 9.0 Object Library" and then import the following:

using Excel = Microsoft.Office.Interop.Excel;

void ExportGridToExcelUsingApplication()
{
Excel.ApplicationClass excel = new Microsoft.Office.Interop.Excel.ApplicationClass();
excel.Application.Workbooks.Add(true);
DataSet ds = (DataSet)GridView1.DataSource;
DataTable table = ds.Tables[0];
int columnIndex = 0;

foreach (DataColumn col in table.Columns)
{
columnIndex++;
excel.Cells[1, columnIndex] = col.ColumnName;
excel.Cells.Font.Name = "Tahoma";
excel.Cells.Font.Size = 8;
excel.Cells.WrapText = true;

}

int rowIndex = 0;
foreach (DataRow row in table.Rows)
{
rowIndex++;
columnIndex = 0;
foreach (DataColumn col in table.Columns)
{
columnIndex++;
excel.Cells[rowIndex + 1, columnIndex] = row.ItemArray[columnIndex - 1].ToString();


}
}

excel.Visible = true;
}

Have a Question and dont know the answer post it below and get answers in minutes

Due to spam this feature is disabled
To get answers fast , make sure you enter a detailed subject for example: "DataGrid issues need answer" not "DataGrid"

Subject:

Catjegory Name:

Message:



© 2008 dotnetwatch.com -- Privacy policy

Website Design & Internet Marketing by Shivam