如何在C#中实现DATAGRIDVIEW 数据打印
-
分享下关于 C#中 实现打印功能的代码 希望对初学者有所帮助
关于打印格式感觉很痛苦 我一般选择两种方式 一种是通过 DRAWING 格式,另外一种通过HTML代码来控制,都特别麻烦,不知道各位高手能否提供一些更好的建议?
第一步 基本操作
定义 一个C#提供的 PrintDocument 对象
private PrintDocument printDocument;
第二步//写一个方法 对打印事件进行初始化
下载: Init.cs- private void PrintDocument()
- {
- printDocument = new PrintDocument();
- printDocument.PrintPage += new PrintPageEventHandler(printDocument_PrintPage);
- }
第三步
事件响应方法
下载: Action.cs- private void printDocument_PrintPage(object sender, PrintPageEventArgs e)
- {
- //StringReader lineReader = new StringReader(textBox.Text);
- Graphics graphic = e.Graphics;//获取绘图对象
- float linesPerPage = 0;//页面行号
- float yPosition = 0;//绘制字符串的纵向位置
- float leftMargin = e.MarginBounds.Left;//左边距
- float topMargin = e.MarginBounds.Top;//上边距
- string line = string.Empty;//读取的行字符串
- int currentPageLine = 0;//当前页读取的行数
- Font charFont = button1.Font;//获取打印字体
- SolidBrush brush = new SolidBrush(Color.Black);//刷子
- linesPerPage = e.MarginBounds.Height / charFont.GetHeight(graphic);//每页可打印的行数
- //countNum记录全局行数,currentPageLine记录当前打印页行数。
- // graphic.DrawString("水费通知单", charFont, brush, 360, 100, new StringFormat());
- //房产地址textbox3
- //租户名字textbox2
- // 上次缴费记录 textbox5
- //费用到期 textbox7
- //合同开始时间 textbox4
- //合同到期时间 textbox6
- //月租金 textbox8
- //月管理费 textbox9
- //缴费租金 textbox11
- //缴费管理费 textbox10
- //总计 textbox12
- graphic.DrawString("武大教育发展有限公司房屋租金缴费登记", charFont, brush, 300, 100, new StringFormat());
- graphic.DrawString("租户: " + this.textBox2.Text, charFont, brush, 100, 140, new StringFormat());
- graphic.DrawString("合同号: " + this.textBox1.Text, charFont, brush, 100, 180, new StringFormat());
- graphic.DrawString("房产地址: " + this.textBox3.Text, charFont, brush, 100, 220, new StringFormat());
- graphic.DrawString("月租金: " + this.textBox8.Text, charFont, brush, 100, 260, new StringFormat());
- graphic.DrawString("月管理费: " + this.textBox9.Text, charFont, brush, 100, 300, new StringFormat());
- graphic.DrawString("上次缴费至: " + this.textBox7.Text, charFont, brush, 100, 340, new StringFormat());
- graphic.DrawString("本次缴费至: " + this.dateTimePicker1.Text, charFont, brush, 100, 370, new StringFormat());
- graphic.DrawString("租金: " + this.textBox11.Text, charFont, brush, 100, 410, new StringFormat());
- graphic.DrawString("管理费: " + this.textBox10.Text, charFont, brush, 100, 450, new StringFormat());
- graphic.DrawString("合计: " + this.textBox12.Text, charFont, brush, 100, 490, new StringFormat());
- graphic.DrawString("备注:", charFont, brush, 100, 530, new StringFormat());
- DateTime today = DateTime.Now;
- string day = today.Date.ToString();
- graphic.DrawString("武大教育发展有限责任公司 " + day, charFont, brush, 580, 1100, new StringFormat());
- }
- 第四步 调用 打印 和预览打印
- this.PrintDocument();
- PrintDialog print = new PrintDialog();
- //将初始化后的printDocument赋给print.Document
- print.Document = printDocument;
- try
- {
- if (print.ShowDialog() == DialogResult.OK)
- {
- //打印时直接调用PrintDocument的Print方法
- printDocument.Print();
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show("打印出错:" + ex.ToString());
- //出错则结束打印过程
- printDocument.PrintController.OnEndPrint(printDocument, new PrintEventArgs());
- }
- 打印预览
- PrintPreviewDialog printPreview = new PrintPreviewDialog();
- //初始化PrintDocument
- this.PrintDocument();
- //将初始化后的printDocument赋给print.Document
- printPreview.Document = printDocument;
- try
- {
- printPreview.ShowDialog();
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.ToString());
- }
- 打印方法实例
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- using System.Drawing.Printing;
- using System.IO;
- /// <summary>
- /// 实现DataGridView的打印
- /// </summary>
- public class PrintDataGridView
- {
- private static List<DataGridViewCellPrint> CellPrintList = new List<DataGridViewCellPrint>();
- private static int printRowCount = 0;
- private static bool IsPrint = true;
- private static bool IsRole = true;
- private static int PoXTmp = 0;
- private static int PoYTmp = 0;
- private static int WidthTmp = 0;
- private static int HeightTmp = 0;
- private static int RowIndex = 0;
- /// <summary>
- /// 打印DataGridView控件
- /// </summary>
- /// <param name="dataGridView">DataGridView控件</param>
- /// <param name="includeColumnText">是否包括列标题</param>
- /// <param name="e">为 System.Drawing.Printing.PrintDocument.PrintPage 事件提供数据。</param>
- /// <param name="PoX">起始X坐标</param>
- /// <param name="PoY">起始Y坐标</param>
- public static void Print(DataGridView dataGridView, bool includeColumnText, PrintPageEventArgs e, ref int PoX, ref int PoY, string pageTitle)
- {
- try
- {
- if (PrintDataGridView.IsPrint)
- {
- PrintDataGridView.printRowCount = 0;
- PrintDataGridView.IsPrint = false;
- PrintDataGridView.DataGridViewCellVsList(dataGridView, includeColumnText);
- if (0 == PrintDataGridView.CellPrintList.Count)
- return;
- if (PoX > e.MarginBounds.Left)
- PrintDataGridView.IsRole = true;
- else
- PrintDataGridView.IsRole = false;
- PrintDataGridView.PoXTmp = PoX;
- PrintDataGridView.PoYTmp = PoY;
- PrintDataGridView.RowIndex = 0;
- WidthTmp = 0;
- HeightTmp = 0;
- }
- if (0 != PrintDataGridView.printRowCount)
- {
- if (IsRole)
- {
- PoX = PoXTmp = e.MarginBounds.Left;
- PoY = PoYTmp = e.MarginBounds.Top;
- }
- else
- {
- PoX = PoXTmp;
- PoY = PoYTmp;
- }
- }
- while (PrintDataGridView.printRowCount < PrintDataGridView.CellPrintList.Count)
- {
- DataGridViewCellPrint CellPrint = CellPrintList[PrintDataGridView.printRowCount];
- if (RowIndex == CellPrint.RowIndex)
- PoX = PoX + WidthTmp;
- else
- {
- PoX = PoXTmp;
- PoY = PoY + HeightTmp;
- if (PoY + HeightTmp > e.MarginBounds.Bottom)
- {
- HeightTmp = 0;
- e.HasMorePages = true;
- return;
- }
- }
- using (SolidBrush solidBrush = new SolidBrush(CellPrint.BackColor))
- {
- RectangleF rectF1 = new RectangleF(PoX, PoY, CellPrint.Width, CellPrint.Height);
- e.Graphics.FillRectangle(solidBrush, rectF1);
- using (Pen pen = new Pen(Color.Black, 1))
- {
- e.Graphics.DrawRectangle(pen, Rectangle.Round(rectF1));
- } solidBrush.Color = CellPrint.ForeColor;
- //打印 页面标题
- e.Graphics.DrawString(pageTitle, CellPrint.Font, solidBrush, 300, 80);
- DateTime dt = new DateTime();
- e.Graphics.DrawString("武大教育发展有限公司 " + dt.Date.ToString(), CellPrint.Font, solidBrush, 400, 1100);
- e.Graphics.DrawString(CellPrint.FormattedValue, CellPrint.Font, solidBrush, new Point(PoX + 2, PoY + 3));
- }
- WidthTmp = CellPrint.Width;
- HeightTmp = CellPrint.Height;
- RowIndex = CellPrint.RowIndex;
- PrintDataGridView.printRowCount++;
- }
- PoY = PoY + HeightTmp;
- e.HasMorePages = false;
- PrintDataGridView.IsPrint = true;
- }
- catch
- {
- e.HasMorePages = false;
- PrintDataGridView.IsPrint = true;
- throw;
- }
- }
- /// <summary>
- /// 将DataGridView控件内容转变到 CellPrintList
- /// </summary>
- /// <param name="dataGridView">DataGridView控件</param>
- /// <param name="includeColumnText">是否包括列标题</param>
- 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);
- }
- }
- }
- //读取单元格数据
- 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
- {
- 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; }
- }
- }
- }
-
我们的说明!
欢迎转载,但请您以链接形式注明本文出处和本站原文链接,下面是链接形式,谢谢合作!
出处链接:Allove of Paradise
原文链接:http://blog.allove.org/archives/c-sharp-datagridview-print.html
2 条评论
Leave a Comment



十一月 13th, 2008 at 10:03 上午
“不想我吗?!”
里面的东西应该默认显示才对…
[回复]
Daniel Reply:
十一月 13th, 2008 at 11:15 上午
希望这样用户体验更好!
[回复]