c# - 如何使用 asp.net 将 gridview 图像和数据导出为 PDF 或使用 asp.net 将 gridview 数据导出为 PDF

标签 c# asp.net ajax components

protected void Button4_Click(object sender, EventArgs e)
{
    Response.ContentType = "application/pdf";
    Response.AddHeader("content-disposition","attachment;filename=GridViewExport.pdf");
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    StringWriter sw = new StringWriter();
    HtmlTextWriter hw = new HtmlTextWriter(sw);
    GridView1.AllowPaging = false;
    GridView1.DataBind();
    GridView1.RenderControl(hw);
    StringReader sr = new StringReader(sw.ToString());
    Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
    HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
    PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
    pdfDoc.Open();
    htmlparser.Parse(sr);//this is the error line
    pdfDoc.Close();
    Response.Write(pdfDoc);
    Response.End();  
}

最佳答案

为此您必须使用第 3 方控件 尝试免费使用 iTextSharp 库

了解更多信息 go here

关于c# - 如何使用 asp.net 将 gridview 图像和数据导出为 PDF 或使用 asp.net 将 gridview 数据导出为 PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11260859/

相关文章:

c# - ConnectionString 在部署到 AWS Elastic Beanstalk 时转换

c# - 如何引发跨线程事件

c# - 不使用 AJAX AutoCompleteExtender 和 Web 服务的 ASP.Net 中的自动完成文本框

javascript - 使用 ASP.NET 和 HTML5 Canvas 创建浏览器游戏

c# - 无法添加对我的项目的引用 - 它根本没有检测到它

c# - Web.config 保存问题

javascript - 当我在 jQuery 中执行 AJAX 调用时出现 JSON 错误

javascript - HTML 格式字符串中的 jQuery

javascript - jquery移动: Uncaught TypeError: Cannot read property 'abort' of undefined error in ajax call

c# - async/await - 我使用了错误的同步上下文吗?