winform DataGridView控件的打印

借用网上的代码,应该是完整的打印过程了。有打印,预览,设置三个按钮

代码:

public partial class Example : Form

    {

        //打印文檔

        PrintDocument pdDocument = new PrintDocument();

 

        //打印格式設置頁面

        PageSetupDialog dlgPageSetup = new PageSetupDialog();

 

        //打印頁面

        PrintDialog dlgPrint = new PrintDialog();

 

        //實例化打印預覽

        PrintPreviewDialog dlgPrintPreview = new PrintPreviewDialog();

        public Example()

        {

            InitializeComponent();          

            pdDocument.PrintPage += new PrintPageEventHandler(OnPrintPage);

 

           

            //頁面設置的打印文檔設置為需要打印的文檔

            dlgPageSetup.Document = pdDocument;

 

            //打印界面的打印文檔設置為被打印文檔

            dlgPrint.Document = pdDocument;

 

            //打印預覽的打印文檔設置為被打印文檔

            dlgPrintPreview.Document = pdDocument;

 

        }

 

 

  /// <summary>

        /// //顯示打印預覽界面 ,此处需要添加一个打印预览的按钮

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        private void btnPrintView_Click(object sender, EventArgs e)

        {           

            dlgPrintPreview.ShowDialog(); 

        }

 

        /// <summary>

        /// 打印设置,此处需要添加一个打印设置的按钮

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        private void btnPrintSetup_Click(object sender, EventArgs e)

        {

            dlgPageSetup.ShowDialog();

            dlgPrint.ShowDialog();

        }

 

        /// 

        /// printDocument的PrintPage事件 ,实现打印功能

        /// 

        /// 

        /// 

        private void OnPrintPage(object sender, PrintPageEventArgs e)

        {

            int iX = 60;

            int iY = 40;

            PrintDataGridView11.Print(dataGridView1, true, e, ref iX, ref iY);

        }

 

        /// <summary>

        /// 打印,此处需添加一个打印按钮

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        private void btnPrint_Click(object sender, EventArgs e)

        {

            pdDocument.Print();

        }

}

 

 

