导出DataGridView为PDF文档

来源:互联网 发布:wifi无法连接到此网络 编辑:程序博客网 时间:2024/06/16 16:28

using System;
using System.Data;
using System.Configuration;
using System.Windows.Forms;
using iTextSharp;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
using System.Data.SqlClient;

[itextsharp.dll]


public partial class GrdvTOpdf
{
   

   

    #region ConvertGrdiViewToPDF() -> 换GridView为PDF文档

    ///
    /// 转换GridView为PDF文档    ///
    /// GridView
    /// 目标PDF文件名字
    /// 字体所在路径
    /// 字体大小
    /// 返回调用是否成功
    public  void ConvertGrdiViewToPDF(DataGridView datagridview)
    {
        ///设置导出字体
        ///
        string FontPath ="C://WINDOWS//Fonts//simsun.ttc,1";
        int FontSize = 12;
        ///
        Boolean cc=false;
        string strFileName;
        SaveFileDialog savFile = new SaveFileDialog();
        savFile.Filter = "PDF文件|.pdf";
        savFile.ShowDialog();
        if (savFile.FileName != "")
        {
            strFileName = savFile.FileName;
        }
        else
        {
            MessageBox.Show("终止导出", "终止导出", MessageBoxButtons.OK, MessageBoxIcon.Information);
            return;
        }

        //初始化一个目标文档类       
        //Document document = new Document();
        //竖排模式,大小为A4,四周边距均为25
        Document document = new Document(PageSize.A4, 25, 25, 25, 25);
        //横排模式,大小为A4,四周边距均为50
        //Document doc = new Document(PageSize.A4.rotate(),50,50,50,50);

        //调用PDF的写入方法流
        //注意FileMode-Create表示如果目标文件不存在,则创建,如果已存在,则覆盖。
        PdfWriter writer = PdfWriter.getInstance(document, new FileStream(strFileName, FileMode.Create));

        //创建PDF文档中的字体
        BaseFont baseFont = BaseFont.createFont(
          FontPath,
          BaseFont.IDENTITY_H,
          BaseFont.NOT_EMBEDDED);

        //根据字体路径和字体大小属性创建字体
        Font font = new Font(baseFont, FontSize);

        // 添加页脚
        //HeaderFooter footer = new HeaderFooter(new Phrase(footertxt), true);
        HeaderFooter footer = new HeaderFooter(new Phrase("-- ", font), new Phrase(" --", font));
        footer.Border = Rectangle.NO_BORDER;        // 不显示两条横线
        footer.Alignment = Rectangle.UNDEFINED;  // 让页码居中
        document.Footer = footer;

        //打开目标文档对象
        document.Open();

        //根据数据表内容创建一个PDF格式的表
        PdfPTable table = new PdfPTable(datagridview.Columns.Count);
        //iTextSharp.text.Table table = new iTextSharp.text.Table(datagridview.Columns.Count);

        // GridView的所有数据全输出
        //datagridview.AllowPaging = false;

        // ---------------------------------------------------------------------------
        // 添加表头
        // ---------------------------------------------------------------------------
        // 设置表头背景色
        //table.DefaultCell.BackgroundColor = Color.GRAY;  // OK
        //table.DefaultCell.BackgroundColor = (iTextSharp.text.Color)System.Drawing.Color.FromName("#3399FF"); // NG
        //table.DefaultCell.BackgroundColor = iTextSharp.text.Color;

        //table.DefaultCell.BackgroundColor = System.Drawing.Color.DodgerBlue; 
        // 添加表头,每一页都有表头
        for (int j = 0; j < datagridview.Columns.Count; j++)
        {
            table.addCell(new Phrase(datagridview[j, 0].Value.ToString(), font));
        }

        // 告诉程序这行是表头,这样页数大于1时程序会自动为你加上表头。
        table.HeaderRows = 1;
        //
        // ---------------------------------------------------------------------------
        // 添加数据
        // ---------------------------------------------------------------------------
        // 设置表体背景色
        //table.DefaultCell.BackgroundColor = Color
        //遍历原gridview的数据行
        for (int i = 0; i < datagridview.Rows.Count; i++)
        {
            for (int j = 0; j < datagridview.Columns.Count; j++)
            {
             
                try
                {
                    table.addCell(new Phrase(datagridview[j,i].Value.ToString(), font));
                }
                catch (Exception e)
                {

                    MessageBox.Show(e.Message);
                    cc = true;
                }
                              

            }
        }

        //在目标文档中添加转化后的表数据
        document.Add(table);

        //关闭目标文件
        document.Close();

        //关闭写入流
        writer.Close();

        // Dialog
        if (!cc)
        {
            MessageBox.Show("已生成PDF文件。", "生成成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
     
    }

   
    #endregion


}