用于将gridview数据导出为pdf的c#代码 [英] c# code for exporting gridview data into pdf

查看:140
本文介绍了用于将gridview数据导出为pdf的c#代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

c#代码,用于将gridview数据导出为pdf

c# code for exporting gridview data into pdf

推荐答案

您可以使用谷歌搜索下载iTextSharp.dll。 。 。



you can download iTextSharp.dll using google search. . .

using iTextSharp.text.pdf;
using iTextSharp.text;
using iTextSharp.text.html.simpleparser;




class string pdf()
{
Response.Clear();
            Response.BufferOutput = true;
            Response.ContentType = "Application/pdf";
            Response.Cache.SetCacheability(HttpCacheability.Private); // set as private for HTTPS dowload issue with IE
            Response.AppendHeader("Content-Disposition", "attachment;filename=Classes_" + cmbsubcategory.SelectedValue.ToString() + "_" + cmbstatus.SelectedItem.ToString() + "_" + DateTime.Now.ToString() + ".pdf");
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            StringWriter sw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(sw);
            gridbind(cmbsubcategory.SelectedValue.ToString(), cmbstatus.SelectedValue.ToString());
            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();
            Paragraph footer = new Paragraph("Category:Class | Subcategory:" + cmbsubcategory.SelectedValue.ToString() + "| Status:" + cmbstatus.SelectedItem.ToString() + "| " + DateTime.Now.ToShortDateString(), FontFactory.GetFont(FontFactory.COURIER, 14, iTextSharp.text.Font.NORMAL, new Color(0, 163, 217)));
            footer.Alignment = Element.ALIGN_RIGHT;
            PdfPTable footerTbl = new PdfPTable(1);
            footerTbl.TotalWidth = 300;
            footerTbl.HorizontalAlignment = Element.ALIGN_CENTER;

            string imageFilePath = GetUrl("header.jpg");

            iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(imageFilePath);

            jpg.Alignment = Element.ALIGN_CENTER;



            PdfPCell cell = new PdfPCell(footer);
            pdfDoc.Add(jpg);
            pdfDoc.Add(footer);


            htmlparser.Parse(sr);
            pdfDoc.Close();
            Response.Write(pdfDoc);
            Response.End();
}


ITextSharp [ ^ ]是一个非常巧妙的第三方,可以让你这样做。
ITextSharp[^] is a very neat third party that would allow you to do this.


你好,



看看这篇文章:将数据导出到Excel,Word,PDF而不使用DataBase自动化



它介绍了如何将数据从数据库导出到datagridview,然后导出为PDF和其他文档格式。



希望你能从中获得一些东西。
Hello,

Have a look at this article: Export Data to Excel, Word, PDF without Automation from DataBase.

It introduces how to export data from database to datagridview and then to PDF and other document format.

Hope that you can get something from it.


这篇关于用于将gridview数据导出为pdf的c#代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