c#,winform打印,实现打印预览,设置打印头,打印datagridview中部分数据,并且自定义datagridview中列的宽度

发布时间 2023-09-01 15:56:26作者: wang王dd

关于打印,我研究了一阵子,网上找了一大堆,但是没有我想要的,然后自己用最原始的方法,利用绘图工具Pen,Brush,Font一个个画出来的

完整的代码在最下面,大家复制后显示表头改一下汉字里面对应的自己datagridview中的列名就好了,显示表体改一下datagridview中列在Select中对应的下标就可以了。

最后的效果如下:

图里面分三个部分:

第一个部分就是加粗字体的标题,页头和页尾的一下信息。

第二部分为datagridview中的部分列表的内容在打印中显示。

第三部分就是将合计对应的列打印出来。

接下来我就要介绍实现步骤了,数据在datagridview中显示和统计我就不介绍了,直接开始介绍打印的模板是怎么画出来的

在窗体中添加printDialog,printDocument,peintPreviewDialog,pageSetupDialog这四个控件

 1、双击printDialog,在属性里面Document中选中关联printDocument1

 

 2、双击peintPreviewDialog,在属性里面Document中选中关联printDocument1

 3、双击pageSetupDialog,在属性里面Document中选中关联printDocument1

 4、双击printDocument,在时间里面双击printPage

打印代码在这里面编写

接下来就是编码了、、、、、、

 

1、首先是图片上

 表头的部分内容代码如下:

string rq = DateTime.Now.ToString("yyyy-MM-dd");
                    int num1 = 200;
                    StringFormat stringFormat = new StringFormat();
                    stringFormat.Alignment = StringAlignment.Center;
                    stringFormat.LineAlignment = StringAlignment.Center;
                    Pen pen = new Pen(Color.Black, 1f);
                    Font font = new Font("楷体", 24f, FontStyle.Bold);
                    Font font2 = new Font("楷体", 12f, FontStyle.Bold);
                    Brush black = Brushes.Black;
                    e.Graphics.DrawString("华禹科技采购入库单", font, black, 245f, 50f);
                    e.Graphics.DrawString("供应商:", font2, black, 100f, 110f);
                    e.Graphics.DrawString(textBox1.Text, font2, black, 150f, 110f);
                    e.Graphics.DrawString("备注:", font2, black, 100f, 130f);
                    e.Graphics.DrawString(textBox6.Text, font2, black, 150f, 130f);
                    e.Graphics.DrawString("日期:", font2, black, 300f, 110f);
                    e.Graphics.DrawString(sj1, font2, black, 350f, 110f);
                    e.Graphics.DrawString("编号:", font2, black, 500f, 110f);
                    e.Graphics.DrawString(textBox5.Text, font2, black, 550f, 110f);
                    e.Graphics.DrawString("仓库:", font2, black, 500f, 130f);
                    e.Graphics.DrawString(comboBox1.Text, font2, black, 550f, 130f);

 内容和位置,大家可以复制此代码,改一下参数就好了

2、接下来就是打印datagridview列名的代码:红色标记的部分

 代码如下:

 foreach (var item in dataGridView2.Columns)
                    {
                        DataGridViewColumn dataGridViewColumn = (DataGridViewColumn)item;
                        if (dataGridViewColumn.HeaderText == "物料名称")
                        {
                            e.Graphics.DrawRectangle(pen, 100, 170, 220, 70);//画框框
                            e.Graphics.DrawString(dataGridViewColumn.HeaderText, new Font("楷体", 12f, FontStyle.Regular), Brushes.Black, new RectangleF(170, 160f, 80f, 50f), stringFormat);//在框框里面填写列名
                        }
                        if (dataGridViewColumn.HeaderText == "规格型号")
                        {
                            e.Graphics.DrawRectangle(pen, 320, 170, 120, 70);
                            e.Graphics.DrawString(dataGridViewColumn.HeaderText, new Font("楷体", 12f, FontStyle.Regular), Brushes.Black, new RectangleF(340, 160f, 80f, 50f), stringFormat);
                        }
                        if (dataGridViewColumn.HeaderText == "入库数量")
                        {
                            e.Graphics.DrawRectangle(pen, 440, 170, 80, 70);
                            e.Graphics.DrawString(dataGridViewColumn.HeaderText, new Font("楷体", 12f, FontStyle.Regular), Brushes.Black, new RectangleF(440, 160f, 80f, 50f), stringFormat);
                        }
                        if (dataGridViewColumn.HeaderText == "入库单价")
                        {
                            e.Graphics.DrawRectangle(pen, 520, 170, 80, 70);
                            e.Graphics.DrawString(dataGridViewColumn.HeaderText, new Font("楷体", 12f, FontStyle.Regular), Brushes.Black, new RectangleF(520, 160f, 80f, 50f), stringFormat);
                        }
                        if (dataGridViewColumn.HeaderText == "金额")
                        {
                            e.Graphics.DrawRectangle(pen, 600, 170, 60, 70);
                            e.Graphics.DrawString(dataGridViewColumn.HeaderText, new Font("楷体", 12f, FontStyle.Regular), Brushes.Black, new RectangleF(590, 160f, 80f, 50f), stringFormat);
                        }
                        if (dataGridViewColumn.HeaderText == "单位")
                        {
                            e.Graphics.DrawRectangle(pen, 660, 170, 60, 70);
                            e.Graphics.DrawString(dataGridViewColumn.HeaderText, new Font("楷体", 12f, FontStyle.Regular), Brushes.Black, new RectangleF(650, 160f, 80f, 50f), stringFormat);
                        }
                    }

