Remove Checkboxes while exporting Gridview to Exce

2020-03-25 16:18发布

问题:

I have a gridview that needs to be exported to Excel. I have managed to remove the Checkboxes from Rows but don't know how to remove from the Header and also delete the Checkbox Column entirely. Thanks for help.

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" DataKeyNames="Id">
    <Columns>
        <asp:TemplateField HeaderText="Select" >
            <HeaderTemplate>
                <input id="chkAll" onclick="checkAll(this);" runat="server" type="checkbox" />
            </HeaderTemplate>
            <ItemTemplate>
            <asp:CheckBox  ID="CheckBox1" runat="server" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="Event" HeaderText="Event" SortExpression="Event" />
        <asp:BoundField DataField="Event_Description" HeaderText="Event_Description" SortExpression="Event_Description" />
        <asp:BoundField DataField="Start_Date" HeaderText="Start_Date" SortExpression="Start_Date" />
    </Columns>
    </asp:GridView>

Here's the backend C# code to export to excel.

protected void download_Click(object sender, EventArgs e)
{
    PrepareGridViewForExport(GridView1);
    Response.ClearContent(); 
    Response.AddHeader("content-disposition", "attachment; filename=GridViewToExcel.xls"); 
    Response.ContentType = "application/excel"; 
    StringWriter sWriter = new StringWriter();
    HtmlTextWriter hTextWriter = new HtmlTextWriter(sWriter);
    GridView1.RenderControl(hTextWriter);
    Response.Write(sWriter.ToString());
    Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{
}
private void PrepareGridViewForExport(Control gv)
{
    Literal l = new Literal();
    string name = String.Empty;
    for (int i = 0; i < gv.Controls.Count; i++)
    {
        if (gv.Controls[i].GetType() == typeof(CheckBox))
        {
            gv.Controls.Remove(gv.Controls[i]);
            gv.Controls.AddAt(i, l);
        }
        if (gv.Controls[i].HasControls())
        {
            PrepareGridViewForExport(gv.Controls[i]);
        }
    }
}

回答1:

Try out this export function , which hides not needed column i.e column 0 of the row...

protected void btnExportExcel_Click(object sender, EventArgs e)
{
    Response.Clear();
    Response.Buffer = true;
    Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.xls");
    Response.Charset = "";
    Response.ContentType = "application/vnd.ms-excel";
    StringWriter sw = new StringWriter();
    HtmlTextWriter hw = new HtmlTextWriter(sw);
    GridView1.DataBind(); 


    GridView1.HeaderRow.Cells[0].Visible = false; 

    for (int i = 0; i < GridView1.Rows.Count;i++ )
    {
       GridViewRow row = GridView1.Rows[i];
       row.Cells[0].Visible = false;
       row.Cells[0].FindControl("chkCol0").Visible = false; 
     }
    GridView1.RenderControl(hw);
    Response.Write(style);
    Response.Output.Write(sw.ToString());
    Response.End();
}

or

On your export, drop the column. Something something like:

GridView1.Columns[0].visible = false;
 GridView1.DataBind();
  GridView1.RenderControl(htmlWrite);
 Response.Write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />"); 


回答2:

Instead of hiding the control we can remove them before exporting into Excel.
This can be done by looping through the cell and find the controls present in the cell. Then identify the control type and remove them from that cell.

Following is the code:

foreach (GridViewRow row in GridView1.Rows)
{
  row.BackColor = Color.White;
  foreach (TableCell cell in row.Cells)
  {
    cell.CssClass = "textmode";
    for (int i = 0; i < cell.Controls.Count; i++)
    {
      Control ctr = cell.Controls[i];
      if (ctr is TextBox)
        cell.Controls.Remove(ctr);
      else if (ctr is DropDownList)
        cell.Controls.Remove(ctr);
      else if (ctr is Label)
        {
          if (ctr.ClientID.Contains("lblrow"))
            cell.Controls.Remove(ctr);
        }
    }
  }
}

In the above code I am looping through the each row and cells. Then removing all the TextBox, DropdownList and Label with the ID lblrow which are present there.