http://zamuj.blog.hexun.com/15140514_d.html
using System;
using
System.Collections.Generic;
using System.ComponentModel;
using
System.Data;
using System.Drawing;
using System.Text;
using
System.Windows.Forms;
using System.Data.SqlClient;
using
System.Drawing.Printing;
namespace ExcelPrj
{
public partial class
Test : Form
{
/// <summary>
/// .NET
框架提供了相关的类实现文档的打印功能,这些类在 System.Drawing.Printing
命名空间内.为了实现打印功能,通常首先需要建立PringDocument 类的实例化对象,PrintDocument
类用于设置打印文档的属性和方法
/// 调用PringDocument类的 Print 方法后,会间接调用打印控制器类的方法,这些方法包括
OnStartPrint, OnEndPring,OnStartPage,OnEndPage,由这些方法来完成把文档内容输出到打印机的工作
/// 在打印之前,还可以选择用于输出的打印机.在.NET 框架中,PringDialog
类可以实现打印机设置对话框,在此对话框中可以选择输出的打印机,打印方法,和文档分数
/// 与 PrintPreviewDialog
类类似, PrintDialog 也是间接调用打印控制器的相关方法实现打印输出的阿功能
/// .NET 框架还提供了
PringPreviewDialog 类实现打印预览的功能.PringviewDialog 在屏幕上显示通用的打印预览窗口,窗口中 的内容是通过
Pringdocument 类间接调用打印控制器的方法,显示在屏幕上.
/// </summary>
public Test()
{
InitializeComponent();
}
private void Test_Load(object sender, EventArgs e)
{
DataSet ds = Bind();
this.dataGridView1.DataSource = ds.Tables[0];
}
private
DataSet Bind()
{
SqlConnection conn = new
SqlConnection("Server=.;Database=testManage;Integrated
Security=SSPI");
SqlDataAdapter da = new SqlDataAdapter("select
FNumber,FExamNum,FName,FSex,FJobAdd,FCardID,FBirDate from stuInfo",
conn);
DataSet ds = new DataSet();
da.Fill(ds);
return ds;
}
///
<summary>
/// [1] 编写打印逻辑步骤
/// 将 PringPreviewDialog1
的Document属性设置为
PringDocument1.当PringPreviewDialog1对象执行ShowDialog方法之后,会触发PringDocument1对象的
PringPage 事件
/// </summary>
/// <param
name="sender"></param>
/// <param
name="e"></param>
private void
printDocument1_PrintPage(object sender, PrintPageEventArgs e)
{
this.pageSetupDialog1.Document = this.printDocument1;
Font objFont = new Font("Tahoma", 11,
FontStyle.Regular);
Brush objBrush =
Brushes.Black;
Pen objPen = new Pen(objBrush);
objPen.Width = 2;
//象打印预览窗口添加内容
int nLeft =
this.pageSetupDialog1.PageSettings.Margins.Left;
int nTop =
this.pageSetupDialog1.PageSettings.Margins.Top;
int
nWidth=this.pageSetupDialog1.PageSettings.PaperSize.Width-(this.pageSetupDialog1.PageSettings.Margins.Left+this.pageSetupDialog1.PageSettings.Margins.Right);
int
nHeight=this.pageSetupDialog1.PageSettings.PaperSize.Height-(this.pageSetupDialog1.PageSettings.Margins.Top+this.pageSetupDialog1.PageSettings.Margins.Bottom);
//画出页面的有效区域
e.Graphics.DrawLine(objPen, nLeft, nTop, nLeft + nWidth, nTop);
e.Graphics.DrawLine(objPen, nLeft, nTop + nHeight, nLeft +
nWidth,nTop+nHeight);
e.Graphics.DrawLine(objPen, nLeft, nTop,
nLeft, nTop+nHeight);
e.Graphics.DrawLine(objPen, nLeft + nWidth,
nTop, nLeft + nWidth, nTop + nHeight);
// 打印表头
e.Graphics.DrawString("学号", new Font("Garamond", 20, FontStyle.Regular),
Brushes.Blue, nLeft + 30, nTop + 10);
e.Graphics.DrawString("姓名",
new Font("Garamond", 20, FontStyle.Regular), Brushes.Blue, nLeft + 150, nTop +
10);
e.Graphics.DrawString("年龄", new Font("Garamond", 20,
FontStyle.Regular), Brushes.Blue, nLeft + 270, nTop + 10);
e.Graphics.DrawString("出生的", new Font("Garamond", 20, FontStyle.Regular),
Brushes.Blue, nLeft + 390, nTop + 10);
DataSet ds = Bind();
for(int i=0;i<ds.Tables[0].Rows.Count;i++)
{
for (int j = 0; j < ds.Tables[0].Columns.Count;
j++)
{
e.Graphics.DrawString(ds.Tables[0].Rows[i][j].ToString(),objFont,objBrush,nLeft
+ 50+nLeft*j+20,nTop + 10+nTop*i);
}
}
//if (e.HasMorePages)
//{
// //
有其他的页未打印,
//}
}
///
<summary>
/// [2] 选择打印机
///
</summary>
/// <param
name="sender"></param>
/// <param
name="e"></param>
private void button2_Click(object sender,
EventArgs e)
{
}
///
<summary>
/// [3]允许用户选择页面设置
///
</summary>
/// <param
name="sender"></param>
/// <param
name="e"></param>
private void button3_Click(object sender,
EventArgs e)
{
pageSetupDialog1.ShowDialog();
}
///
<summary>
/// [4]显示预览窗体
///
</summary>
/// <param
name="sender"></param>
/// <param
name="e"></param>
private void button1_Click(object sender,
EventArgs e)
{
//PrintPreviewDialog 是.NET
类库中的打印预览窗体类,每个窗体类的对象都包含 PrintPreviewControl 控件
//PrintPreviewControl 控件用于显示打印预览的内容,如果需要定制预览窗体,可把
PrintPreviewControl控件置于定制预览窗体上面
this.printPreviewDialog1.Document
= this.printDocument1;
this.printPreviewDialog1.ShowDialog();
}
private void button4_Click(object sender,
EventArgs e)
{
try
{
DialogResult pDialogResult =
printDialog1.ShowDialog();
//判断打印对话框的返回值是否是OK,通常按下OK
就会返回此值
if (pDialogResult == DialogResult.OK)
printDocument1.Print();
}
catch (Exception
ex)
{
throw ex;
}
}
}
}