数据表转换为PDF [英] Convert Datatable to PDF

查看:122
本文介绍了数据表转换为PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以得到code在Asp.net的Web应用程序转换数据表为PDF。我想有功能导出数据表 PDF 。我发现<一个href=\"http://www.aspsnippets.com/Articles/Export-DataSet-or-DataTable-to-Word-Excel-PDF-and-CSV-Formats.aspx\">this文章,但它使用 GridView控件导出

Can I get a code for converting datatable to pdf in Asp.net Web application. I want to have functionality to export datatable into PDF. I found this article but it is using gridview for exporting

推荐答案

使用iTextSharp的,你可以做it.It可以从网络上下载,它是免费的。
请在下面找到了code,

Using iTextSharp,you can do it.It can be download from internet and it is free. Please, find the code below,

   public void ExportToPdf(DataTable dt)
   {      
    Document document = new Document();
    PdfWriter writer = PdfWriter.GetInstance(document, new FileStream("c://sample.pdf", FileMode.Create));
    document.Open();
            iTextSharp.text.Font font5 = iTextSharp.text.FontFactory.GetFont(FontFactory.HELVETICA, 5);

    PdfPTable table = new PdfPTable(dt.Columns.Count);
    PdfPRow row = null;
    float[] widths = new float[] { 4f, 4f, 4f, 4f };

    table.SetWidths(widths);

    table.WidthPercentage = 100;
    int iCol = 0;
    string colname = "";
    PdfPCell cell = new PdfPCell(new Phrase("Products"));

    cell.Colspan = dt.Columns.Count;

    foreach (DataColumn c in dt.Columns)
    {

        table.AddCell(new Phrase(c.ColumnName, font5));
    }

    foreach (DataRow r in dt.Rows)
    {
        if (dt.Rows.Count > 0)
        {
            table.AddCell(new Phrase(r[0].ToString(), font5));
            table.AddCell(new Phrase(r[1].ToString(), font5));
            table.AddCell(new Phrase(r[2].ToString(), font5));
            table.AddCell(new Phrase(r[3].ToString(), font5));
        }          
    }  document.Add(table);
        document.Close();
}

这篇关于数据表转换为PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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