C#使用Windows打印组件PrintDocument等打印DataGridView中的数据

本文详细介绍了如何使用C#中的Windows打印组件PrintDocument来打印DataGridView中的数据,包括预览打印内容、直接打印的功能,以及自定义类 DataGridViewPrinter 的使用,该类用于绘制并分页打印数据。通过示例代码展示了PrintDocument的PrintPage事件处理程序和自定义类的方法,实现了在预览和实际打印时的分页处理。
摘要由CSDN通过智能技术生成

 

在打印按钮的点击事件中调用下面的方法:

//printPreviewDialog1打印,可预览打印内容
        private void Print1()
        {
            if (System.Windows.Forms.MessageBox.Show("是否要预览打印文档", "打印预览",System.Windows.Forms.MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                this.printPreviewDialog1.UseAntiAlias = true;
                this.printPreviewDialog1.Document = this.printDocument1;

                printer1 = new DataGridViewPrinter(dataGridView1, printDocument1, true, true,
                "表头1", "表头2", new Font("宋体", 30, FontStyle.Regular),
                new Font("宋体", 30, FontStyle.Regular), Color.Black);
                printPreviewDialog1.ShowDialog();
                
            }
            else
            {
                this.printDocument1.Print();//不预览,直接打印
            }
        }

printDocument1的PrintPage事件处理程序:

private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
        {
            
            //e.HasMorePage为真,系统会自动调用printDocument1_PrintPage方法。实现在预览时的分页
            if (printer1.DrawDataGridView(e.Graphics))
                e.HasMorePages = true;
            else
                e.HasMorePages = false;
            
        }

 

自定义类,绘制打印内容(以网上的代码为基础,做了一些修改,添加了一些解释,想看原来的代码请点击这里!):

using System;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Printing;
using System.Data;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlClient;
using System.ComponentModel;

namespace v2.print
{
    class DataGridViewPrinter
    {
        //定义成员变量
        private DataGridView TheDataGridView; 
        private PrintDocument ThePrintDocument; 
        private bool IsCenterOnPage; 
        private bool IsWithTitle; 
        private string TheTitleText; 
        private string TheTitleTextTwo;
        private Font TheTitleFont; 
        private Font TheTitleFontTwo;
        private Color TheTitleColor;

        static int CurrentRow; 

        static int PageNumber;
        static int PageNumberson;
        static int lastnumber;

        private int PageWidth;
        private int PageHeight;
        private int LeftMargin;
        private int TopMargin;
        private int RightMargin;
        private int BottomMargin;

        private float CurrentY; 

        private float RowHeaderHeight;
        private List<float> RowsHeight;
        private List<float> ColumnsWidth;
        private float TheDataGridViewWidth;

        
        private List<int[]> mColumnPoints;
        private List<float> mColumnPointsWidth;
        private int mColumnPoint;
       

        //构造函数
        public DataGridViewPrinter(DataGridView aDataGridView, PrintDocument aPrintDocument, 
            bool CenterOnPage, bool WithTitle, string aTitleText, 
            string bTitleText, Font aTitleFont, Font bTitleFont, 
            Color aTitleColor)
        {
            TheDataGridView = aDataGridView;
            ThePrintDocument = aPrintDocument;
            IsCenterOnPage = CenterOnPage;
            IsWithTitle = WithTitle;
            TheTitleText = aTitleText;
            TheTitleTextTwo = bTitleText;
            TheTitleFont = aTitleFont;
            TheTitleFontTwo = bTitleFont;
            TheTitleColor = aTitleColor;
            

            PageNumber = 0;
            PageNumberson = 0;

            RowsHeight = new List<float>();
            ColumnsWidth = new List<float>();

            mColumnPoints = new List<int[]>();
            mColumnPointsWidth = new List<float>();

            //利用ThePrintDocument.DefaultPageSettings.PaperSize获取页面的宽度和高度
            if (!ThePrintDocument.DefaultPageSettings.Landscape)
            {
                PageWidth = ThePrintDocument.DefaultPageSettings.PaperSize.Width;
                PageHeight = ThePrintDocument.DefaultPageSettings.PaperSize.Height;
            }
            else
            {
                PageHeight = ThePrintDocument.DefaultPageSettings.PaperSize.Width;
                PageWidth = ThePrintDocument.DefaultPageSettings.PaperSize.Height;
            }

            // 计算页边距
            LeftMargin = ThePrintDocument.DefaultPageSettings.Margins.Left;
            TopMargin = ThePrintDocument.DefaultPageSettings.Margins.Top;
            RightMargin = ThePrintDocument.DefaultPageSettings.Margins.Right;
            BottomMargin = ThePrintDocument.DefaultPageSettings.Margins.Bottom;

           
            CurrentRow = 0;
            lastnumber = 0;
        }

        //构造函数,初始化变量
        //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


        private float intergered(float num)
        {
            float num_interger;
            int point = 0;//小数点的位置
            string num_temp = num.ToString();
            for (int i = 0; i <

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值