在好例子网,分享、交流、成长!
您当前所在位置:首页C# 开发实例C#语言基础 → winfrom打印DataSet 数据 实例源码下载

winfrom打印DataSet 数据 实例源码下载

C#语言基础

下载此实例
  • 开发语言:C#
  • 实例大小:0.14M
  • 下载次数:58
  • 浏览次数:495
  • 发布时间:2016-11-08
  • 实例类别:C#语言基础
  • 发 布 人:xingn1
  • 文件格式:.7z
  • 所需积分:2
 相关标签: 打印 WinFrom

实例介绍

【实例简介】

【实例截图】

【核心代码】

using System;
using System.Collections;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;

namespace OmnipotentPrint
{
    public class PrintClass
    {
        #region 全局变量

        public static bool PageScape;
        private readonly DataGridView dataGridView;
        private readonly PageSetupDialog pageSetupDialog;
        private readonly PrintDocument printDocument;
        private readonly PrintPreviewDialog printPreviewDialog;
        private readonly string title = "";

        private readonly int titleSize = 16;
        public int AlignmentSgin = -1;
        public int BottomMargin = 80;
        public bool Boundary;
        public Brush Brush = new SolidBrush(Color.Black);
        public Brush BrushCellBack = new SolidBrush(Color.White);
        public Brush BrushCellFont = new SolidBrush(Color.Black);
        public Brush BrushHeaderBack = new SolidBrush(Color.White);
        public Brush BrushHeaderFont = new SolidBrush(Color.Black);
        public Font CellFont = new Font("arial", 9);
        public int CellLeftMargin = 40;
        public int CellTopMargin = 6;
        public int CenterAlignment;
        public int ColGap = 5;
        public Font FooterFont = new Font("arial", 8);
        public int HeaderAlignment;
        public Font HeaderFont = new Font("arial", 9, FontStyle.Bold);
        public int HeaderHeight = 30;

        public bool IsAutoPageRowCount = true;
        public bool IsEveryPagePrintTitle;
        public int LeftAlignment = 50;
        public int LeftMargin = 50;
        public Color LineColor = Color.Black;

        public bool LineLeft = true;
        public bool LineUnit = true;
        public bool LineUp = true;
        public bool NeedPrintPageIndex = true;
        public bool PageAspect;
        public int PageRowCount = 7;
        public int PageSheet;
        public int PrintPageHeight = 1169;
        public int PrintPageWidth = 827;
        public int RightAlignment = 50;
        public int RowGap = 23;
        public Font TextFont = new Font("arial", 10);
        public Font TitleFont = new Font("arial", 14);
        public int TopMargin = 60;
        public Font UnderlineFont = new Font("arial", 8);
        public int UnitAlignment;
        public Font UplineFont = new Font("arial", 9, FontStyle.Bold);
        public int X;
        private int currentPageIndex;
        private int pageCount;
        private int rowCount;
        private int xOffset;

        #endregion

        #region 打印信息的初始化

        public PrintClass(DataGridView dataGridView, string title, int titleSize, int pageSheet, bool landScape)
        {
            this.title = title;
            this.titleSize = titleSize;
            this.dataGridView = dataGridView;
            PageSheet = pageSheet;

            printDocument = new PrintDocument();
            pageSetupDialog = new PageSetupDialog();
            pageSetupDialog.Document = printDocument;
            printPreviewDialog = new PrintPreviewDialog();
            printPreviewDialog.Document = printDocument;
            printPreviewDialog.FormBorderStyle = FormBorderStyle.Fixed3D;

            if (pageSheet >= 0)
            {
                if (landScape)
                {
                    printDocument.DefaultPageSettings.Landscape = landScape;
                }
                else
                {
                    printDocument.DefaultPageSettings.Landscape = landScape;
                }
            }
            pageSetupDialog.Document = printDocument;
            printDocument.PrintPage  = PrintPage;
        }

        #endregion

        #region 纸张大小的设置

