我有一个名为 GridView1 的网格视图。我想以 PDF 格式下载此网格视图。为此,我尝试了以下代码。
PDF 文件正在下载但未打开。我收到错误,即 Adobe Reader 无法打开此文件。
请帮我。
SqlDataAdapter da = new SqlDataAdapter("select * from info", con);
DataTable dt = new DataTable();
da.Fill(dt);
GridView1.DataSource = dt;
GridView grdview = new GridView();
GridView1.DataBind();
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.pdf");
HttpContext.Current.Response.ContentType = "application/pdf";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView1.RenderControl(hw);
HttpContext.Current.Response.Write(sw.ToString());
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();