分享

C#打印DataGridView的详细代码

 羊玉wngbx 2018-09-21
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Text;  
  7. using System.Windows.Forms;  
  8. using System.Drawing.Printing;  
  9. using System.IO;  
  10.   
  11. namespace NetCsharp  
  12. {  
  13.   
  14.     ///  
  15.     /// 实现DataGridView的打印  
  16.     ///  
  17.     public class PrintDataGridView  
  18.     {  
  19.         private static List CellPrintList = new List();  
  20.   
  21.         private static int printRowCount = 0;  
  22.   
  23.         private static bool IsPrint = true;  
  24.         private static bool IsRole = true;  
  25.         private static int PoXTmp = 0;  
  26.         private static int PoYTmp = 0;  
  27.         private static int WidthTmp = 0;  
  28.         private static int HeightTmp = 0;  
  29.         private static int RowIndex = 0;  
  30.   
  31.   
  32.         ///  
  33.         /// 打印DataGridView控件  
  34.         ///  
  35.         /// DataGridView控件  
  36.         /// 是否包括列标题  
  37.         /// 为 System.Drawing.Printing.PrintDocument.PrintPage 事件提供数据。  
  38.         /// 起始X坐标  
  39.         /// 起始Y坐标  
  40.         public static void Print(DataGridView dataGridView, bool includeColumnText, PrintPageEventArgs e, ref int PoX, ref int PoY)  
  41.         {  
  42.             try  
  43.             {  
  44.                 if (PrintDataGridView.IsPrint)  
  45.                 {  
  46.                     PrintDataGridView.printRowCount = 0;  
  47.                     PrintDataGridView.IsPrint = false;  
  48.                     PrintDataGridView.DataGridViewCellVsList(dataGridView, includeColumnText);  
  49.                     if (0 == PrintDataGridView.CellPrintList.Count)  
  50.                         return;  
  51.                     if (PoX > e.MarginBounds.Left)  
  52.                         PrintDataGridView.IsRole = true;  
  53.                     else  
  54.                         PrintDataGridView.IsRole = false;  
  55.                     PrintDataGridView.PoXTmp = PoX;  
  56.                     PrintDataGridView.PoYTmp = PoY;  
  57.                     PrintDataGridView.RowIndex = 0;  
  58.                     WidthTmp = 0;  
  59.                     HeightTmp = 0;  
  60.                 }  
  61.                 if (0 != PrintDataGridView.printRowCount)  
  62.                 {  
  63.                     if (IsRole)  
  64.                     {  
  65.                         PoX = PoXTmp = e.MarginBounds.Left;  
  66.                         PoY = PoYTmp = e.MarginBounds.Top;  
  67.                     }  
  68.                     else  
  69.                     {  
  70.                         PoX = PoXTmp;  
  71.                         PoY = PoYTmp;  
  72.                     }  
  73.                 }  
  74.                 while (PrintDataGridView.printRowCount < PrintDataGridView.CellPrintList.Count)  
  75.                 {  
  76.                     DataGridViewCellPrint CellPrint = CellPrintList[PrintDataGridView.printRowCount];  
  77.                     if (RowIndex == CellPrint.RowIndex)  
  78.                         PoX = PoX + WidthTmp;  
  79.                     else  
  80.                     {  
  81.                         PoX = PoXTmp;  
  82.                         PoY = PoY + HeightTmp;  
  83.                         if (PoY + HeightTmp > e.MarginBounds.Bottom)  
  84.                         {  
  85.                             HeightTmp = 0;  
  86.                             e.HasMorePages = true;  
  87.                             return;  
  88.                         }  
  89.                     }  
  90.                     using (SolidBrush solidBrush = new SolidBrush(CellPrint.BackColor))  
  91.                     {  
  92.                         RectangleF rectF1 = new RectangleF(PoX, PoY, CellPrint.Width, CellPrint.Height);  
  93.                         e.Graphics.FillRectangle(solidBrush, rectF1);  
  94.                         using (Pen pen = new Pen(Color.Black, 1))  
  95.                             e.Graphics.DrawRectangle(pen, Rectangle.Round(rectF1));  
  96.                         solidBrush.Color = CellPrint.ForeColor;  
  97.                         e.Graphics.DrawString(CellPrint.FormattedValue, CellPrint.Font, solidBrush, new Point(PoX + 2, PoY + 3));  
  98.                     }  
  99.                     WidthTmp = CellPrint.Width;  
  100.                     HeightTmp = CellPrint.Height;  
  101.                     RowIndex = CellPrint.RowIndex;  
  102.                     PrintDataGridView.printRowCount++;  
  103.                 }  
  104.                 PoY = PoY + HeightTmp;  
  105.                 e.HasMorePages = false;  
  106.                 PrintDataGridView.IsPrint = true;  
  107.             }  
  108.             catch  
  109.             {  
  110.                 e.HasMorePages = false;  
  111.                 PrintDataGridView.IsPrint = true;  
  112.                 throw;  
  113.             }  
  114.   
  115.         }  
  116.   
  117.   
  118.         ///  
  119.         /// 将DataGridView控件内容转变到 CellPrintList  
  120.         ///  
  121.         /// DataGridView控件  
  122.         /// 是否包括列标题  
  123.         private static void DataGridViewCellVsList(DataGridView dataGridView, bool includeColumnText)  
  124.         {  
  125.             CellPrintList.Clear();  
  126.             try  
  127.             {  
  128.                 int rowsCount = dataGridView.Rows.Count;  
  129.                 int colsCount = dataGridView.Columns.Count;  
  130.   
  131.                 //最后一行是供输入的行时,不用读数据。  
  132.                 if (dataGridView.Rows[rowsCount - 1].IsNewRow)  
  133.                     rowsCount--;  
  134.                 //包括列标题  
  135.                 if (includeColumnText)  
  136.                 {  
  137.                     for (int columnsIndex = 0; columnsIndex < colsCount; columnsIndex++)  
  138.                     {  
  139.                         if (dataGridView.Columns[columnsIndex].Visible)  
  140.                         {  
  141.                             DataGridViewCellPrint CellPrint = new DataGridViewCellPrint();  
  142.                             CellPrint.FormattedValue = dataGridView.Columns[columnsIndex].HeaderText;  
  143.                             CellPrint.RowIndex = 0;  
  144.                             CellPrint.ColumnIndex = columnsIndex;  
  145.                             CellPrint.Font = dataGridView.Columns[columnsIndex].HeaderCell.Style.Font;  
  146.                             CellPrint.BackColor = dataGridView.ColumnHeadersDefaultCellStyle.BackColor;  
  147.                             CellPrint.ForeColor = dataGridView.ColumnHeadersDefaultCellStyle.ForeColor;  
  148.                             CellPrint.Width = dataGridView.Columns[columnsIndex].Width;  
  149.                             CellPrint.Height = dataGridView.ColumnHeadersHeight;  
  150.                             CellPrintList.Add(CellPrint);  
  151.                         }  
  152.                     }  
  153.                 }  
  154.                 //读取单元格数据  
  155.                 for (int rowIndex = 0; rowIndex < rowsCount; rowIndex++)  
  156.                 {  
  157.                     for (int columnsIndex = 0; columnsIndex < colsCount; columnsIndex++)  
  158.                     {  
  159.                         if (dataGridView.Columns[columnsIndex].Visible)  
  160.                         {  
  161.                             DataGridViewCellPrint CellPrint = new DataGridViewCellPrint();  
  162.                             CellPrint.FormattedValue = dataGridView.Rows[rowIndex].Cells[columnsIndex].FormattedValue.ToString();  
  163.                             if (includeColumnText)  
  164.                                 CellPrint.RowIndex = rowIndex + 1;//假如包括列标题则从行号1开始  
  165.                             else  
  166.                                 CellPrint.RowIndex = rowIndex;  
  167.                             CellPrint.ColumnIndex = columnsIndex;  
  168.                             CellPrint.Font = dataGridView.Rows[rowIndex].Cells[columnsIndex].Style.Font;  
  169.                             System.Drawing.Color TmpColor = System.Drawing.Color.Empty;  
  170.                             if (System.Drawing.Color.Empty != dataGridView.Rows[rowIndex].Cells[columnsIndex].Style.BackColor)  
  171.                                 TmpColor = dataGridView.Rows[rowIndex].Cells[columnsIndex].Style.BackColor;  
  172.                             else if (System.Drawing.Color.Empty != dataGridView.Rows[rowIndex].DefaultCellStyle.BackColor)  
  173.                                 TmpColor = dataGridView.Rows[rowIndex].DefaultCellStyle.BackColor;  
  174.                             else  
  175.                                 TmpColor = dataGridView.DefaultCellStyle.BackColor;  
  176.                             CellPrint.BackColor = TmpColor;  
  177.                             TmpColor = System.Drawing.Color.Empty;  
  178.                             if (System.Drawing.Color.Empty != dataGridView.Rows[rowIndex].Cells[columnsIndex].Style.ForeColor)  
  179.                                 TmpColor = dataGridView.Rows[rowIndex].Cells[columnsIndex].Style.ForeColor;  
  180.                             else if (System.Drawing.Color.Empty != dataGridView.Rows[rowIndex].DefaultCellStyle.ForeColor)  
  181.                                 TmpColor = dataGridView.Rows[rowIndex].DefaultCellStyle.ForeColor;  
  182.                             else  
  183.                                 TmpColor = dataGridView.DefaultCellStyle.ForeColor;  
  184.                             CellPrint.ForeColor = TmpColor;  
  185.                             CellPrint.Width = dataGridView.Columns[columnsIndex].Width;  
  186.                             CellPrint.Height = dataGridView.Rows[rowIndex].Height;  
  187.                             CellPrintList.Add(CellPrint);  
  188.                         }  
  189.                     }  
  190.                 }  
  191.             }  
  192.             catch { throw; }  
  193.         }  
  194.   
  195.         private class DataGridViewCellPrint  
  196.         {  
  197.             private string _FormattedValue = "";  
  198.             private int _RowIndex = -1;  
  199.             private int _ColumnIndex = -1;  
  200.             private System.Drawing.Color _ForeColor = System.Drawing.Color.Black;  
  201.             private System.Drawing.Color _BackColor = System.Drawing.Color.White;  
  202.             private int _Width = 100;  
  203.             private int _Height = 23;  
  204.             private System.Drawing.Font _Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));  
  205.   
  206.             ///  
  207.             /// 获取或设置单元格的字体。  
  208.             ///  
  209.             public System.Drawing.Font Font  
  210.             {  
  211.                 set { if (null != value) _Font = value; }  
  212.                 get { return _Font; }  
  213.             }  
  214.   
  215.             ///  
  216.             /// 获取为显示进行格式化的单元格的值。  
  217.             ///  
  218.             public string FormattedValue  
  219.             {  
  220.                 set { _FormattedValue = value; }  
  221.                 get { return _FormattedValue; }  
  222.             }  
  223.   
  224.             ///  
  225.             /// 获取或设置列的当前宽度 (以像素为单位)。默认值为 100。  
  226.             ///  
  227.             public int Width  
  228.             {  
  229.                 set { _Width = value; }  
  230.                 get { return _Width; }  
  231.             }  
  232.   
  233.             ///  
  234.             /// 获取或设置列标题行的高度(以像素为单位)。默认值为 23。  
  235.             ///  
  236.             public int Height  
  237.             {  
  238.                 set { _Height = value; }  
  239.                 get { return _Height; }  
  240.             }  
  241.   
  242.             ///  
  243.             /// 获取或设置行号。  
  244.             ///  
  245.             public int RowIndex  
  246.             {  
  247.                 set { _RowIndex = value; }  
  248.                 get { return _RowIndex; }  
  249.             }  
  250.   
  251.             ///  
  252.             /// 获取或设置列号。  
  253.             ///  
  254.             public int ColumnIndex  
  255.             {  
  256.                 set { _ColumnIndex = value; }  
  257.                 get { return _ColumnIndex; }  
  258.             }  
  259.   
  260.             ///  
  261.             /// 获取或设置前景色。  
  262.             ///  
  263.             public System.Drawing.Color ForeColor  
  264.             {  
  265.                 set { _ForeColor = value; }  
  266.                 get { return _ForeColor; }  
  267.             }  
  268.   
  269.             ///  
  270.             /// 获取或设置背景色。  
  271.             ///  
  272.             public System.Drawing.Color BackColor  
  273.             {  
  274.                 set { _BackColor = value; }  
  275.                 get { return _BackColor; }  
  276.             }  
  277.   
  278.         }  
  279.   
  280.     }  
  281. }  

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约