zoukankan      html  css  js  c++  java
  • asp.net 调用打印机 (转)

    首先  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);
            }
  • 相关阅读:
    C语言编程 两种方法打印一个菱形(渐入显示)
    Java编程格式输出中 printf 的使用实例
    C语言编程输入一个5位数以内的正整数,完成以下操作
    C语言编程判断两个矩阵是否相等(n阶矩阵)
    C语言编程输出100到200的素数的两种方法,和三步优化(逐步优化)
    Java编程中Math.random()的应用(随机生成数的应用)
    C语言编程求1X2X3····Xn所得的数末尾有多少个零
    C语言大数的阶乘
    maven构建一个简单的springboot项目
    nginx的配置
  • 原文地址:https://www.cnblogs.com/zhwl/p/2010171.html
Copyright © 2011-2022 走看看