/// <summary>

    /// 实现DataGridView的打印 类

   /// </summary>

    public class PrintDataGridView11

    {

        private static List<DataGridViewCellPrint> CellPrintList = new List<DataGridViewCellPrint>();

        /// <summary>

        /// 打印的行数

        /// </summary>

        private static int printRowCount = 0;

        /// <summary>

        /// 是否要打印

        /// </summary>

        private static bool IsPrint = true;

        /// <summary>

        /// 设置的起始位置是否大于默认打印的边框

        /// </summary>

        private static bool IsRole = true;

        /// <summary>

        /// X坐标

        /// </summary>

        private static int PoXTmp = 0;

        /// <summary>

        /// Y坐标

        /// </summary>

        private static int PoYTmp = 0;

        /// <summary>

        /// 列间距

        /// </summary>

        private static int WidthTmp = 0;

        /// <summary>

        /// 行间距

        /// </summary>

        private static int HeightTmp = 0;

        /// <summary>

        /// 列数

        /// </summary>

        private static int RowIndex = 0;

 

        /// 

        /// 打印DataGridView控件

        /// 

        /// DataGridView控件

        /// 是否包括列标题

        /// 为 System.Drawing.Printing.PrintDocument.PrintPage 事件提供数据。

        /// 起始X坐标

        /// 起始Y坐标

        ///

        public static void Print(DataGridView dataGridView, bool includeColumnText, PrintPageEventArgs eValue, ref int PoX, ref int PoY)

        {

            try

            {

                if (PrintDataGridView11.IsPrint)

                {

                    PrintDataGridView11.printRowCount = 0;

                    PrintDataGridView11.IsPrint = false;

                    PrintDataGridView11.DataGridViewCellVsList(dataGridView, includeColumnText); //获取要打印的数据

                    if (0 == PrintDataGridView11.CellPrintList.Count)

                        return;

                    if (PoX > eValue.MarginBounds.Left) //如果设置的起始位置大于默认打印的边框,IsRole为true

                        PrintDataGridView11.IsRole = true;

                    else

                        PrintDataGridView11.IsRole = false;

                    PrintDataGridView11.PoXTmp = PoX;

                    PrintDataGridView11.PoYTmp = PoY;

                    PrintDataGridView11.RowIndex = 0;

                    WidthTmp = 0;

                    HeightTmp = 0;

                }

                if (0 != PrintDataGridView11.printRowCount)//换页后确定打印的初始位置

                {

                    if (IsRole)  //如果设置的起始位置大于默认打印的边框,起始位置为默认打印边框

                    {

                        PoX = PoXTmp = eValue.MarginBounds.Left;

                        PoY = PoYTmp = eValue.MarginBounds.Top;

                    }

                    else

                    {

                        PoX = PoXTmp;

                        PoY = PoYTmp;

                    }

                }

                while (PrintDataGridView11.printRowCount < PrintDataGridView11.CellPrintList.Count)

                {

                    DataGridViewCellPrint CellPrint = CellPrintList[PrintDataGridView11.printRowCount];

                    if (RowIndex == CellPrint.RowIndex)//一行一行打印,CellPrint.RowIndex为datagridview1的行号

                        PoX = PoX + WidthTmp;            //如果数据在一行,x坐标右移

                    else //换行后Y坐标下移,X坐标回到初始位置

                    {

                        PoX = PoXTmp;

                        PoY = PoY + HeightTmp;

                        if (PoY + HeightTmp > eValue.MarginBounds.Bottom)//分页

                        {

                            HeightTmp = 0;

                            eValue.HasMorePages = true;

                            return; //重新触发OnPrintPage事件

                        }

                    }

                   

                    using (SolidBrush solidBrush = new SolidBrush(CellPrint.BackColor))

                    {

                        RectangleF rectF1 = new RectangleF(PoX, PoY, CellPrint.Width, CellPrint.Height);

                        eValue.Graphics.FillRectangle(solidBrush, rectF1);

                        using (Pen pen = new Pen(Color.Black, 1))

                        eValue.Graphics.DrawRectangle(pen, System.Drawing.Rectangle.Round(rectF1));//画出单个数据的方框格子

                        solidBrush.Color = CellPrint.ForeColor;

                        eValue.Graphics.DrawString(CellPrint.FormattedValue, CellPrint.Font, solidBrush, new System.Drawing.Point(PoX + 2, PoY + 3));//在方框中画出数据

                    }

                    WidthTmp = CellPrint.Width;

                    HeightTmp = CellPrint.Height;

                    RowIndex = CellPrint.RowIndex;

                    PrintDataGridView11.printRowCount++;

                }

                PoY = PoY + HeightTmp; //全部打印完后不再分页

                eValue.HasMorePages = false;

                PrintDataGridView11.IsPrint = true;

            }

            catch

            {

                eValue.HasMorePages = false;

                PrintDataGridView11.IsPrint = true;

                throw;//抛出异常

            }

 

        }

 

        /// 

        /// 将DataGridView控件内容转变到 CellPrintList

        /// 

        /// DataGridView控件

        /// 是否包括列标题

        ///

        private static void DataGridViewCellVsList(DataGridView dataGridView, bool includeColumnText)

        {

            CellPrintList.Clear();

            try

            {

                int rowsCount = dataGridView.Rows.Count;

                int colsCount = dataGridView.Columns.Count;

 

                //最后一行是供输入的行时,不用读数据。

                if (dataGridView.Rows[rowsCount - 1].IsNewRow)

                    rowsCount--;

                //包括列标题

                if (includeColumnText)

                {

                    for (int columnsIndex = 0; columnsIndex < colsCount; columnsIndex++)

                    {

                        if (dataGridView.Columns[columnsIndex].Visible)

                        {

                            DataGridViewCellPrint CellPrint = new DataGridViewCellPrint();

                            CellPrint.FormattedValue = dataGridView.Columns[columnsIndex].HeaderText;

                            CellPrint.RowIndex = 0;

                            CellPrint.ColumnIndex = columnsIndex;

                            CellPrint.Font = dataGridView.Columns[columnsIndex].HeaderCell.Style.Font;

                            CellPrint.BackColor = dataGridView.ColumnHeadersDefaultCellStyle.BackColor;

                            CellPrint.ForeColor = dataGridView.ColumnHeadersDefaultCellStyle.ForeColor;

                            CellPrint.Width = dataGridView.Columns[columnsIndex].Width;

                            CellPrint.Height = dataGridView.ColumnHeadersHeight;

                            CellPrintList.Add(CellPrint); //add:每次添加一个数据

                        }

                    }

                }

                //读取单元格数据

                for (int rowIndex = 0; rowIndex < rowsCount; rowIndex++)

                {

                    for (int columnsIndex = 0; columnsIndex < colsCount; columnsIndex++)

                    {

                        if (dataGridView.Columns[columnsIndex].Visible)

                        {

                            DataGridViewCellPrint CellPrint = new DataGridViewCellPrint();

                            CellPrint.FormattedValue = dataGridView.Rows[rowIndex].Cells[columnsIndex].FormattedValue.ToString();

                            if (includeColumnText)

                                CellPrint.RowIndex = rowIndex + 1;//假如包括列标题则从行号1开始

                            else

                                CellPrint.RowIndex = rowIndex;

                            CellPrint.ColumnIndex = columnsIndex;

                            CellPrint.Font = dataGridView.Rows[rowIndex].Cells[columnsIndex].Style.Font;

                            System.Drawing.Color TmpColor = System.Drawing.Color.Empty;

                            if (System.Drawing.Color.Empty != dataGridView.Rows[rowIndex].Cells[columnsIndex].Style.BackColor)

                                TmpColor = dataGridView.Rows[rowIndex].Cells[columnsIndex].Style.BackColor;

                            else if (System.Drawing.Color.Empty != dataGridView.Rows[rowIndex].DefaultCellStyle.BackColor)

                                TmpColor = dataGridView.Rows[rowIndex].DefaultCellStyle.BackColor;

                            else

                                TmpColor = dataGridView.DefaultCellStyle.BackColor;

                            CellPrint.BackColor = TmpColor;

                            TmpColor = System.Drawing.Color.Empty;

                            if (System.Drawing.Color.Empty != dataGridView.Rows[rowIndex].Cells[columnsIndex].Style.ForeColor)

                                TmpColor = dataGridView.Rows[rowIndex].Cells[columnsIndex].Style.ForeColor;

                            else if (System.Drawing.Color.Empty != dataGridView.Rows[rowIndex].DefaultCellStyle.ForeColor)

                                TmpColor = dataGridView.Rows[rowIndex].DefaultCellStyle.ForeColor;

                            else

                                TmpColor = dataGridView.DefaultCellStyle.ForeColor;

                            CellPrint.ForeColor = TmpColor;

                            CellPrint.Width = dataGridView.Columns[columnsIndex].Width;

                            CellPrint.Height = dataGridView.Rows[rowIndex].Height;

                            CellPrintList.Add(CellPrint);

                        }

                    }

                }

            }

            catch { throw; }

        }

 

        private class DataGridViewCellPrint

        {

            /// <summary>

            /// 格式化的单元格的值

            /// </summary>

            private string _FormattedValue = "";

            private int _RowIndex = -1;

            private int _ColumnIndex = -1;

            private System.Drawing.Color _ForeColor = System.Drawing.Color.Black;

            private System.Drawing.Color _BackColor = System.Drawing.Color.White;

            private int _Width = 100;

            private int _Height = 23;

            private System.Drawing.Font _Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,((byte)(134)));

            /// <summary>

            /// 获取或设置单元格的字体。

            ///<summary>

            public System.Drawing.Font Font

            {

                set { if (null != value) _Font = value; }

                get { return _Font; }

            }

            ///  <summary>

            /// 获取为显示进行格式化的单元格的值。

            ///  <summary>

            public string FormattedValue

            {

                set { _FormattedValue = value; }

                get { return _FormattedValue; }

            }

            ///  <summary>

            /// 获取或设置列的当前宽度(以像素为单位)。默认值为 100。

            ///  <summary>

            public int Width

            {

                set { _Width = value; }

                get { return _Width; }

            }

            /// <summary>

            /// 获取或设置列标题行的高度(以像素为单位)。默认值为 23。

            ///  <summary>

            public int Height

            {

                set { _Height = value; }

                get { return _Height; }

            }

            ///  <summary>

            /// 获取或设置行号。

            /// <summary>

            public int RowIndex

            {

                set { _RowIndex = value; }

                get { return _RowIndex; }

            }

            ///  <summary>

            /// 获取或设置列号。

            ///  <summary>

            public int ColumnIndex

            {

                set { _ColumnIndex = value; }

                get { return _ColumnIndex; }

            }

            ///  <summary>

            /// 获取或设置前景色。

            ///  <summary>

            public System.Drawing.Color ForeColor

            {

                set { _ForeColor = value; }

                get { return _ForeColor; }

            }

            /// <summary>

            /// 获取或设置背景色。

            ///  <summary>

            public System.Drawing.Color BackColor

            {

                set { _BackColor = value; } //只写

                get { return _BackColor; }  //只读

            }

 

        }

 

    }

 

