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

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

完整的代码在最下面,大家复制后显示表头改一下汉字里面对应的自己datagridview中的列名就好了,显示表体改一下datagridview中列在Select中对应的下标就可以了。代码不难理解,就是画框框,大家可以复制后,慢慢调,改参数,慢慢玩就懂了

 

还有一个点:单击datagridview在属性里面找到截图这个:设置未为false,不让会空指针异常!!!!!!!

 

最后的效果如下:

图里面分三个部分:

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

第二部分为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);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

posted @   wang王dd  阅读(3361)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示