»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 objectFor 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;
}
|