求C# printdocument 的用法示例程序,求 C# 续打的实现?

解决方案 »

  1.   

    我自己搜索了 会了
    付源码: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.Data.OracleClient;namespace printOracle
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        DataTable dt;
            DataTable dtTp;
            private PrintDocument printDocument;
            private void button1_Click(object sender, EventArgs e)//绑定数据
            {
                //{
                //    string ConnectionString = "Data Source=2YHIS;user=his;password=ey510;";//写连接串
                //    OracleConnection conn = new OracleConnection(ConnectionString);//创建一个新连接
                //    try
                //    {
                //        conn.Open();
                //        OracleCommand cmd = conn.CreateCommand();
                //        cmd.CommandText = "select * from ipmomdod left join II_INMAININFO on II_INMAININFO.INPATIENT_NO=ipmomdod.inpatient_no where ipmomdod.inpatient_no='ZY010000410055'";//在这儿写sql语句
                //        dt = new DataTable();
                //        OracleDataAdapter oda = new OracleDataAdapter(cmd.CommandText, conn);
                //        try
                //        {
                //            oda.Fill(dt);
                //        }
                //        catch (Exception ex)
                //        {
                //            MessageBox.Show(ex.ToString());
                //        }
                //    }
                //    catch (Exception ee)
                //    {
                //        MessageBox.Show(ee.ToString());
                //    }
                //    finally
                //    {
                //        conn.Close(); //关闭连接
                //    }            //    BindGrid(dt);
                //}            {
                    //string ConnectionString = "Data Source=2YHIS;user=his;password=ey510;";//写连接串
                    string ConnectionString = "Data Source=ORCL;user=system;password=his;";//写连接串
                    OracleConnection conn = new OracleConnection(ConnectionString);//创建一个新连接
                    try
                    {
                        conn.Open();
                        OracleCommand cmd = conn.CreateCommand();
                        //cmd.CommandText = "select * from ipmomdod where inpatient_no='ZY010000410055'";//在这儿写sql语句
                        //cmd.CommandText = "select t.*, decode((select innerid from test_temp a where a.innerid=t.innerid and a.no=t.no),t.innerid,'1','0') as flag from test  t where t.innerid='ZY010000410055' ";//在这儿写sql语句
                        cmd.CommandText = "select * from test";
                        dt = new DataTable();
                        OracleDataAdapter oda = new OracleDataAdapter(cmd.CommandText, conn);
                        try
                        {
                            oda.Fill(dt);
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.ToString());
                        }
                    }
                    catch (Exception ee)
                    {
                        MessageBox.Show(ee.ToString());
                    }
                    finally
                    {
                        conn.Close(); //关闭连接
                    }                BindGrid(dt);
                }        }
      

  2.   

            private void BindGrid(DataTable dt)
            {
                this.dataGridView1.DataSource = dt;
            }        private void button2_Click(object sender, EventArgs e)
            {
                printDocument = new PrintDocument();
                printDocument.PrintPage += new PrintPageEventHandler(this.printDocument1_PrintPage);
                this.printPreviewDialog1.Document = this.printDocument1;//设置预览内容
                this.printPreviewDialog1.ShowDialog();//预览
                i = 0;//打印预览之后给全局变量赋回原值
                j = 0;
                flagTitle = 0;
                count = 0;
                //y = 3000;            try
                {
                    //printDocument.Print();
                    string insert = "insert into TEST_TEMP values('1','2','3','4')";
                    string ConnectionString = "Data Source=ORCL;user=system;password=his;";//写连接串
                    OracleConnection conn = new OracleConnection(ConnectionString);//创建一个新连接
                    try
                    {
                        conn.Open();
                        OracleCommand cmd = conn.CreateCommand();
                        cmd.CommandText = insert;
                        //dt = new DataTable();
                        //OracleDataAdapter oda = new OracleDataAdapter(cmd.CommandText, conn);
                        //try
                        //{
                        //    oda.Fill(dt);
                        //}
                        //catch (Exception ex)
                        //{
                        //    MessageBox.Show(ex.ToString());
                        //}
                        cmd.ExecuteOracleNonQuery();
                        return 0;
                    }
                    catch (Exception ee)
                    {
                        MessageBox.Show(ee.ToString());
                    }
                    finally
                    {
                        conn.Close(); //关闭连接
                    }            }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }
            int i = 0;//为了循环 datatable
            int j = 0;//打印的是第几行
            int flagTitle = 0;//若是续打 则传个标志 如果是下一页打印 1 则打印表头
            int count = 0;//分页之后 赋值给 i 以便循环 datatable 
            //int y = 3000;
            private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
            {
                Font fTitle = new Font("宋体", 16); // 标题字体
                int titleHeight = 20; // 标题的高度
                Font fText = new Font("宋体", 9); // 文本字体            Graphics g = e.Graphics;
                Rectangle R1 = new Rectangle(15, 70, 800, 30);
                //g.DrawRectangle(Pens.Black, R1);
                Font fs9 = new Font("宋体", 9);
                Font ft1 = new Font("宋体", 12);
                if (this.checkBox1.Checked == true)
                {
                    if (flagTitle == 1)
                    {
                        g.DrawString("临 时 医 嘱 单", fTitle, Brushes.Black, 300, 50);                    //患者姓名
                        g.DrawString("姓名:", fs9, Brushes.Black, 25, 140);                    //住院号
                        g.DrawString("住院号:", fs9, Brushes.Black, 655, 140);                    //表头 医嘱
                        R1 = new Rectangle(15, 160, 160, 30);
                        g.DrawRectangle(Pens.Black, R1);
                        g.DrawString("医    嘱", fs9, Brushes.Black, 70, 170);                    //表头 日期
                        R1 = new Rectangle(15, 190, 80, 30);
                        g.DrawString("日期", fs9, Brushes.Black, 40, 200);
                        g.DrawRectangle(Pens.Black, R1);                    // 表头 时间
                        R1 = new Rectangle(95, 190, 80, 30);
                        g.DrawRectangle(Pens.Black, R1);
                        g.DrawString("时间", fs9, Brushes.Black, 115, 200);                    //表头  医嘱内容
                        R1 = new Rectangle(175, 160, 400, 60);//95+80=175 800-80*3-160=400
                        g.DrawRectangle(Pens.Black, R1);
                        g.DrawString("医                          嘱", fs9, Brushes.Black, 270, 185);                    //表头 医生签字
                        R1 = new Rectangle(575, 160, 80, 60);//575=175+400
                        g.DrawRectangle(Pens.Black, R1);
                        g.DrawString("医生签字", fs9, Brushes.Black, 585, 185);                    //表头 执行时间
                        R1 = new Rectangle(655, 160, 80, 60);//
                        g.DrawRectangle(Pens.Black, R1);
                        g.DrawString("执行时间", fs9, Brushes.Black, 665, 185);                    //表头 护士签名
                        R1 = new Rectangle(735, 160, 80, 60);//
                        g.DrawRectangle(Pens.Black, R1);
                        g.DrawString("护士签名", fs9, Brushes.Black, 745, 185);
                    }
                }
                else//不是续打就打印表头
                {
                    #region 2010-02-10 表头
                    g.DrawString("临 时 医 嘱 单", fTitle, Brushes.Black, 300, 50);                //患者姓名
                    g.DrawString("姓名:", fs9, Brushes.Black, 25, 140);                //住院号
                    g.DrawString("住院号:", fs9, Brushes.Black, 655, 140);                //表头 医嘱
                    R1 = new Rectangle(15, 160, 160, 30);
                    g.DrawRectangle(Pens.Black, R1);
                    g.DrawString("医    嘱", fs9, Brushes.Black, 70, 170);                //表头 日期
                    R1 = new Rectangle(15, 190, 80, 30);
                    g.DrawString("日期", fs9, Brushes.Black, 40, 200);
                    g.DrawRectangle(Pens.Black, R1);                // 表头 时间
                    R1 = new Rectangle(95, 190, 80, 30);
                    g.DrawRectangle(Pens.Black, R1);
                    g.DrawString("时间", fs9, Brushes.Black, 115, 200);                //表头  医嘱内容
                    R1 = new Rectangle(175, 160, 400, 60);//95+80=175 800-80*3-160=400
                    g.DrawRectangle(Pens.Black, R1);
                    g.DrawString("医                          嘱", fs9, Brushes.Black, 270, 185);                //表头 医生签字
                    R1 = new Rectangle(575, 160, 80, 60);//575=175+400
                    g.DrawRectangle(Pens.Black, R1);
                    g.DrawString("医生签字", fs9, Brushes.Black, 585, 185);                //表头 执行时间
                    R1 = new Rectangle(655, 160, 80, 60);//
                    g.DrawRectangle(Pens.Black, R1);
                    g.DrawString("执行时间", fs9, Brushes.Black, 665, 185);                //表头 护士签名
                    R1 = new Rectangle(735, 160, 80, 60);//
                    g.DrawRectangle(Pens.Black, R1);
                    g.DrawString("护士签名", fs9, Brushes.Black, 745, 185);
                    #endregion
                }
                //连接 新的datatable
               
                string ConnectionString = "Data Source=ORCL;user=system;password=his;";//写连接串
                OracleConnection conn = new OracleConnection(ConnectionString);//创建一个新连接
                try
                {
                    conn.Open();
                    OracleCommand cmd = conn.CreateCommand();
                    //cmd.CommandText = "select * from ipmomdod where inpatient_no='ZY010000410055'";//在这儿写sql语句
                    //cmd.CommandText = "select t.*, decode((select innerid from test_temp a where a.innerid=t.innerid and a.no=t.no),t.innerid,'1','0') as flag from test  t where t.innerid='ZY010000410055' ";//在这儿写sql语句
                    cmd.CommandText = "select * from tp t";
                    dtTp = new DataTable();
                    OracleDataAdapter oda = new OracleDataAdapter(cmd.CommandText, conn);
                    try
                    {
                        oda.Fill(dtTp);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.ToString());
                    }
                }
                catch (Exception ee)
                {
                    MessageBox.Show(ee.ToString());
                }
                finally
                {
                    conn.Close(); //关闭连接
                }           
      

  3.   

     for (i = count; i < dt.Rows.Count; i++)
                {
                    //g.DrawString("  " + dt.Rows[i]["item_name"].ToString(), ftext, Brushes.Black, 15, i * 15+50);
                    //if (dt.Rows[i]["sign"].ToString() == '0'.ToString())
                    //{
                    //    g.DrawString("  " + dt.Rows[i]["name"].ToString(), ftext, Brushes.Black, 15, 900 + 50);
                    //    //g.DrawLine(Pens.Black,new Point(15), new Point(i * 15 + 50));
                    //    titleHeight += 1200;
                    //}
                    //if (m == 1)
                    //{
                    //    g.DrawString("" + dt.Rows[i]["dept_code"].ToString(), ftext, Brushes.Black, 15, i * 20 + 100);
                    //}
                    //else
                    //{
                    //    g.DrawString("" + dt.Rows[i]["dept_code"].ToString(), ftext, Brushes.Black, 15, i * 20 + 100);
                    //}
                    //g.DrawString("" + dt.Rows[i]["name"].ToString(), fs9, Brushes.Black, 15, j*40 + 230);
                    if (checkBox1.Checked == true)
                    {
                        if (dtTp.Rows[i]["flag"].ToString() == '1'.ToString())
                        {                    }
                        else
                        {
                            g.DrawString("" + dtTp.Rows[i]["name"].ToString(), fs9, Brushes.Black, 185, j * 40 + 230);
                            //绘制日期内容的表格
                            R1 = new Rectangle(15, j * 40 + 220, 80, 40);//绘制日期内容的表格
                            g.DrawRectangle(Pens.Black, R1);                        //绘制时间内容的表格
                            R1 = new Rectangle(95, j * 40 + 220, 80, 40);//绘制时间内容的表格
                            g.DrawRectangle(Pens.Black, R1);                        //绘制医嘱内容的表格
                            R1 = new Rectangle(175, j * 40 + 220, 400, 40);//绘制医嘱内容的表格
                            g.DrawRectangle(Pens.Black, R1);                        //绘制医生签字内容的表格
                            R1 = new Rectangle(575, j * 40 + 220, 80, 40);//绘制医生签字内容的表格
                            g.DrawRectangle(Pens.Black, R1);                        //绘制执行时间内容的表格
                            R1 = new Rectangle(655, j * 40 + 220, 80, 40);//绘制执行时间内容的表格
                            g.DrawRectangle(Pens.Black, R1);                        //绘制护士签名内容的表格
                            R1 = new Rectangle(735, j * 40 + 220, 80, 40);//绘制护士签名内容的表格
                            g.DrawRectangle(Pens.Black, R1);
                        }
                    }
                    else 
                    {
                        g.DrawString("" + dtTp.Rows[i]["name"].ToString(), fs9, Brushes.Black, 185, j * 40 + 230);
                        //绘制日期内容的表格
                        R1 = new Rectangle(15, j * 40 + 220, 80, 40);//绘制日期内容的表格
                        g.DrawRectangle(Pens.Black, R1);                    //绘制时间内容的表格
                        R1 = new Rectangle(95, j * 40 + 220, 80, 40);//绘制时间内容的表格
                        g.DrawRectangle(Pens.Black, R1);                    //绘制医嘱内容的表格
                        R1 = new Rectangle(175, j * 40 + 220, 400, 40);//绘制医嘱内容的表格
                        g.DrawRectangle(Pens.Black, R1);                    //绘制医生签字内容的表格
                        R1 = new Rectangle(575, j * 40 + 220, 80, 40);//绘制医生签字内容的表格
                        g.DrawRectangle(Pens.Black, R1);                    //绘制执行时间内容的表格
                        R1 = new Rectangle(655, j * 40 + 220, 80, 40);//绘制执行时间内容的表格
                        g.DrawRectangle(Pens.Black, R1);                    //绘制护士签名内容的表格
                        R1 = new Rectangle(735, j * 40 + 220, 80, 40);//绘制护士签名内容的表格
                        g.DrawRectangle(Pens.Black, R1);
                    }                #region 2010-02-10
                    //g.DrawString("" + dt.Rows[i]["name"].ToString(), fs9, Brushes.Black, 185, j * 40 + 230);//2010-02-10
                    //R1 = new Rectangle(15, j * 40 + 220, 800, 40);
                    ////绘制日期内容的表格
                    //R1 = new Rectangle(15, j * 40 + 220, 80, 40);//绘制日期内容的表格
                    //g.DrawRectangle(Pens.Black, R1);                ////绘制时间内容的表格
                    //R1 = new Rectangle(95, j * 40 + 220, 80, 40);//绘制时间内容的表格
                    //g.DrawRectangle(Pens.Black, R1);                ////绘制医嘱内容的表格
                    //R1 = new Rectangle(175, j * 40 + 220, 400, 40);//绘制医嘱内容的表格
                    //g.DrawRectangle(Pens.Black, R1);                ////绘制医生签字内容的表格
                    //R1 = new Rectangle(575, j * 40 + 220, 80, 40);//绘制医生签字内容的表格
                    //g.DrawRectangle(Pens.Black, R1);                ////绘制执行时间内容的表格
                    //R1 = new Rectangle(655, j * 40 + 220, 80, 40);//绘制执行时间内容的表格
                    //g.DrawRectangle(Pens.Black, R1);                ////绘制护士签名内容的表格
                    //R1 = new Rectangle(735, j * 40 + 220, 80, 40);//绘制护士签名内容的表格
                    //g.DrawRectangle(Pens.Black, R1);
                    #endregion                titleHeight += j * 40 + 220;
                    j++;                //if (y >= e.PageBounds.Height - titleHeight) // 如果已经到了底部则开始下一页
                    //if (y >= 28000 - titleHeight) // 如果已经到了底部则开始下一页
                    if(titleHeight>=11300)
                    {
                        e.HasMorePages = true;//分页
                        count = i + 1;
                        j = 0;                    //分页之后 打印的 j*70 + 105 中的j应该从0算起 
                        flagTitle = 1;//若是续打 则传个标志 如果是下一页打印 1 则打印表头
                        return;
                    }
                }
            }
        }
    }