我在按钮点击事件中将gridview导出到Excel工作表,但导出的Excel工作表仅在信息下方显示
private void ExportGridView(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";
using (StringWriter sw = new StringWriter())
{
HtmlTextWriter hw = new HtmlTextWriter(sw);
//To Export all pages
GridView1.AllowPaging = false;
GridView1.DataBind();
GridView1.RenderControl(hw);
//style to format numbers to string
string style = @"<style> .textmode { } </style>";
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}
}
public override void VerifyRenderingInServerForm(Control control)
{
/* Verifies that the control is rendered */
}
另外,aspx for gridview中的代码是
......
<asp:GridView ID="GridView1" runat="server" Font-Size="10pt" AllowSorting="True"
BackColor="White" BorderColor="#CCCCCC" AutoGenerateSelectButton="true"
OnSelectedIndexChanged="GridView1_SelectedIndexChanged" OnRowDataBound="GridView1_RowDataBound" OnSorting="GridView1_Sorting">
<%-- <Columns>
....................
<td> <asp:Button runat="server" ID="btnExport" Text="Export to Excel" onclick="btnExport_Click" /></td>
</tr>
</table>
</asp:Content>
答案 0 :(得分:0)
我解决了这个问题。在将allowpaging设置为false之后,我没有绑定gridview。以下是完整代码:
<form class="form">
<input class="form-control" />
</form>
<div id="bootstrap-container">
<form class="form">
<input class="form-control" />
</form>
</div>
此外,另一个更改是在我设置的页面中的.aspx文件中设置EnableEventValidation =&#34; false&#34;