asp.net 打印和数据导出功能

2025-05-18 21:21:37
推荐回答(1个)
回答1:

可以使用网页的打印来实现吧。 导出电子表格的相关代码挺多的。
我提供一个给您:
public override void VerifyRenderingInServerForm(Control control)
{

}
public void DGToExcel(System.Web.UI.Control ctl)
{
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=Excel.xls");
HttpContext.Current.Response.Charset = "UTF-8";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Default;
HttpContext.Current.Response.ContentType = "application/ms-excel";
ctl.Page.EnableViewState = false;
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
ctl.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();
}
protected void Button2_Click(object sender, EventArgs e)
{
DGToExcel(this.GridView1);
}