        public string PageSize(int n)
        {
            string pageSize = "";
            switch (n)
            {
                case 1:
                    {
                        pageSize = "A5";
                        PrintPageWidth = 583;
                        PrintPageHeight = 827;
                        break;
                    }
                case 2:
                    {
                        pageSize = "A6";
                        PrintPageWidth = 413;
                        PrintPageHeight = 583;
                        break;
                    }
                case 3:
                    {
                        pageSize = "B5(ISO)";
                        PrintPageWidth = 693;
                        PrintPageHeight = 984;
                        break;
                    }
                case 4:
                    {
                        pageSize = "B5(JIS)";
                        PrintPageWidth = 717;
                        PrintPageHeight = 1012;
                        break;
                    }
                case 5:
                    {
                        pageSize = "Double Post Card";
                        PrintPageWidth = 583;
                        PrintPageHeight = 787;
                        break;
                    }
                case 6:
                    {
                        pageSize = "Envelope #10";
                        PrintPageWidth = 412;
                        PrintPageHeight = 950;
                        break;
                    }
                case 7:
                    {
                        pageSize = "Envelope B5";
                        PrintPageWidth = 693;
                        PrintPageHeight = 984;
                        break;
                    }
                case 8:
                    {
                        pageSize = "Envelope C5";
                        PrintPageWidth = 638;
                        PrintPageHeight = 902;
                        break;
                    }
                case 9:
                    {
                        pageSize = "Envelope DL";
                        PrintPageWidth = 433;
                        PrintPageHeight = 866;
                        break;
                    }
                case 10:
                    {
                        pageSize = "Envelope Monarch";
                        PrintPageWidth = 387;
                        PrintPageHeight = 750;
                        break;
                    }
                case 11:
                    {
                        pageSize = "ExeCutive";
                        PrintPageWidth = 725;
                        PrintPageHeight = 1015;
                        break;
                    }
                case 12:
                    {
                        pageSize = "Legal";
                        PrintPageWidth = 850;
                        PrintPageHeight = 1400;
                        break;
                    }
                case 13:
                    {
                        pageSize = "Letter";
                        PrintPageWidth = 850;
                        PrintPageHeight = 1100;
                        break;
                    }
                case 14:
                    {
                        pageSize = "Post Card";
                        PrintPageWidth = 394;
                        PrintPageHeight = 583;
                        break;
                    }
                case 15:
                    {
                        pageSize = "16K";
                        PrintPageWidth = 775;
                        PrintPageHeight = 1075;
                        break;
                    }
                case 16:
                    {
                        pageSize = "8.5x13";
                        PrintPageWidth = 850;
                        PrintPageHeight = 1300;
                        break;
                    }
            }
            return pageSize;
        }

        #endregion

        #region 页边距的设置

        public void PrintSetUp(string[] margins)
        {
            if (margins[0] == "true")
            {
                TopMargin = Int32.Parse(margins[1]);
                LeftMargin = Int32.Parse(margins[2]);
                BottomMargin = Int32.Parse(margins[3]);
                AlignmentSgin = -1;
            }
        }

        #endregion

        #region 文字的位置

        private int AlignmentMode(int cellWidth, int stringWidth, int colLeftPadding, int alignment)
        {
            int enumAlignment = 0;
            switch (alignment)
            {
                case 0:
                    {
                        enumAlignment = colLeftPadding;
                        break;
                    }
                case 1:
                    {
                        enumAlignment = ((cellWidth - stringWidth)/2);
                        break;
                    }
                case 2:
                    {
                        enumAlignment = cellWidth - stringWidth - colLeftPadding;
                        break;
                    }
            }
            return enumAlignment;
        }

        #endregion

        #region 页的打印事件

