猫猫要努力学打印系列之网格数据

来源:互联网 发布:wps表格数据连接 编辑:程序博客网 时间:2024/06/06 18:02

这是我写的第一个打印程序,虽然很基本,但是我很高兴,拿出来和初学者和新人一起分享吧!

我做了个简单的Demo有需要的朋友可以下载:ftp://leike@211.137.100.89:2124/猫猫要努力学打印系列之网格数据.rar

提示:下面的类代码是没有任何Bug的(至少作者没有发现),Demo里面会出现一些Bug。

           原因是我更新了类代码,没有去更新Demo 呵呵  不好意思了。如果有Bug欢迎指出谢谢.

using System.Drawing;
using System.Drawing.Printing;

namespace 企业资源计划
{
    /// <summary>
    /// 打印数组
    /// </summary>
    class PrintArray
    {
        int t = 0;
        int rowIndex = 0 //行索引
            , currentRowIndex = 0;//当前行索引
        /// <summary>
        /// 打印二维数组的方法
        /// </summary>
        /// <param name="e">为 System.Drawing.Printing.PrintDocument.PrintPage 事件提供数据</param>
        /// <param name="Array2">string类型的二维数组 string[,] Array2</param>
        public void PrintArray2(PrintPageEventArgs e, string[,] Array2)
        {
            float x, y;
            //string[,] printTable = ToStringArray(dataGridView1, true);//dataGridView 转换为二维数组
            SolidBrush s = new SolidBrush(Color.Red);//字体颜色
            Font fnt = new Font("宋体", 9F, FontStyle.Regular);//字体大小和样式

            y = e.MarginBounds.Top;//上边距
            x = e.MarginBounds.Left;//左边距

            float[] column = new float[Array2.GetLength(1)];//列宽
            float x1Row = (float)e.MarginBounds.Left
              , yRow = (float)e.MarginBounds.Top
              , x2Row = (float)e.MarginBounds.Right;

            for (int i = 0; i < Array2.GetLength(1); i++)
            {
                column[i] = 0;//列宽
                for (int j = rowIndex; j < Array2.GetLength(0); j++)
                {
                    if (column[i] < e.Graphics.MeasureString(Array2[j, i], fnt).Width)//取出当前列宽度最宽的字符串
                    {
                        column[i] = e.Graphics.MeasureString(Array2[j, i], fnt).Width;
                    }
                    yRow = yRow + fnt.GetHeight(e.Graphics) + 10;//当前行y点 + 字高 + 字行距 = 下行y点
                    if (yRow + 10 + fnt.GetHeight(e.Graphics) > e.MarginBounds.Height)//判断是否到了打印页最后一行
                    {
                        currentRowIndex = j;
                        break;
                    }
                }
                yRow = (float)e.MarginBounds.Top;
            }

            rowIndex = currentRowIndex;//记录当前行索引

            for (; t < Array2.GetLength(0); t++)
            {
                for (int j = 0; j < Array2.GetLength(1); j++)
                {
                    e.Graphics.DrawString(Array2[t, j], fnt, s, x + 5, y + 6);//字体边距 + 5,字体行距 + 6;
                    x = x + column[j] + 10;//字体与字体距离 + 10;
                    //if (x > e.MarginBounds.Width)
                    //{
                    //    break;
                    //}
                }
                e.Graphics.DrawLine(new Pen(Color.Red), x1Row, yRow, x, yRow);//横线
                yRow = yRow + fnt.GetHeight(e.Graphics) + 10;//当前行y点 + 字高 + 字行距 = 下行y点
                y = y + fnt.GetHeight(e.Graphics) + 10;//当前字y点 + 字高 + 字行距 = 下行字y点

                if (yRow + 10 + fnt.GetHeight(e.Graphics) > e.MarginBounds.Height)//判断下一行y点是否超出了打印页面的高度
                {
                    e.HasMorePages = true;
                    t++;
                    break;
                }
                x = e.MarginBounds.Left;
            }

            x = e.MarginBounds.Left;
            for (int i = 0; i < column.Length; i++)
            {
                x = x + column[i] + 10;//字体与字体距离 + 10;
                e.Graphics.DrawLine(new Pen(Color.Red), x, e.MarginBounds.Top, x, yRow);//竖线(右端线包含在内)
                //if (x > e.MarginBounds.Width)
                //{
                //    //MessageBox.Show("您打印的行超出了页面");
                //    break;
                //}
            }
            e.Graphics.DrawLine(new Pen(Color.Red), e.MarginBounds.Left, e.MarginBounds.Top, e.MarginBounds.Left, yRow);//左端线
            e.Graphics.DrawLine(new Pen(Color.Red), x1Row, yRow, x, yRow);//低线
            if (e.HasMorePages == false)//如果是最后一页了,重置全局变量
            {
                t = 0;
                rowIndex = 0; //行索引
                currentRowIndex = 0;//当前行索引
            }
        }
    }
}

 

原创粉丝点击