0

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

Newbie
  • 193
  • 13
  • Does this answer your question? [gridview data export to excel in asp.net](https://stackoverflow.com/questions/15832339/gridview-data-export-to-excel-in-asp-net) – Amit Verma Jun 02 '22 at 07:34
  • No, I did try that but still not working. The excel with the right headers are being exported but still no data is displaying. – Newbie Jun 02 '22 at 08:45
  • Use EPPlus: https://www.epplussoftware.com/ – VDWWD Jun 02 '22 at 15:57
  • Looking at dt.Rows.Add(); I think you want to pass in the row like dt.Rows.Add(your_row); – JobesK Jun 02 '22 at 18:21
  • Why are you executing a gridview.databind(), but not having set the data source? If you execute databind() without the datasource, then the grid will become empty. I would remove that databind() method of the gv, and see if that works. – Albert D. Kallal Jun 04 '22 at 02:08

0 Answers0