        private void PrintPage(object sender, PrintPageEventArgs e)
        {
            PrintPageWidth = e.PageBounds.Width;
            PrintPageHeight = e.PageBounds.Height;
            if (IsAutoPageRowCount)
                PageRowCount = ((PrintPageHeight - TopMargin - titleSize - HeaderFont.Height - HeaderHeight -
                                 BottomMargin)/RowGap);
            pageCount = (rowCount/PageRowCount);
            pageSetupDialog.AllowOrientation = true;
            if (rowCount%PageRowCount > 0)
                pageCount  ;
            int colCount = 0;
            int Y = TopMargin;
            string cellValue = "";
            int startRow = currentPageIndex*PageRowCount;
            int endRow = startRow   PageRowCount < rowCount ? startRow   PageRowCount : rowCount;
            int currentPageRowCount = endRow - startRow;
            colCount = dataGridView.ColumnCount;
            X = LeftMargin;

            int reportWidth = 0;
            for (int j = 0; j < colCount; j  )
            {
                if (dataGridView.Columns[j].Width > 0)
                {
                    reportWidth  = dataGridView.Columns[j].Width   ColGap;
                }
            }

            if (AlignmentSgin >= -1)
            {
                int headerWidth = (int) (e.Graphics.MeasureString(title, TitleFont).Width);
                switch (AlignmentSgin)
                {
                    case 0:
                        {
                            X = LeftAlignment;
                            LeftMargin = X;
                            if (headerWidth > reportWidth)
                            {
                                xOffset = LeftMargin;
                            }
                            else
                            {
                                xOffset = (PrintPageWidth - (PrintPageWidth - 50)   (reportWidth - headerWidth)/2);
                            }
                            break;
                        }
                    case 1:
                        {
                            X = (PrintPageWidth - reportWidth)/2;
                            LeftMargin = X;
                            xOffset = ((PrintPageWidth - headerWidth)/2);
                            break;
                        }
                    case 2:
                        {
                            X = PrintPageWidth - reportWidth - RightAlignment;
                            LeftMargin = X;
                            if (headerWidth > reportWidth)
                            {
                                xOffset = (PrintPageWidth - headerWidth);
                            }
                            else
                            {
                                xOffset = (PrintPageWidth - 50 - reportWidth   (reportWidth - headerWidth)/2);
                            }
                            break;
                        }
                    case -1:
                        {
                            if (headerWidth > reportWidth)
                            {
                                if ((headerWidth - reportWidth)/2 < LeftMargin)
                                {
                                    xOffset = (LeftMargin - (headerWidth - reportWidth)/2);
                                }
                                else
                                {
                                    xOffset = LeftMargin;
                                }
                            }
                            else
                                xOffset = (X   (reportWidth - headerWidth)/2);
                            break;
                        }
                }
            }

            if (currentPageIndex == 0 || IsEveryPagePrintTitle)
            {
                e.Graphics.DrawString(title, TitleFont, Brush, xOffset, Y);
                Y  = titleSize;
            }
            Y  = RowGap;

            Point headUp = new Point(X, Y);
            Point headDown = new Point(reportWidth, HeaderHeight);
            DrawRectangle(BrushHeaderBack, headUp, headDown, e.Graphics);
            headUp = new Point(X, Y   HeaderHeight);
            headDown = new Point(reportWidth, (endRow - startRow)*RowGap);
            DrawRectangle(BrushCellBack, headUp, headDown, e.Graphics);
            if (LineLeft)
            {
                DrawLine(new Point(X, Y), new Point(X, Y   currentPageRowCount*RowGap   HeaderHeight), e.Graphics, 0);
            }
            for (int j = 0; j < colCount; j  )
            {
                int colWidth = dataGridView.Columns[j].Width;
                if (colWidth > 0)
                {
                    cellValue = dataGridView.Columns[j].HeaderText;
                    int height = AlignmentMode(dataGridView.Columns[j].Width,
                                               (int) (e.Graphics.MeasureString(cellValue, CellFont).Width),
                                               CellLeftMargin, HeaderAlignment);
                    e.Graphics.DrawString(cellValue, HeaderFont, BrushHeaderFont, X   height, Y   CellTopMargin);
                    X  = colWidth   ColGap;
                    if (LineUnit)
                    {
                        DrawLine(new Point(X, Y), new Point(X, Y   currentPageRowCount*RowGap   HeaderHeight),
                                 e.Graphics, 0);
                    }
                    if (LineLeft && (j == (colCount - 1)))
                    {
                        DrawLine(new Point(X, Y), new Point(X, Y   currentPageRowCount*RowGap   HeaderHeight),
                                 e.Graphics, 0);
                    }
                    int nnp = Y   currentPageRowCount*RowGap   HeaderHeight;
                }
            }
            int rightbound = X;
            if (LineUp)
            {
                DrawLine(new Point(LeftMargin, Y), new Point(rightbound, Y), e.Graphics, 0);
            }
            headUp = new Point(LeftMargin, Y);
            Y  = HeaderHeight;
            for (int i = startRow; i < endRow; i  )
            {
                X = LeftMargin;
                for (int j = 0; j < colCount; j  )
                {
                    if (dataGridView.Columns[j].Width > 0)
                    {
                        cellValue = dataGridView.Rows[i].Cells[j].Value.ToString();
                        int Ua = AlignmentMode(dataGridView.Columns[j].Width,
                                               (int) (e.Graphics.MeasureString(cellValue, CellFont).Width),
                                               CellLeftMargin, UnitAlignment);
                        e.Graphics.DrawString(cellValue, CellFont, BrushHeaderFont, X   Ua, Y   CellTopMargin);
                        X  = dataGridView.Columns[j].Width   ColGap;
                        Y = Y   RowGap*(cellValue.Split(new[] {'\r', '\n'}).Length - 1);
                    }
                }
                if (LineUnit)
                {
                    DrawLine(new Point(LeftMargin, Y), new Point(rightbound, Y), e.Graphics, 0);
                }
                if (Boundary && i == startRow)
                {
                    DrawLine(new Point(LeftMargin, Y), new Point(rightbound, Y), e.Graphics, 1);
                }
                Y  = RowGap;
            }
            if (LineUp)
            {
                DrawLine(new Point(LeftMargin, Y), new Point(rightbound, Y), e.Graphics, 0);
            }
            currentPageIndex  ;
            if (NeedPrintPageIndex)
            {
                e.Graphics.DrawString("共 "   pageCount.ToString()   " 页   第 "   currentPageIndex.ToString()   " 页",
                                      FooterFont, Brush, PrintPageWidth - 200,
                                      (PrintPageHeight - BottomMargin/2 - FooterFont.Height));
            }
            if (currentPageIndex < pageCount)
            {
                e.HasMorePages = true;
            }
            else
            {
                e.HasMorePages = false;
                currentPageIndex = 0;
            }
        }

