I am trying to export multiple GridViews to Excel, each of the gridviews in separate tabs.
It exports, but only the headers and not the data (Rows)?
protected void Excel_Export(object sender, EventArgs e)
{
XLWorkbook wb = new XLWorkbook();
GridView[] gvExcel = new GridView[] { GridViewA, GridViewB, GridViewC, GridViewD };
string[] name = new string[] { "A", "B", "C", "D" };
for (int i = 0; i < gvExcel.Length; i++)
{
if (gvExcel[i].Visible)
{
gvExcel[i].AllowPaging = false;
gvExcel[i].DataBind();
DataTable dt = new DataTable(name[i].ToString());
for (int z = 0; z < gvExcel[i].Columns.Count; z++)
{
dt.Columns.Add(gvExcel[i].Columns[z].HeaderText);
}
foreach (GridViewRow row in gvExcel[i].Rows)
{
dt.Rows.Add();
for (int c = 0; c < row.Cells.Count; c++)
{
dt.Rows[dt.Rows.Count - 1][c] = row.Cells[c].Text;
}
}
wb.Worksheets.Add(dt);
gvExcel[i].AllowPaging = true;
}
}
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.Buffer = true;
Response.Charset = "";
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("content-disposition", "attachment;filename=WorkBook.xlsx");
using (MemoryStream MyMemoryStream = new MemoryStream())
{
wb.SaveAs(MyMemoryStream);
MyMemoryStream.WriteTo(Response.OutputStream);
Response.Flush();
Response.End();
}
}
My Gridview Markup is:
<asp:GridView ID="GridViewA" runat="server" AutoGenerateColumns="false"
DataKeyNames="id" OnRowEditing="OnRowEditingA" OnRowCancelingEdit="OnRowCancelingEditA" OnRowDataBound="DataBoundA"
OnRowUpdating="OnRowUpdatingA" OnRowDeleting="OnRowDeletingA" EmptyDataText="No records has been added." Width="100%">
<alternatingrowstyle backcolor="White" forecolor="#000000" />
<editrowstyle backcolor="#999999" />
<footerstyle backcolor="#5D7B9D" font-bold="True" forecolor="White" />
<headerstyle backcolor="#5D7B9D" font-bold="True" forecolor="White" />
<pagerstyle backcolor="#284775" forecolor="White" horizontalalign="Center" />
<rowstyle backcolor="#F7F6F3" forecolor="#000000" />
<selectedrowstyle backcolor="#E2DED6" font-bold="True" forecolor="#000000" />
<sortedascendingcellstyle backcolor="#E9E7E2" />
<sortedascendingheaderstyle backcolor="#506C8C" />
<sorteddescendingcellstyle backcolor="#FFFDF8" />
<sorteddescendingheaderstyle backcolor="#6F8DAE" />
<columns>
<asp:TemplateField HeaderText="Name" ItemStyle-Width="200">
<itemtemplate>
<asp:Label ID="lbl1" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
</itemtemplate>
<edititemtemplate>
<asp:TextBox ID="txt1" runat="server" Text='<%# Eval("Name") %>' Width="150"></asp:TextBox>
</edititemtemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Laptop" ItemStyle-Width="150">
<itemtemplate>
<asp:Label ID="lbl2" runat="server" Text='<%# Eval("LaptopAllowance") %>'></asp:Label>
</itemtemplate>
<edititemtemplate>
<asp:TextBox ID="txt2" runat="server" Text='<%# Eval("LaptopAllowance") %>' Width="150"></asp:TextBox>
</edititemtemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Email Address" ItemStyle-Width="250">
<itemtemplate>
<asp:Label ID="lbl3" runat="server" Text='<%# Eval("EmailAddress") %>'></asp:Label>
</itemtemplate>
<edititemtemplate>
<asp:TextBox ID="txt3" runat="server" Text='<%# Eval("EmailAddress") %>' Width="120"></asp:TextBox>
</edititemtemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Status" ItemStyle-Width="90">
<itemtemplate>
<asp:Label ID="lbl4" runat="server" Text='<%# Eval("Status") %>'></asp:Label>
</itemtemplate>
<edititemtemplate>
<asp:TextBox ID="txt4" runat="server" Text='<%# Eval("Status") %>' Width="120"></asp:TextBox>
</edititemtemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="AV" ItemStyle-Width="80">
<itemtemplate>
<asp:Label ID="lbl5" runat="server" Text='<%# Eval("AV") %>'></asp:Label>
</itemtemplate>
<edititemtemplate>
<asp:TextBox ID="txt5" runat="server" Text='<%# Eval("AV") %>' Width="120"></asp:TextBox>
</edititemtemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Agent" ItemStyle-Width="80">
<itemtemplate>
<asp:Label ID="lbl6" runat="server" Text='<%# Eval("Agent") %>'></asp:Label>
</itemtemplate>
<edititemtemplate>
<asp:TextBox ID="txt6" runat="server" Text='<%# Eval("Agent") %>' Width="120"></asp:TextBox>
</edititemtemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Device Name" ItemStyle-Width="150">
<itemtemplate>
<asp:Label ID="lbl7" runat="server" Text='<%# Eval("DeviceName") %>'></asp:Label>
</itemtemplate>
<edititemtemplate>
<asp:TextBox ID="txt7" runat="server" Text='<%# Eval("DeviceName") %>' Width="120"></asp:TextBox>
</edititemtemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="true" />
<asp:CommandField ShowDeleteButton="true" />
</columns>
</asp:GridView>
GridView is getting the data from SQL tables in my database. I can edit, update and delete rows in the gridview as well, so am not sure if that would cause the problem for it not to export the gridview data as well.
Not sure why it's not adding the data which is in the gridview to the excel spreadsheet?
Thanks