首页 > 代码库 > .Net常用技巧_打印DataGridView(转)

.Net常用技巧_打印DataGridView(转)

选择相应的列进行打印报表、只需实例化带一个DataGridView参数的构造函数就可以使用

class PrintGridViewData    {        public  PrintGridViewData()        { }        private DataGridView DGridView;        public  PrintGridViewData(DataGridView DGView)        {            DGridView = DGView;        }        private Form dialogForm = new Form();        private CheckedListBox CListBox = new CheckedListBox();        private Button Bton = new Button();        public void ControlAdd()        {            dialogForm.StartPosition = FormStartPosition.CenterParent;            dialogForm.Size = new Size(300, 330);            dialogForm.MinimizeBox = false;            dialogForm.MaximizeBox = false;            dialogForm.Text = "请选择打印的列";            dialogForm.Controls.Add(CListBox);            CListBox.Size = new Size(200, 300);            CListBox.Location = new Point(dialogForm.Location.X, dialogForm.Location.Y);            dialogForm.Controls.Add(Bton);            Bton.Text = "打印";            Bton.Size = new Size(75, 23);            Bton.Location = new Point(dialogForm.Location.X + 210, dialogForm.Location.Y + 180);            Bton.Click +=new EventHandler(Bton_Click);            for (int i = 0; i < DGridView.ColumnCount; i++)            {                CListBox.Items.Add(DGridView.Columns[i].HeaderText);            }            dialogForm.Show();        }        private String[] ArrayStr;        private int ArrayCount=0;        private void Bton_Click(object sender, EventArgs e)        {            ArrayCount = CListBox.CheckedItems.Count;            ArrayStr = new String[ArrayCount];            for (int i = 0; i < CListBox.CheckedItems.Count; i++)            {                ArrayStr[i] = CListBox.CheckedItems[i].ToString();            }                        PrintTable();                   }        public void PrintTable()        {            dialogForm.Close();            PrintDocument PDocument = new PrintDocument();            PDocument.DocumentName = "打印Table";            PDocument.PrintPage +=new PrintPageEventHandler(PDocument_PrintPage);            PrintDialog PDialog = new PrintDialog();            PDialog.Document = PDocument;            PrintPreviewDialog PPreviewDialog = new PrintPreviewDialog();            PPreviewDialog.Document = PDocument;            PPreviewDialog.ClientSize = new System.Drawing.Size(1024, 768);            if (PDialog.ShowDialog() == DialogResult.OK)            {                PageSetupDialog PSetupDialog= new PageSetupDialog();                PSetupDialog.Document = PDocument;                PSetupDialog.Document.DefaultPageSettings.Landscape = true;//设置默认为横向打印                if (PSetupDialog.ShowDialog() == DialogResult.OK)                {                    if (PPreviewDialog.ShowDialog() == DialogResult.OK)                    {                        PDocument.Print();                    }                }            }                    }        private int PrintRow = 0;        private void PDocument_PrintPage(object sender,PrintPageEventArgs e)        {            Graphics Gh = e.Graphics;            String Tablehead = "人事资料表";  //表头名称            Font headFont = new Font("宋体", 18, FontStyle.Bold);  //表头字体            int headHeight = headFont.Height;  //表头的占用的高度            Font FieldFont = new Font("宋体", 14, FontStyle.Bold);  //表字段名的字体            int FieldHeight = FieldFont.Height;   //表字段名占用的高度            Font DateFont = new Font("宋体", 12);  //表头打印日期的字体            int DateHeight = DateFont.Height;        //表头打印日期的字体占用的高度            Font SubjectFont = new Font("宋体", 13);  //表主体文本的字体            int SubjectHeight = SubjectFont.Height;  //表主体文本的字体占用的高度            int margin = 5;   //字符串与单元格外框上下的间距            int ColumnsWidth = e.PageBounds.Width/ArrayCount;   //列宽            Brush B = new SolidBrush(Color.Black);            Pen P = new Pen(Color.Black);            int x = e.PageBounds.X;            int y = e.PageBounds.Y;            int width = e.PageBounds.Width;            int height = e.PageBounds.Height-20;            //画表头名称            Gh.DrawString(Tablehead, headFont, B, x + width / 2 - ((int)(Gh.MeasureString(Tablehead, headFont)).Width / 2), 0);            //画跟在表头下面的打印日期(与表头名称间隔10px)            Gh.DrawString("打印日期:" + System.DateTime.Now.Date.ToString().Substring(0, 10), DateFont, B, x, y + headHeight + 10);            //画表字段名上的第一条横线            Gh.DrawLine(P, x, y + headHeight + 10 + DateHeight, x + width, y + headHeight + 10 + DateHeight);            //画表的字段名            int FieldIndex = 0;            for (int i = 0; i < DGridView.ColumnCount; i++)            {                  //当字符串数组ArrayStr包含指定的字符串时返回true                  bool trueORfalse = ((IList<String>)ArrayStr).Contains(DGridView.Columns[i].HeaderText);                  if (trueORfalse)                  {                      Gh.DrawString(DGridView.Columns[i].HeaderText.ToString(), FieldFont, B, x + FieldIndex * ColumnsWidth, y + headHeight + 10 + DateHeight + margin);                      FieldIndex++;                  }            }            //画表字段名下的横线            Gh.DrawLine(P, x, y + headHeight + 10 + DateHeight + margin + FieldHeight + margin, x + width, y + headHeight + 10 + DateHeight + margin + FieldHeight + margin);            //计算表头和字段名之间占用的高度            int headToSubjectHeight = y + headHeight + 10 + DateHeight + margin + FieldHeight + margin;            //除去表头和字段名之间占用的高度,毎页画的最大行数            int PageRowCount = (height - (y + headHeight + 10 + DateHeight + margin + FieldHeight + margin)) / (SubjectFont.Height + 10);            //减去已打印的行,余下的行数            int count = DGridView.RowCount - PrintRow;            if (PageRowCount >= count)            {                int rowIndex = 0;//打印的行数                int columnIndex = 0;//打印的列数                while (DGridView.RowCount > PrintRow)                {                    for (int z = 0; z < DGridView.ColumnCount; z++)                    {                        try                        {                            DGridView[z, PrintRow].Value.ToString();                            //当字符串数组ArrayStr包含指定的字符串时返回true                            bool trueORfalse = ((IList<String>)ArrayStr).Contains(DGridView.Columns[z].HeaderText);                            if (trueORfalse)                            {                                //打印毎个单元格,单元格的字符串与一下两条横线的间距为margin这个变量的大小                                Gh.DrawString(DGridView[z, PrintRow].Value.ToString(), SubjectFont, B, x + columnIndex * ColumnsWidth, y + headToSubjectHeight + margin + rowIndex * (SubjectHeight + 2 * margin));                                columnIndex++;                            }                            //打印完一行数据,画一条横线                            Gh.DrawLine(P, x, y + headToSubjectHeight + (rowIndex + 1) * (SubjectHeight + 2 * margin), x + width, y + headToSubjectHeight + (rowIndex + 1) * (SubjectHeight + 2 * margin));                        }                        catch                        {                            break;                        }                    }                    columnIndex = 0;                    rowIndex++;                    PrintRow++;                }                for (int k = 0; k < ArrayCount + 1; k++)//画竖的直线                {                    Gh.DrawLine(P, x + ColumnsWidth * k, y + headHeight + 10 + DateHeight, x + ColumnsWidth * k, y + headHeight + 10 + DateHeight + rowIndex * (SubjectHeight + 2 * margin));                }            }            else  //下面的与上面的大同小异,主要区分用的循环语句            {                int rowIndex = 0;                int columnIndex = 0;                do                {                    for (int z = 0; z < DGridView.ColumnCount; z++)                    {                        try                        {                             //当字符串数组ArrayStr包含指定的字符串时返回true                             bool trueORfalse = ((IList<String>)ArrayStr).Contains(DGridView.Columns[z].HeaderText);                             if (trueORfalse)                             {                                 Gh.DrawString(DGridView[z, PrintRow].Value.ToString(), SubjectFont, B, x + columnIndex * ColumnsWidth, y + headToSubjectHeight + margin + rowIndex * (SubjectHeight + 2 * margin));                                 columnIndex++;                             }                            Gh.DrawLine(P, x, y + headToSubjectHeight + (rowIndex + 1) * (SubjectHeight + 2 * margin), x + width, y + headToSubjectHeight + (rowIndex + 1) * (SubjectHeight + 2 * margin));                        }                        catch                        {                            break;                        }                    }                    columnIndex = 0;                    rowIndex++;                    PrintRow++;                } while (PrintRow % PageRowCount > 0);//当求余的结果不为0,则说明没有打满一页,继续执行。否则打满,跳出循环。                for (int k = 0; k < ArrayCount + 1; k++)//画竖的直线                {                    Gh.DrawLine(P, x + ColumnsWidth * k, y + headHeight + 10 + DateHeight, x + ColumnsWidth * k, y + headHeight + 10 + DateHeight + (rowIndex+1) * (SubjectHeight + 2 * margin));                }            }            if (DGridView.RowCount > PrintRow)            {                e.HasMorePages = true;//返回true时,则还有数据没打印出来,再执打印,打印附加页            }            else            {                e.HasMorePages = false;                PrintRow = 0;            }        }           }