        #endregion

        #region 绘制边线

        private void DrawLine(Point startPoint, Point endPoint, Graphics graphics, int flag)
        {
            int width = 1;
            if (flag == 1)
            {
                width = 2;
            }
            Pen pen = new Pen(LineColor, width);
            graphics.DrawLine(pen, startPoint, endPoint);
        }

        #endregion

        #region 绘制填充的矩形框

        private void DrawRectangle(Brush backColor, Point point1, Point point2, Graphics graphics)
        {
            graphics.FillRectangle(backColor, point1.X, point1.Y, point2.X, point2.Y);
        }

        #endregion

        #region 显示打印预览窗体

        public void Print()
        {
            rowCount = 0;
            string paperName = PageSize(PageSheet);
            PageSettings storePageSetting = new PageSettings();
            foreach (PaperSize paperSize in printDocument.PrinterSettings.PaperSizes)
            {
                if (paperName == paperSize.PaperName)
                {
                    storePageSetting.PaperSize = paperSize;
                }
            }

            if (dataGridView.DataSource.GetType().ToString() == "System.Data.DataTable")
            {
                rowCount = ((DataTable) dataGridView.DataSource).Rows.Count;
            }
            else if (dataGridView.DataSource.GetType().ToString() == "System.Collections.ArrayList")
            {
                rowCount = ((ArrayList) dataGridView.DataSource).Count;
            }
            try
            {
                printDocument.DefaultPageSettings.Landscape = PageScape;
                pageSetupDialog.Document = printDocument;

                printPreviewDialog.ShowDialog();
            }
            catch (Exception e)
            {
                throw new Exception("Printer Error."   e.Message);
            }
        }

        #endregion
    }
}

标签: 打印 WinFrom

实例下载地址

winfrom打印DataSet 数据 实例源码下载

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警