我有一个需要导出到Excel的gridview。我已设法从行中删除Checkboxes但不知道如何从Header中删除并完全删除Checkbox列。谢谢你的帮助。
<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]);
}
}
}
答案 0 :(得分: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\" />");
答案 1 :(得分:0)
除了隐藏控件之外,我们可以在导出到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);
}
}
}
}
在上面的代码中,我循环遍历每一行和单元格。然后删除所有 TextBox,DropdownList和Label ,其中包含ID lblrow 。