这是我写的第一个打印程序,虽然很基本,但是我很高兴,拿出来和初学者和新人一起分享吧!我做了个简单的Demo有需要的朋友可以下载:ftp://[email protected]:2124/猫猫要努力学打印系列之网格数据.rarusing System;
using System.Collections.Generic;
using System.Text;
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;
            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;
                    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);
                    x = x + column[j] + 10;
                }
                e.Graphics.DrawLine(new Pen(Color.Red), x1Row, yRow, x, yRow);
                yRow = yRow + fnt.GetHeight(e.Graphics) + 10;
                y = y + fnt.GetHeight(e.Graphics) + 10;                if (yRow + 10 + fnt.GetHeight(e.Graphics) > e.MarginBounds.Height)
                {
                    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;
                e.Graphics.DrawLine(new Pen(Color.Red), x, e.MarginBounds.Top, x, yRow);
            }
            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);
        }
        /// <summary>
        /// 重置 PrintArray 此方法在打印或打印预览完成时调用
        /// </summary>
        public void Dispose()
        {
            t = 0;
            rowIndex = 0; 
            currentRowIndex = 0;
        }
    }
}

解决方案 »

  1.   

    呵呵谢谢大家我也是学习前辈 flygoldfish(长江支流) 他们
      

  2.   

    怎么样不注释介绍一下呀,还有好像下载的文件少了一个打印类的源码呀?ClassLibrary2.dll没有源码看不到
      

  3.   

    csdsuper(齐天)类 的源码就是这贴子里面,demo里面直接调用了ClassLibrary2.dll 所以就没必要附加源码了。这是一个刚写好的版本,还有很多功能不完善(比如:是否打印网格线,自定义初始行的 X,Y点等等),所以我就没写注解上来 有什么不清楚的可以加我QQ65321128或到Blog留言 希望很大家交流。呵呵
      

  4.   

    关于listBox的问题 
    如果有一个按钮是“重命名字段”
    有一个textbox 用来写你要改的名字
    如何在事件中把选中的项的内容改掉?并且让它保持原来的索引位置。用什么方法?
    private void resetname_Click(object sender, System.EventArgs e)
    {
    ??????
    }