转载于:https://www.cnblogs.com/jinyuttt/archive/2012/02/21/2361245.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
注意:本3.0版控件过于老旧,该控件最新版本为4.7版,您可以到http://myyouping.download.csdn.net/免费下载,也是完全免费的,没有任何功能及使用时间限制,0积分下载。我将3.0版控件的下载分提为10分,是希望大家使用最新版的控件控件特色: 1、超强大的DataGridView打印功能,不光可以以多种形式(普通打印、分栏打印、跨页打印、工资条打印打印DataGridView表格,基本上能完全按DataGridView控件本身设置的格式如字体、字号、背景颜色、前景颜色、单元格对齐方式等打印出来,文字图像都可以打印,而且是完全根据表格当前的显示顺序进行打印的,如果您在使用时调整了列的顺序,刷新打印后就会按调整后的列显示顺序进行打印(这是网上很多DataGridView打印控件没有实现的),基本上做到了所见即所得的打印。 2、强大的文本打印输出功能,打印文本时,如果需要,控件会自动换行或换页打印输出。 3、支持同一文档多种版面格式打印(类似于Word的节的功能):对同一份文档,不同的页面可以设置不同的格式(纸张大小、纸张方向、页边距),只需要在新增一页时在NewPage方法指定要使用的页面格式即可,使用非常简单。 4、报表功能。本控件允许将当前打印预览的内容保存为报表文件,以后使用本控件重新打开该报表文件即可重现原来保存报表时的打印内容。 5、打印方案保存与读取功能。可以将当前打印参数保存为打印方案文件,或都从保存的打印方案文件读取打印参数。 6、水印功能。根据需要,可以在页面打印或不打印以半透明空心文字打印水印。 7、特殊文字效果功能。本版控件具有打印浮雕文字、阴影文字、空心文字、块文字的功能,效果非常不错。 8、页眉页脚既可打印文字,也可打印图像,或者即打印图像又打印输出文字。 9、图像打印输出功能。 本控件包括两个打印组件:DGVprint打印组件与VB2008print打印组件。VB2008Print为通用打印控件(为可视化组件),可以混合打印一个或多个DataGridView表格、DataGrid表格与文字、图片等内容;而DGVprint是以VB2008Print为基础开发的(为非可视化组件),为专用的表格打印控件,可以很方便打印单个DatatGridview、DataGrid、DataTable表格的内容,并提供打印时的可视化设置界面,可设置表格打印方式(普通打印、分栏打印、跨页打印、工资条打印)、标题内容及字体、页眉页脚、选择要打印的表格列、列顺序调整及列格式设置、更改列标题名称等,使用非常方便,功能非常强大。 与本控件的上一个版本2.1版相比,本版控件新增功能如下: VB2008Print打印组件新增功能: 1、多种特效文字打印输出功能,能打印的特效文字有 空心文字、浮雕文字、块文字、阴影文字等。 2、水印打印功能。可以在面页以半透明空心文字的形式打印背景水印,只需要简单设置控件的WaterMarkText等几个以WaterMark开头的属性即可,程序会自动打印水印。 3、PrintDGV函数可直接使用DGVPrint组件保存的打印方案文件打印输出DataGridView表格, 4、在页眉页脚不光可以打印文本,还可以打印图像,文本与图像可同时打印(即在图像上显示文本)。此外,页眉页脚文字可以换行打印了,页面的左边也右边距也可以打印内容了(调用相应的函数PrintLeft与PrintRight实现)。 5、改进DrawText函数输出文本的功能,现在,即便调用没有指定打印区域或打印宽度的DrawText函数输出文本,打印输出时控件也会智能换行和换页(原版本是需要指定打印宽度才能自动换行换页打印的) 6、改进DrawImage与DrawCellImage输出图像功能,如果图像比较小(小于打印区域大小),可以不进行放大打印。(但如果图像大于打印区域的话,还是采用整体缩小打印,而不是区域剪裁打印)。 7、增加IsShowPrintStatusDialog属性,指示在发送到打印打印时,是否显示一个指示正在打印的状态窗口(可以取消打印),为TRUE表示要显示,为False表示不显示。 8、改进页眉页脚事件,将原来的HeaderOut与FooterOut统一为HeaderFooterOut事件,在该事件,您可以调用PrintFooter、PrintHeader、PrintLeft、PrintRight函数分别打印上下左右的页眉。(PrintLeft与PrintRight函数为新增加的,用于在左边与右边页边距处输出内容) DGVPrint打印组件新增功能: 1、打印方案保存与读取功能。本版控件可以将您的可视化设置(包括列格式设置等)全部保存为打印方案文件(文本文件,您可以用记事本打开并修改),并有读取方案文件的功能,不再需要每次都进行打印格式设置了,一劳永逸! 2、直接调用打印方案文件打印功能。您不光可以设计DGVPrint打印组件的属性来进行打印,还可以直接调用DGVPrint组件保存的打印方案文件,直接利用保存的方案文件的参数进行打印预览输出。 3、新增在可视化打印参数设置界面的列标题重命名功能,可能修改列标题要打印的名字。 4、水印打印功能。如果水印文本设置为空,则不打印水印。 5、导出数据成Excel功能。暂未提供该功能的函数接口,只在打印参数设置窗口增加了一个数据导出的按钮,可以将当前要打印DataGridView的内容导出成Excel文件。该功能以后会进一步完善。 6、在进行页眉页脚文本设置时,可以用 [页码] 代表要输出的当前页码,用 [总页数] 代表要输出总页数,控件在进行输出时,会自动将其转换为对应的页码与文档总页数。
DataGridView打印控件和.NET打印控件5.6版(含报表模板设计组件)2014年6月22日修改完成,完全免费,在.NET2.0及以上环境下都可以使用(VB打印、C#打印都是可以的),有帮助文档与使用实例。 与上一版本的5.5版相比,新控件5.6版的主要更改如下: 1、增加了一个新打印组件SimpleReport组件,该组件与DGVPrint组件一样在运行时可进行打印参数设置,但比DGVPrint组件功能更强大,可以自动管理多个打印方案,在打印预览时可以自由在各个打印方案之间切换;可以在打印参数设置窗口动态定义多表头,还可以像EasyReport组件一样设置和使用参数变量(具体使用效果参见实例程序); 2、解决了EasyReport组件在插入变量参数时,变量参数未排序导致查找不方便的问题; 3、在打印预览界面添加了简单的双面打印功能。(打印预览界面工具栏的“打印\双面打印”菜单); 4、其他一些完善,比如DGVPrint组件设置的行高无效等问题。 本控件特色: 1、强大的DataGridView打印功能,不仅可以以多种形式(普通打印、分栏打印、跨页打印、工资条打印打印DGV表格,基本上能完全按DGV控件本身设置的格式如字体、字号、背景颜色、前景颜色、单元格对齐方式等打印出来,文字图像都可以打印,而且是完全根据表格当前的显示顺序进行打印的,基本上做到了所见即所得的打印。 2、报表设计功能。报表模板设计组件EasyReport与WebEasyReport组件可以设计普通报表、分组报表、套打模板等,分别以DataGridView为数据源。控件的位置以毫米为计量单位,定位准确,很适合套打单据设计。 3、强大的图表打印功能。5.2版控件新增了一个Chartlet的组件,使用非常方便,可以生成柱形图、饼图、折线图等多种图形,而且可以设置2D或3D效果,既可以在打印控件打印出来,也可以在Graphics对象显示。 4、分组汇总打印DataGridVeiw功能,每组还可以自动换新页打印,还可以自动增加行号。 5、强大的文本打印输出功能,控件提供多个文本打印重载函数,打印文本时,如果需要,控件会自动换行和换页打印输出。还增加了以指定行间距及字符间距打印文本的功能,可以用固定行距,也可以用单倍或多倍行距打印文本。 6、强大的绘图功能,基本上.NET的GDI+的绘图函数(如直线、矩形、路径、多边形、曲线等)都有,只有个别函数的名称有点区别。 7、支持同一文档多种版面格式打印(类似于Word的节的功能):对同一份文档,不同的页面可以设置不同的格式(纸张大小、纸张方向、页边距),只需要在新增一页时在NewPage方法指定要使用的页面格式即可,使用非常简单。 8、报表文件保存功能。本控件允许将当前打印预览的内容保存为报表文件,以后使用本控件重新打开该报表文件即可重现原来保存报表时的打印内容。 9、Excel导出功能,可以将DataGridView和GridView导出为Excel文件,5.2版控件还增加了不依赖Office的导出Excel功能,而且速度非常快,5.4版还增加了合并单元格的导出功能。 10、打印DataGridView时的打印方案保存与读取功能。可以将当前打印参数保存为打印方案文件,或者从保存的打印方案文件读取打印参数。 11、水印打印功能。根据需要,可以在页面打印或不打印以半透明空心文字打印水印。 12、强大的容器控件打印功能(DrawPanel函数)。借助该函数,您只需要在您的容器控件设计好要打印的内容及打印内容的相对位置,控件轻松帮你打印出来(如果超过一页,控件会自动换页续打)。 13、特殊文字效果打印功能。控件具有打印浮雕文字、阴影文字、空心文字、块文字的功能。 14、页眉页脚既可打印文字,也可打印图像,或者即打印图像又打印输出文字。 15、图像与图标打印输出功能。 16、多表头(跨行跨列的复杂表头)打印功能,多表头组件支持多表头显示与打印、单元格内容的合并显示、打印与导出。 17、自定义纸张支持功能。 18、纸张背景图片设置打印功能。 19、.NET4.0支持功能(是单独的一个文件)。 20、直接打印窗口的TreeView控件功能。 21、打印窗口的ListView功能。 22、RichTextBox控件的RTF文本打印功能。 23、斜线表头打印功能(5.4版新增)。 24、二维码打印功能(5.5版本增加)。 25、5.6版新增的SimpleReport组件允许您在一个方案文件管理多个打印方案,在打印预览时能自由在各个打印方案之间切换。 我将持续改进该控件,并将不断推出控件的新版本,要查看或下载控件的升级版本,请登陆网站:http://myyouping.download.csdn.n
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值