using System.IO;
using System.Data;
using ClosedXML.Excel;
using System.Data.SqlClient;
using System.Configuration;
protected void ExportExcel_Click(object sender, EventArgs e)
{
using (XLWorkbook wb = new XLWorkbook())
{
//Loop through the GridView pages.
for (int i = 0; i < GridView1.PageCount; i++)
{
//Set the Current Page.
GridView1.PageIndex = i;
this.BindGrid();
//Create a DataTable with schema same as GridView columns.
DataTable dt = new DataTable("Page_" + (i + 1));
foreach (TableCell cell in GridView1.HeaderRow.Cells)
{
dt.Columns.Add(cell.Text);
}
//Loop and add rows from GridView to DataTable
foreach (GridViewRow row in GridView1.Rows)
{
dt.Rows.Add();
for (int j = 0; j < row.Cells.Count; j++)
{
dt.Rows[dt.Rows.Count - 1][j] = row.Cells[j].Text;
}
}
//Add DataTable as Worksheet.
wb.Worksheets.Add(dt);
//2nd Sheet
GridView1.PageIndex = i;
this.BindGrid();
//Create a DataTable with schema same as GridView columns.
dt = new DataTable("Page_" + (i + 2));
foreach (TableCell cell in GridView1.HeaderRow.Cells)
{
dt.Columns.Add(cell.Text);
}
//Loop and add rows from GridView to DataTable
foreach (GridViewRow row in GridView1.Rows)
{
dt.Rows.Add();
for (int j = 0; j < row.Cells.Count; j++)
{
dt.Rows[dt.Rows.Count - 1][j] = row.Cells[j].Text;
}
}
//Add DataTable as Worksheet.
wb.Worksheets.Add(dt);
}
//Export the Excel file.
Response.Clear();
Response.Buffer = true;
Response.Charset = "";
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("content-disposition", "attachment;filename=GridView.xlsx");
using (MemoryStream MyMemoryStream = new MemoryStream())
{
wb.SaveAs(MyMemoryStream);
MyMemoryStream.WriteTo(Response.OutputStream);
Response.Flush();
Response.End();
}
}
}
using System.Data;
using ClosedXML.Excel;
using System.Data.SqlClient;
using System.Configuration;
protected void ExportExcel_Click(object sender, EventArgs e)
{
using (XLWorkbook wb = new XLWorkbook())
{
//Loop through the GridView pages.
for (int i = 0; i < GridView1.PageCount; i++)
{
//Set the Current Page.
GridView1.PageIndex = i;
this.BindGrid();
//Create a DataTable with schema same as GridView columns.
DataTable dt = new DataTable("Page_" + (i + 1));
foreach (TableCell cell in GridView1.HeaderRow.Cells)
{
dt.Columns.Add(cell.Text);
}
//Loop and add rows from GridView to DataTable
foreach (GridViewRow row in GridView1.Rows)
{
dt.Rows.Add();
for (int j = 0; j < row.Cells.Count; j++)
{
dt.Rows[dt.Rows.Count - 1][j] = row.Cells[j].Text;
}
}
//Add DataTable as Worksheet.
wb.Worksheets.Add(dt);
//2nd Sheet
GridView1.PageIndex = i;
this.BindGrid();
//Create a DataTable with schema same as GridView columns.
dt = new DataTable("Page_" + (i + 2));
foreach (TableCell cell in GridView1.HeaderRow.Cells)
{
dt.Columns.Add(cell.Text);
}
//Loop and add rows from GridView to DataTable
foreach (GridViewRow row in GridView1.Rows)
{
dt.Rows.Add();
for (int j = 0; j < row.Cells.Count; j++)
{
dt.Rows[dt.Rows.Count - 1][j] = row.Cells[j].Text;
}
}
//Add DataTable as Worksheet.
wb.Worksheets.Add(dt);
}
//Export the Excel file.
Response.Clear();
Response.Buffer = true;
Response.Charset = "";
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("content-disposition", "attachment;filename=GridView.xlsx");
using (MemoryStream MyMemoryStream = new MemoryStream())
{
wb.SaveAs(MyMemoryStream);
MyMemoryStream.WriteTo(Response.OutputStream);
Response.Flush();
Response.End();
}
}
}