代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Asp.NET
】
调用打印机
作者:
Frank
/ 发布于
2016/2/15
/
935
首先 using System.Drawing.Printing; 这个一定要注意 不然没法用的 要报错
//************************************************************** // // 打印相关(以下部分) // //************************************************************** private DataTable dtStudent; //存放需打印的学生ID列表 private int intIndex = 0; //当前打印序号 //打印所选班级 private void btnPrintClassroom_Click(object sender, EventArgs e) { if (clbClassroom.Items.Count == 0) return; //无班级列表则不能打印 //初始化打印学生数据表 intIndex = 0; dtStudent = new DataTable(); dtStudent.Columns.Add("Id", typeof(string)); string strClassroomId; string strSql; OleDbConnection conn = new OleDbConnection(SqlHelper.connStr); conn.Open(); OleDbCommand cmd; OleDbDataReader dr; //若没有复选,则打印选择项 if (clbClassroom.CheckedItems.Count == 0) { strSql = "select Id from Student where ClassroomId='" + clbClassroom.SelectedValue.ToString() + "'"; cmd = new OleDbCommand(strSql, conn); dr = cmd.ExecuteReader(); while (dr.Read()) { DataRow drow = dtStudent.NewRow(); string strStudentId = dr["Id"].ToString(); StudentInfo si = new StudentInfo(strStudentId); //若选择了只打带毕业证号并且有毕业证号 或者 没有选择只打带毕业证号的,则打印 if (chbOnlyPrintDiplomaNo.Checked && si.DiplomaNo.Trim() != "" || !chbOnlyPrintDiplomaNo.Checked) { drow["Id"] = strStudentId; dtStudent.Rows.Add(drow); } } dr.Close(); } foreach (System.Data.DataRowView item in clbClassroom.CheckedItems) { strClassroomId = item.Row["Id"].ToString(); strSql = "select Id from Student where ClassroomId='" + strClassroomId + "'"; cmd = new OleDbCommand(strSql, conn); dr = cmd.ExecuteReader(); while (dr.Read()) { DataRow drow = dtStudent.NewRow(); string strStudentId = dr["Id"].ToString(); StudentInfo si = new StudentInfo(strStudentId); //若选择了只打带毕业证号并且有毕业证号 或者 没有选择只打带毕业证号的,则打印 if (chbOnlyPrintDiplomaNo.Checked && si.DiplomaNo.Trim() != "" || !chbOnlyPrintDiplomaNo.Checked) { drow["Id"] = strStudentId; dtStudent.Rows.Add(drow); } } dr.Close(); } conn.Close(); //初始化进度条 pb.Minimum = 0; pb.Maximum = dtStudent.Rows.Count; pb.Value = 0; pb.Step = 1; if (dtStudent.Rows.Count > 0) { printDocument.Print(); } else { MessageBox.Show("没有满足打印条件的数据!"); } } //打印所选学生 private void btnPrintStudent_Click(object sender, EventArgs e) { if (clbStudent.Items.Count == 0) return; //无学生列表则不能打印 //初始化打印学生数据表 intIndex = 0; dtStudent = new DataTable(); dtStudent.Columns.Add("Id", typeof(string)); //若没有复选,则打印选择学生项 if (clbStudent.CheckedItems.Count == 0) { DataRow drow = dtStudent.NewRow(); drow["Id"] = clbStudent.SelectedValue.ToString(); dtStudent.Rows.Add(drow); } foreach (System.Data.DataRowView item in clbStudent.CheckedItems) { DataRow drow = dtStudent.NewRow(); string strStudentId = item["Id"].ToString(); StudentInfo si = new StudentInfo(strStudentId); //若选择了只打带毕业证号并且有毕业证号 或者 没有选择只打带毕业证号的,则打印 if (chbOnlyPrintDiplomaNo.Checked && si.DiplomaNo.Trim() != "" || !chbOnlyPrintDiplomaNo.Checked) { drow["Id"] = strStudentId; dtStudent.Rows.Add(drow); } } //初始化进度条 pb.Minimum = 0; pb.Maximum = dtStudent.Rows.Count; pb.Value = 0; pb.Step = 1; if (dtStudent.Rows.Count > 0) { printDocument.Print(); } else { MessageBox.Show("没有满足打印条件的数据!"); } } //打印预览 private void btnPrintPreview_Click(object sender, EventArgs e) { if (clbStudent.Items.Count == 0) return; //无学生列表则不能打印 //初始化打印学生数据表 intIndex = 0; dtStudent = new DataTable(); dtStudent.Columns.Add("Id", typeof(string)); //若没有复选,则打印预览选择学生项 if (clbStudent.CheckedItems.Count == 0) { DataRow drow = dtStudent.NewRow(); drow["Id"] = clbStudent.SelectedValue.ToString(); dtStudent.Rows.Add(drow); } foreach (System.Data.DataRowView item in clbStudent.CheckedItems) { DataRow drow = dtStudent.NewRow(); string strStudentId = item["Id"].ToString(); StudentInfo si = new StudentInfo(strStudentId); //若选择了只打带毕业证号并且有毕业证号 或者 没有选择只打带毕业证号的,则打印 if (chbOnlyPrintDiplomaNo.Checked && si.DiplomaNo.Trim() != "" || !chbOnlyPrintDiplomaNo.Checked) { drow["Id"] = strStudentId; dtStudent.Rows.Add(drow); } } if (dtStudent.Rows.Count > 0) { printPreviewDialog1.Document = printDocument; printPreviewDialog1.ShowDialog(); } else { MessageBox.Show("没有满足打印条件的数据!"); } } //打印设置 private void btnPrintSetup_Click(object sender, EventArgs e) { printDialog1.Document = printDocument; if (printDialog1.ShowDialog() == DialogResult.OK) { printDocument.Print(); } } //设置打印学生成绩内容 private void printDocument_PrintPage(object sender, PrintPageEventArgs e) { lblState.Text = "打印处理中,请等候......[" + intIndex.ToString() + "/" + dtStudent.Rows.Count.ToString() + "]"; Application.DoEvents(); if (intIndex < dtStudent.Rows.Count) { string strStudentId = dtStudent.Rows[intIndex]["Id"].ToString(); printContent(sender, e, strStudentId); } pb.PerformStep(); intIndex++; if (intIndex < dtStudent.Rows.Count) { e.HasMorePages = true; } else { lblState.Text = "打印完成"; e.HasMorePages = false; } } //打印内容设置函数 private void printContent(object sender, PrintPageEventArgs e, string StudentId) { int x, y, w, h, l, t; //坐标,宽、高、左、上 x = e.MarginBounds.X; y = e.MarginBounds.Y; w = e.MarginBounds.Width; h = e.MarginBounds.Height; l = e.MarginBounds.Left; t = e.MarginBounds.Top; int rowSpan = Tools.IsNumeric( cbxRowSpan.Text ); //行间距 int xLeft = x + 20; //左边距 int xRight = 760 -20 ; //右边距 int intRemark = 0; //该生有几科不正常成绩 //获取学生基本信息 StudentInfo si = new StudentInfo(StudentId); //设置Graphics实例 Graphics g = e.Graphics; //画笔普通 Pen pen = new Pen(Color.Black); //画笔重 Pen penStrong = new Pen(Color.Black,2); //字体(表头,即大标题) Font fontHead = new Font(new FontFamily("宋体"), 17, FontStyle.Bold); //字体(小标题,即姓名、班级、落款等) Font fontCaption = new Font(new FontFamily("宋体"), 12); //字体(小表头,即序号,课程名等) Font fontBodyTitle = new Font(new FontFamily("黑体"), 10); //字体(正文) Font fontBody = new Font(new FontFamily("宋体"), 10); //表头 string strHead="XX学院" + si.Jie.ToString() + "届毕业生成绩单"; g.DrawString(strHead, fontHead, Brushes.Black, xLeft + (xRight-xLeft)/2 -strHead.Length*fontHead.Height /2 + 20, y); xLeft = xLeft - 30; y = y + Convert.ToInt32( fontHead.Height * 2.5); //姓名,专业班级,制表日期 g.DrawString("姓名:" + si.StudentName, fontCaption, Brushes.Black, xLeft, y); //是否打印毕业证书号 ,同时考虑专业班级的位置对称 if (chbPrintDiplomaNo.Checked) { g.DrawString("专业班级:" + si.Classroom, fontCaption, Brushes.Black, xLeft + 140, y); if (si.Classroom.Length < 11) { g.DrawString("毕业证号:" + si.DiplomaNo, fontCaption, Brushes.Black, xLeft + 390, y); } else { y = y + Convert.ToInt32(fontCaption.Height * 1.1); g.DrawString("毕业证号:" + si.DiplomaNo, fontCaption, Brushes.Black, xLeft+140, y); } } else { g.DrawString("专业班级:" + si.Classroom, fontCaption, Brushes.Black, xLeft + 330, y); } y = y + Convert.ToInt32(fontCaption.Height*1.5); g.DrawLine(penStrong, xLeft, y, xRight, y); //加水平线 int yStart = y; y = y + rowSpan; g.FillRectangle(Brushes.Gray, xLeft, y-1, xRight-xLeft, fontBodyTitle.Height+2 ); //左成绩表头 g.DrawString("序号", fontBodyTitle, Brushes.Black, xLeft, y); g.DrawString("科目", fontBodyTitle, Brushes.Black, xLeft + 60, y); g.DrawString("成绩", fontBodyTitle, Brushes.Black, xLeft + 245, y); //右成绩表头 g.DrawString("序号", fontBodyTitle, Brushes.Black, xLeft + (xRight - xLeft) / 2 , y); g.DrawString("科目", fontBodyTitle, Brushes.Black, xLeft + (xRight - xLeft) / 2 + 60, y); g.DrawString("成绩", fontBodyTitle, Brushes.Black, xLeft + (xRight - xLeft) / 2 + 245, y); y =y+ fontBodyTitle.Height +rowSpan ; g.DrawLine(penStrong, xLeft, y, xRight, y); y = y + rowSpan; int intNo = 1; //课程序号 for (int i = 0; i < si.TermCount; i++) { //g.FillRectangle(Brushes.LightGray, xLeft, y, xRight2, fontBody.Height); g.DrawString("第" + (i + 1).ToString() + "学期(" + si.arrTerm[i] + ")", fontBodyTitle, Brushes.Black, xLeft, y); y = y + fontBodyTitle.Height + rowSpan; g.DrawLine(pen, xLeft, y, xRight, y); //加水平线 y = y + rowSpan; DataView dv = si.dtScore.DefaultView; dv.RowFilter = "Term='" + si.arrTerm[i] + "'"; for (int j = 0; j < dv.Count; j++) { //第一列成绩 if (dv[j]["Score"].ToString()=="" && dv[j]["Remark"].ToString() != "") //取非正常成绩 { intRemark ++; } g.DrawString((intNo++).ToString() + "." , fontBody, Brushes.Black, xLeft+5, y); g.DrawString( dv[j]["Course"].ToString() + ":", fontBody, Brushes.Black, xLeft+25, y); g.DrawString(dv[j]["Score"].ToString(), fontBody, Brushes.Black, xLeft + 250, y); //第二列成绩 if (++j < dv.Count) { if (dv[j]["Score"].ToString() == "" && dv[j]["Remark"].ToString() != "") { intRemark++; } g.DrawString((intNo++).ToString() + ".", fontBody, Brushes.Black, xLeft + (xRight - xLeft) / 2+10, y); g.DrawString(dv[j]["Course"].ToString(), fontBody, Brushes.Black, xLeft + (xRight - xLeft) / 2 +10 + 25, y); g.DrawString(dv[j]["Score"].ToString(), fontBody, Brushes.Black, xLeft + (xRight - xLeft) / 2 + 250, y); } y = y + fontBody.Height+rowSpan ; } if (i < si.TermCount-1) { g.DrawLine(pen, xLeft, y, xRight, y); //加水平线 } y = y + rowSpan; } g.DrawLine(penStrong, xLeft, y, xRight, y); //加水平线 g.DrawLine(penStrong, xLeft, yStart, xLeft, y); //加左竖线 g.DrawLine(pen, xLeft + (xRight - xLeft) / 2, yStart, xLeft + (xRight - xLeft) / 2, y); //加中竖线 g.DrawLine(penStrong, xRight, yStart, xRight, y); //加右竖线 //选择合适的打印页脚的位置 if (y < (t + h)) //若剩余空行多,就在之间打印页脚 { y=Convert.ToInt32( y+(t + h-y )*0.5); if (intRemark > 0) { g.FillRectangle(Brushes.Gray, xLeft + 20, y, fontCaption.Height*2, fontCaption.Height); g.DrawString(intRemark.ToString(), fontCaption, Brushes.Black, xLeft + 20, y); } g.DrawString("XX学院学生处", fontCaption, Brushes.Black, xLeft + 400, y); y = y + 20; g.DrawString("制表日期: " + System.DateTime.Now.ToShortDateString(), fontCaption, Brushes.Black, xLeft + 435, y); } else //否则在页底打印页脚 { if (intRemark > 0) { g.FillRectangle(Brushes.Gray, xLeft + 20, y, fontCaption.Height * 2, fontCaption.Height); g.DrawString(intRemark.ToString(), fontCaption, Brushes.Black, xLeft + 20, y); } g.DrawString("XX学院学生处", fontCaption, Brushes.Black, xLeft + 200, y + 5); g.DrawString("制表日期: " + System.DateTime.Now.ToShortDateString(), fontCaption, Brushes.Black, xLeft + 450, y + 5); } } private void btnAbout_Click(object sender, EventArgs e) { AboutBox aboutBox = new AboutBox(); aboutBox.ShowDialog(this); }
试试其它关键字
同语言下
.
gzip压缩
.
实现http多线程断点续传下载文件
.
实现多线程断点续传下载大文件
.
生成字符串的 CheckSum
.
根据 UserAgent 获取浏览器的类型和版本
.
根据 Agent 判断是否是智能手机
.
隐藏手机号中间四位为*方法
.
合并图片(二维码和其他图片合并)
.
ASP.NET CORE中判断是否移动端打开网页
.
ASP.NET(C#)实现页面计时(定时)自动跳转
可能有用的
.
gzip压缩
.
实现http多线程断点续传下载文件
.
实现多线程断点续传下载大文件
.
生成字符串的 CheckSum
.
根据 UserAgent 获取浏览器的类型和版本
.
根据 Agent 判断是否是智能手机
.
隐藏手机号中间四位为*方法
.
合并图片(二维码和其他图片合并)
.
ASP.NET CORE中判断是否移动端打开网页
.
ASP.NET(C#)实现页面计时(定时)自动跳转
Frank
贡献的其它代码
(
3
)
.
调用打印机
.
input file控件限制上传文件类型
.
SQL Server跨服务器查询
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3