3

我有一个需要导出到 Excel 的网格视图。我已设法从行中删除复选框,但不知道如何从标题中删除并完全删除复选框列。感谢帮助。

    <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>

这是要导出到 excel 的后端 C# 代码。

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]);
        }
    }
}
4

2 回答 2

5

试试这个导出功能,它隐藏了不需要的列,即行的第 0 列......

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();
}

或者

在导出时,删除该列。类似的东西:

GridView1.Columns[0].visible = false;
 GridView1.DataBind();
  GridView1.RenderControl(htmlWrite);
 Response.Write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />"); 
于 2012-06-18T06:49:47.440 回答
1

我们可以在导出到 Excel 之前删除它们,而不是隐藏控件。
这可以通过遍历单元格并找到单元格中存在的控件来完成。然后识别控件类型并将它们从该单元格中删除。

以下是代码:

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);
        }
    }
  }
}

在上面的代码中,我循环遍历每一行和单元格。然后删除那里存在的所有 ID为 lblrow的TextBox、DropdownList 和 Label 。

于 2015-05-20T10:22:05.290 回答