Here I have created sample that will help you out.
HTML
<div>
<asp:GridView runat="server" ID="GridView1" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="Country" HeaderText="Country" />
</Columns>
</asp:GridView>
<br />
<asp:GridView runat="server" ID="GridView2" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="Country" HeaderText="Country" />
</Columns>
</asp:GridView>
<br />
<br />
<asp:Button ID="bntExport" Text="Export" runat="server" OnClick="ExcelExport" />
</div>
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id", typeof(int)), new DataColumn("Name", typeof(string)), new DataColumn("Country", typeof(string)) });
dt.Rows.Add(1, "John Hammond", "United States");
dt.Rows.Add(2, "Mudassar Khan", "India");
dt.Rows.Add(3, "Suzanne Mathews", "France");
dt.Rows.Add(4, "Robert Schidner", "Russia");
DataTable dt1 = dt.Clone();
dt1.Rows.Add(1,"Sachin","India");
dt1.Rows.Add(2,"Sangakara","SriLanka");
dt1.Rows.Add(3,"Rickey","Australia");
dt1.Rows.Add(4, "Kalis", "South Africa");
GridView1.DataSource = dt;
GridView1.DataBind();
GridView2.DataSource = dt1;
GridView2.DataBind();
}
}
protected void ExcelExport(object sender, EventArgs e)
{
GridView[] gvExcel = new GridView[] { GridView1, GridView2 };
string[] name = new string[] { "Grid1", "Grid2" };
var aCode = 65;
int i = 0;
using (XLWorkbook wb = new XLWorkbook())
{
foreach (GridView grid in gvExcel)
{
var ws = wb.Worksheets.Add(name[i]);
int rowIndex = 1;
int columnIndex = 0;
foreach (TableCell cell in grid.HeaderRow.Cells)
{
ws.Cell(string.Format("{0}{1}", Char.ConvertFromUtf32(aCode + columnIndex), rowIndex)).Value = cell.Text;
ws.Cell(string.Format("{0}{1}", Char.ConvertFromUtf32(aCode + columnIndex), rowIndex)).Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
ws.Cell(string.Format("{0}{1}", Char.ConvertFromUtf32(aCode + columnIndex), rowIndex)).Style.Fill.BackgroundColor = XLColor.LightSlateGray;
columnIndex++;
}
rowIndex++;
foreach (GridViewRow row in grid.Rows)
{
int valueCount = 0;
foreach (TableCell rowValue in row.Cells)
{
ws.Cell(string.Format("{0}{1}", Char.ConvertFromUtf32(aCode + valueCount), rowIndex)).Value = rowValue.Text;
ws.Cell(string.Format("{0}{1}", Char.ConvertFromUtf32(aCode + valueCount), rowIndex)).Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
valueCount++;
}
rowIndex++;
}
i++;
}
Response.Clear();
Response.Buffer = true;
Response.Charset = "";
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("content-disposition", "attachment;filename=ExportMultiple.xlsx");
using (MemoryStream MyMemoryStream = new MemoryStream())
{
wb.SaveAs(MyMemoryStream);
MyMemoryStream.WriteTo(Response.OutputStream);
Response.Flush();
Response.End();
}
}
}
Screenshot