3、最后就是显示列名下面的具体内容的代码了:

这里的横坐标和框框的宽度要与列名的一致,其他的大家根据自己慢慢调

for (int i = 0; i < dataGridView2.Rows.Count; i++)
                    {
                        for (int j = 0; j < dataGridView2.Columns.Count; j++)
                        {
                            if (j == 3)
                            {
                                e.Graphics.DrawRectangle(pen, 100, num1, 220, 40);
                                e.Graphics.DrawString(dataGridView2.Rows[i].Cells[j].Value.ToString(), new Font("楷体", 12f, FontStyle.Regular), Brushes.Black, new RectangleF(100f, (float)num1, 200f, 50f), stringFormat);
                            }
                            if (j == 4)
                            {
                                e.Graphics.DrawRectangle(pen, 320, num1, 120, 40);
                                e.Graphics.DrawString(dataGridView2.Rows[i].Cells[j].Value.ToString(), new Font("楷体", 12f, FontStyle.Regular), Brushes.Black, new RectangleF(320f, (float)num1, 100f, 50f), stringFormat);
                            }
                            if (j == 6)
                            {
                                e.Graphics.DrawRectangle(pen, 440, num1, 80, 40);
                                e.Graphics.DrawString(dataGridView2.Rows[i].Cells[j].Value.ToString(), new Font("楷体", 12f, FontStyle.Regular), Brushes.Black, new RectangleF(420f, (float)num1, 100f, 50f), stringFormat);
                            }
                            if (j == 7)
                            {
                                e.Graphics.DrawRectangle(pen, 520, num1, 80, 40);
                                e.Graphics.DrawString(dataGridView2.Rows[i].Cells[j].Value.ToString(), new Font("楷体", 12f, FontStyle.Regular), Brushes.Black, new RectangleF(500f, (float)num1, 100f, 50f), stringFormat);
                            }
                            if (j == 9)
                            {
                                e.Graphics.DrawRectangle(pen, 600, num1, 60, 40);
                                e.Graphics.DrawString(dataGridView2.Rows[i].Cells[j].Value.ToString(), new Font("楷体", 12f, FontStyle.Regular), Brushes.Black, new RectangleF(580f, (float)num1, 100f, 50f), stringFormat);
                            }
                            if (j == 10)
                            {
                                e.Graphics.DrawRectangle(pen, 660, num1, 60, 40);
                                e.Graphics.DrawString(dataGridView2.Rows[i].Cells[j].Value.ToString(), new Font("楷体", 12f, FontStyle.Regular), Brushes.Black, new RectangleF(640f, (float)num1, 100f, 50f), stringFormat);
                            }
                        }
                        num1 += 40;
                       
                    }

 

最后还有一段代码就是图片箭头指示的内容,他说根据表体的内容增加而自动往下递增的,

字段代码放到表体第一个for循环外面:

e.Graphics.DrawString("制单:", font2, black, 100f, num1+10);
                    e.Graphics.DrawString(User, font2, black, 150f, num1+10);

 

最后:我贴一下完整代码:

private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            //button2.PerformClick();
            try
            {
                bool flag = dataGridView2.Rows.Count > 0;
                if (flag)
                {

                    string rq = DateTime.Now.ToString("yyyy-MM-dd");
                    int num1 = 200;
                    StringFormat stringFormat = new StringFormat();
                    stringFormat.Alignment = StringAlignment.Center;
                    stringFormat.LineAlignment = StringAlignment.Center;
                    Pen pen = new Pen(Color.Black, 1f);
                    Font font = new Font("楷体", 24f, FontStyle.Bold);
                    Font font2 = new Font("楷体", 12f, FontStyle.Bold);
                    Brush black = Brushes.Black;
                    e.Graphics.DrawString("华禹科技采购入库单", font, black, 245f, 50f);
                    e.Graphics.DrawString("供应商:", font2, black, 100f, 110f);
                    e.Graphics.DrawString(textBox1.Text, font2, black, 150f, 110f);
                    e.Graphics.DrawString("备注:", font2, black, 100f, 130f);
                    e.Graphics.DrawString(textBox6.Text, font2, black, 150f, 130f);
                    e.Graphics.DrawString("日期:", font2, black, 300f, 110f);
                    e.Graphics.DrawString(sj1, font2, black, 350f, 110f);
                    e.Graphics.DrawString("编号:", font2, black, 500f, 110f);
                    e.Graphics.DrawString(textBox5.Text, font2, black, 550f, 110f);
                    e.Graphics.DrawString("仓库:", font2, black, 500f, 130f);
                    e.Graphics.DrawString(comboBox1.Text, font2, black, 550f, 130f);
                    foreach (var item in dataGridView2.Columns)
                    {
                        DataGridViewColumn dataGridViewColumn = (DataGridViewColumn)item;
                        if (dataGridViewColumn.HeaderText == "物料名称")
                        {
                            e.Graphics.DrawRectangle(pen, 100, 170, 220, 70);
                            e.Graphics.DrawString(dataGridViewColumn.HeaderText, new Font("楷体", 12f, FontStyle.Regular), Brushes.Black, new RectangleF(170, 160f, 80f, 50f), stringFormat);
                        }
                        if (dataGridViewColumn.HeaderText == "规格型号")
                        {
                            e.Graphics.DrawRectangle(pen, 320, 170, 120, 70);
                            e.Graphics.DrawString(dataGridViewColumn.HeaderText, new Font("楷体", 12f, FontStyle.Regular), Brushes.Black, new RectangleF(340, 160f, 80f, 50f), stringFormat);
                        }
                        if (dataGridViewColumn.HeaderText == "入库数量")
                        {
                            e.Graphics.DrawRectangle(pen, 440, 170, 80, 70);
                            e.Graphics.DrawString(dataGridViewColumn.HeaderText, new Font("楷体", 12f, FontStyle.Regular), Brushes.Black, new RectangleF(440, 160f, 80f, 50f), stringFormat);
                        }
                        if (dataGridViewColumn.HeaderText == "入库单价")
                        {
                            e.Graphics.DrawRectangle(pen, 520, 170, 80, 70);
                            e.Graphics.DrawString(dataGridViewColumn.HeaderText, new Font("楷体", 12f, FontStyle.Regular), Brushes.Black, new RectangleF(520, 160f, 80f, 50f), stringFormat);
                        }
                        if (dataGridViewColumn.HeaderText == "金额")
                        {
                            e.Graphics.DrawRectangle(pen, 600, 170, 60, 70);
                            e.Graphics.DrawString(dataGridViewColumn.HeaderText, new Font("楷体", 12f, FontStyle.Regular), Brushes.Black, new RectangleF(590, 160f, 80f, 50f), stringFormat);
                        }
                        if (dataGridViewColumn.HeaderText == "单位")
                        {
                            e.Graphics.DrawRectangle(pen, 660, 170, 60, 70);
                            e.Graphics.DrawString(dataGridViewColumn.HeaderText, new Font("楷体", 12f, FontStyle.Regular), Brushes.Black, new RectangleF(650, 160f, 80f, 50f), stringFormat);
                        }
                    }
                    for (int i = 0; i < dataGridView2.Rows.Count; i++)
                    {
                        for (int j = 0; j < dataGridView2.Columns.Count; j++)
                        {
                            if (j == 3)
                            {
                                e.Graphics.DrawRectangle(pen, 100, num1, 220, 40);
                                e.Graphics.DrawString(dataGridView2.Rows[i].Cells[j].Value.ToString(), new Font("楷体", 12f, FontStyle.Regular), Brushes.Black, new RectangleF(100f, (float)num1, 200f, 50f), stringFormat);
                            }
                            if (j == 4)
                            {
                                e.Graphics.DrawRectangle(pen, 320, num1, 120, 40);
                                e.Graphics.DrawString(dataGridView2.Rows[i].Cells[j].Value.ToString(), new Font("楷体", 12f, FontStyle.Regular), Brushes.Black, new RectangleF(320f, (float)num1, 100f, 50f), stringFormat);
                            }
                            if (j == 6)
                            {
                                e.Graphics.DrawRectangle(pen, 440, num1, 80, 40);
                                e.Graphics.DrawString(dataGridView2.Rows[i].Cells[j].Value.ToString(), new Font("楷体", 12f, FontStyle.Regular), Brushes.Black, new RectangleF(420f, (float)num1, 100f, 50f), stringFormat);
                            }
                            if (j == 7)
                            {
                                e.Graphics.DrawRectangle(pen, 520, num1, 80, 40);
                                e.Graphics.DrawString(dataGridView2.Rows[i].Cells[j].Value.ToString(), new Font("楷体", 12f, FontStyle.Regular), Brushes.Black, new RectangleF(500f, (float)num1, 100f, 50f), stringFormat);
                            }
                            if (j == 9)
                            {
                                e.Graphics.DrawRectangle(pen, 600, num1, 60, 40);
                                e.Graphics.DrawString(dataGridView2.Rows[i].Cells[j].Value.ToString(), new Font("楷体", 12f, FontStyle.Regular), Brushes.Black, new RectangleF(580f, (float)num1, 100f, 50f), stringFormat);
                            }
                            if (j == 10)
                            {
                                e.Graphics.DrawRectangle(pen, 660, num1, 60, 40);
                                e.Graphics.DrawString(dataGridView2.Rows[i].Cells[j].Value.ToString(), new Font("楷体", 12f, FontStyle.Regular), Brushes.Black, new RectangleF(640f, (float)num1, 100f, 50f), stringFormat);
                            }
                        }
                        num1 += 40;
                       
                    }
                    e.Graphics.DrawString("制单:", font2, black, 100f, num1+10);
                    e.Graphics.DrawString(User, font2, black, 150f, num1+10);
                    /*foreach (object obj in dataGridView2.Columns)
                           {
                               DataGridViewColumn dataGridViewColumn = (DataGridViewColumn)obj;
                               bool flag2 = !dataGridViewColumn.Visible;
                               if (!flag2)
                               {
                                   bool flag3 = dataGridViewColumn.HeaderText != "编号";
                                   if (flag3)
                                   {
                                       bool flag4 = dataGridViewColumn.HeaderText != "物料代码";
                                       if (flag4)
                                       {
                                           bool flag5 = dataGridViewColumn.HeaderText != "仓库代码";
                                           if (flag5)
                                           {

                                               bool flag7 = dataGridViewColumn.HeaderText != "库存数量";
                                               if (flag7)
                                               {
                                                   bool flag8 = dataGridViewColumn.HeaderText != "最新进价";
                                                   if (flag8)
                                                   {
                                                       bool flag9 = dataGridViewColumn.HeaderText != "库存金额";
                                                       if (flag9)
                                                       {
                                                           bool flag10 = dataGridViewColumn.HeaderText != "库位";
                                                           if (flag10)
                                                           {
                                                               bool flag11 = dataGridViewColumn.HeaderText != "重量数量";
                                                               if (flag11)
                                                               {
                                                                   bool flag12 = dataGridViewColumn.HeaderText != "重量单位";
                                                                   if (flag12)
                                                                   {
                                                                       bool flag13 = dataGridViewColumn.HeaderText != "仓库名称";
                                                                       if (flag13)
                                                                       {
                                                                           bool flag14 = dataGridViewColumn.HeaderText != "供应商";
                                                                           if (flag14)
                                                                           {
                                                                               bool flag15 = dataGridViewColumn.HeaderText != "备注";
                                                                               if (flag15)
                                                                               {
                                                                                   e.Graphics.DrawRectangle(pen, num, 170, 120, 550);
                                                                                   e.Graphics.DrawString(dataGridViewColumn.HeaderText, new Font("楷体", 12f, FontStyle.Regular), Brushes.Black, new RectangleF((float)num, 160f, 80f, 50f), stringFormat);
                                                                                   num += 120;
                                                                               }
                                                                           }
                                                                       }

                                                                   }
                                                               }
                                                           }
                                                       }
                                                   }
                                               }
                                           }
                                       }
                                   }
                               }
                           }
                           num = 50;
                           for (int i = 0; i < dataGridView2.Rows.Count; i++)
                           {
                               for (int j = 0; j < dataGridView2.Columns.Count; j++)
                               {
                                   if (j == 3 || j == 4 || j == 10 || j == 6 || j == 7 || j == 9)
                                   {
                                       e.Graphics.DrawRectangle(pen, num, num2, 120, 50);
                                       e.Graphics.DrawString(dataGridView2.Rows[i].Cells[j].Value.ToString(), new Font("楷体", 12f, FontStyle.Regular), Brushes.Black, new RectangleF((float)num, (float)num2, 100f, 50f), stringFormat);
                                       num += 120;
                                   }
                               }
                               num = 50;
                               num2 += 50;
                           }*/
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }