今年毕业了,好不容易在上海一家软件公司找到工作最近才实习一个月多一点,在一个项目组里写写代码什么的,也算是我的毕业设计.有个报表打印的程序要我来写,是从DataGrid中打印出来的^说真的,c#本来就不熟悉,何况还是做这么麻烦的事……于是,我先在网上找了一下,也发现不少相关的代码.但是,不是写的太空洞,就是用别的语言,如vb写的,虽然还能看得懂,但却用不起来,几乎没有能够找到可以用的代码,只有一点借鉴的意义吧于是,我自己花了一天的时间,完完整整地写了下来,贴在这里供网友参考,希望高手们给点意见。说明:我是在vs2003.net里做的,所以有些控件布置什么的就不做说明,和一般的打印程序一样,都只有那么几个.具体的名字什么的,可以根据我的定义名称来判断,比如,dlgPrint就是一个打印控件等。using System;
using System.IO;
using System.Drawing;
using System.Drawing.Printing;
using System.Data;
using System.Windows.Forms;//这里隐去公司内部的项目类空间namespace *.*.Manage//这里隐去公司内部的项目类空间
{
 /// <summary>
 /// 
 /// </summary>
 public class frmCourseTable : System.Windows.Forms.Form
 {
  private System.Windows.Forms.ToolBar toolBar1;
  private System.Windows.Forms.ImageList imageList1;
  private System.Windows.Forms.ToolBarButton btnPreView;
  private System.Windows.Forms.ToolBarButton btnPrint;
  private System.Windows.Forms.ToolBarButton btnSave;
  private System.Windows.Forms.ToolBarButton btnClose;
  private System.Windows.Forms.ToolBarButton btnSeparator2;
  private System.Windows.Forms.ToolBarButton btnSeparator1;
  private System.Windows.Forms.DataGrid dgClasses;
  private System.Windows.Forms.DataGridTableStyle dataGridTableStyle1;
  private System.Windows.Forms.DataGridTextBoxColumn dataGridTextBoxColumn1;
  private System.Windows.Forms.DataGridTextBoxColumn dataGridTextBoxColumn2;
  private System.Windows.Forms.DataGridTextBoxColumn dataGridTextBoxColumn3;
  private System.Windows.Forms.DataGridTextBoxColumn dataGridTextBoxColumn4;
  private System.Windows.Forms.DataGridTextBoxColumn dataGridTextBoxColumn5;
  private System.Windows.Forms.DataGridTextBoxColumn dataGridTextBoxColumn6;
  private System.Windows.Forms.DataGridTextBoxColumn dataGridTextBoxColumn7;
  private System.Windows.Forms.DataGridTextBoxColumn dataGridTextBoxColumn8;
  private System.Windows.Forms.DataGridTextBoxColumn dataGridTextBoxColumn9;
  
  private System.ComponentModel.IContainer components = null;
  private System.Windows.Forms.PrintPreviewDialog dlgPreview;
  private System.Drawing.Printing.PrintDocument printCourseDoc;
  private System.Windows.Forms.ToolBarButton btnPrintSetup;
  private System.Windows.Forms.PageSetupDialog dlgPrintSetup;
  private System.Windows.Forms.PrintDialog dlgPrint;
  private System.Windows.Forms.PrintPreviewDialog printPreviewDialog1;  private string fileName = "未命名";
  private DataTable dgTable;
  private int ColsCount;//列数
  private int PrintRecordComplete = 0;//已经打印完的记录行数
  private int PrintingPageNumber = 0;//正要打印的页,初始为0
  private int printLines;//当前页共要分成多少行,其值由“纵向打印”还是“横向打印”来决定
          //是针对整个页面来说的,不管是否有内容。该常量只用于划分单元格,不做打印之用  private int PrintRecordNumber = 45;//每页要打印的记录的行数,初始设定
             //针对每页要打印的记录的行数来说的,注意与pringLines的区别,一般来说该常量数值应小于printLines  private int PageRecordNumber = 0;//当前要打印的行数,由计算得到
  private int PrintingLine = 0;//当前正在打印的行数
  private PointF DrawPoint;//(标题的)位置在顶部的正中央
  private Brush DrawBrush = Brushes.Black;//画笔颜色
  int x;
  int y;
  int X_unit;
  int Y_unit;  public frmCourseTable()
  {
   InitializeComponent();
  }  /// <summary>
  /// 清理所有正在使用的资源。
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if(components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }  private void frmCourseTable_Load(object sender, System.EventArgs e)
  {
   try
   {
    Class cla = new Class();//这里是公司内部的项目类
    dgTable = cla.ListAllClasses();
    dgTable.TableName = "Result";
    dgClasses.DataSource = dgTable;
    dgClasses.CaptionText = "课程列表";
   }
   catch(Exception ex)
   {
    Common.ShowErrorDetail(ex.Message);
   }
  }  #region Windows 窗体设计器生成的代码
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {
   this.components = new System.ComponentModel.Container();
   System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(frmCourseTable));
   this.toolBar1 = new System.Windows.Forms.ToolBar();
   this.btnPreView = new System.Windows.Forms.ToolBarButton();
   this.btnPrintSetup = new System.Windows.Forms.ToolBarButton();
   this.btnPrint = new System.Windows.Forms.ToolBarButton();
   this.btnSeparator1 = new System.Windows.Forms.ToolBarButton();
   this.btnSave = new System.Windows.Forms.ToolBarButton();
   this.btnSeparator2 = new System.Windows.Forms.ToolBarButton();
   this.btnClose = new System.Windows.Forms.ToolBarButton();
   this.imageList1 = new System.Windows.Forms.ImageList(this.components);
   this.dgClasses = new System.Windows.Forms.DataGrid();
   this.dataGridTableStyle1 = new System.Windows.Forms.DataGridTableStyle();
   this.dataGridTextBoxColumn1 = new System.Windows.Forms.DataGridTextBoxColumn();
   this.dataGridTextBoxColumn2 = new System.Windows.Forms.DataGridTextBoxColumn();
   this.dataGridTextBoxColumn3 = new System.Windows.Forms.DataGridTextBoxColumn();
   this.dataGridTextBoxColumn4 = new System.Windows.Forms.DataGridTextBoxColumn();
   this.dataGridTextBoxColumn5 = new System.Windows.Forms.DataGridTextBoxColumn();
   this.dataGridTextBoxColumn6 = new System.Windows.Forms.DataGridTextBoxColumn();
   this.dataGridTextBoxColumn7 = new System.Windows.Forms.DataGridTextBoxColumn();
   this.dataGridTextBoxColumn8 = new System.Windows.Forms.DataGridTextBoxColumn();
   this.dataGridTextBoxColumn9 = new System.Windows.Forms.DataGridTextBoxColumn();
   this.dlgPreview = new System.Windows.Forms.PrintPreviewDialog();
   this.printCourseDoc = new System.Drawing.Printing.PrintDocument();
   this.dlgPrintSetup = new System.Windows.Forms.PageSetupDialog();
   this.dlgPrint = new System.Windows.Forms.PrintDialog();
   this.printPreviewDialog1 = new System.Windows.Forms.PrintPreviewDialog();
   ((System.ComponentModel.ISupportInitialize)(this.dgClasses)).BeginInit();
   this.SuspendLayout();
   // 
   // toolBar1
   // 
   this.toolBar1.AutoSize = false;
   this.toolBar1.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[] {
                      this.btnPreView,
                      this.btnPrintSetup,
                      this.btnPrint,
                      this.btnSeparator1,
                      this.btnSave,
                      this.btnSeparator2,
                      this.btnClose});
   this.toolBar1.DropDownArrows = true;
   this.toolBar1.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
   this.toolBar1.ImageList = this.imageList1;
   this.toolBar1.Location = new System.Drawing.Point(0, 0);
   this.toolBar1.Name = "toolBar1";
   this.toolBar1.ShowToolTips = true;
   this.toolBar1.Size = new System.Drawing.Size(416, 32);
   this.toolBar1.TabIndex = 0;
   this.toolBar1.TextAlign = System.Windows.Forms.ToolBarTextAlign.Right;
   this.toolBar1.ButtonClick += new System.Windows.Forms.ToolBarButtonClickEventHandler(this.toolBar1_ButtonClick);

解决方案 »

  1.   

    // 
       // btnPreView
       // 
       this.btnPreView.ImageIndex = 0;
       this.btnPreView.ToolTipText = "打印预览";
       // 
       // btnPrintSetup
       // 
       this.btnPrintSetup.ImageIndex = 3;
       this.btnPrintSetup.ToolTipText = "打印设置";
       // 
       // btnPrint
       // 
       this.btnPrint.ImageIndex = 1;
       this.btnPrint.ToolTipText = "打印";
       // 
       // btnSeparator1
       // 
       this.btnSeparator1.Style = System.Windows.Forms.ToolBarButtonStyle.Separator;
       // 
       // btnSave
       // 
       this.btnSave.ImageIndex = 2;
       this.btnSave.ToolTipText = "保存";
       // 
       // btnSeparator2
       // 
       this.btnSeparator2.Style = System.Windows.Forms.ToolBarButtonStyle.Separator;
       // 
       // btnClose
       // 
       this.btnClose.Text = "关闭";
       this.btnClose.ToolTipText = "关闭";
       // 
       // imageList1
       // 
       this.imageList1.ImageSize = new System.Drawing.Size(16, 16);
       this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream")));
       this.imageList1.TransparentColor = System.Drawing.Color.Transparent;
       // 
       // dgClasses
       // 
       this.dgClasses.CaptionVisible = false;
       this.dgClasses.DataMember = "";
       this.dgClasses.Dock = System.Windows.Forms.DockStyle.Fill;
       this.dgClasses.HeaderForeColor = System.Drawing.SystemColors.ControlText;
       this.dgClasses.Location = new System.Drawing.Point(0, 32);
       this.dgClasses.Name = "dgClasses";
       this.dgClasses.ReadOnly = true;
       this.dgClasses.Size = new System.Drawing.Size(416, 261);
       this.dgClasses.TabIndex = 1;
       this.dgClasses.TableStyles.AddRange(new System.Windows.Forms.DataGridTableStyle[] {
                              this.dataGridTableStyle1});
       // 
       // dataGridTableStyle1
       // 
       this.dataGridTableStyle1.DataGrid = this.dgClasses;
       this.dataGridTableStyle1.GridColumnStyles.AddRange(new System.Windows.Forms.DataGridColumnStyle[] {
                                  this.dataGridTextBoxColumn1,
                                  this.dataGridTextBoxColumn2,
                                  this.dataGridTextBoxColumn3,
                                  this.dataGridTextBoxColumn4,
                                  this.dataGridTextBoxColumn5,
                                  this.dataGridTextBoxColumn6,
                                  this.dataGridTextBoxColumn7,
                                  this.dataGridTextBoxColumn8,
                                  this.dataGridTextBoxColumn9});
       this.dataGridTableStyle1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
       this.dataGridTableStyle1.MappingName = "Result";
       this.dataGridTableStyle1.ReadOnly = true;
       // 
       // dataGridTextBoxColumn1
       // 
       this.dataGridTextBoxColumn1.Format = "";
       this.dataGridTextBoxColumn1.FormatInfo = null;
       this.dataGridTextBoxColumn1.HeaderText = "课程号";
       this.dataGridTextBoxColumn1.MappingName = "ID";
       this.dataGridTextBoxColumn1.Width = 55;
       // 
       // dataGridTextBoxColumn2
       // 
       this.dataGridTextBoxColumn2.Format = "";
       this.dataGridTextBoxColumn2.FormatInfo = null;
       this.dataGridTextBoxColumn2.HeaderText = "教师号";
       this.dataGridTextBoxColumn2.MappingName = "TeachNo";
       this.dataGridTextBoxColumn2.Width = 40;
       // 
       // dataGridTextBoxColumn3
       // 
       this.dataGridTextBoxColumn3.Format = "";
       this.dataGridTextBoxColumn3.FormatInfo = null;
       this.dataGridTextBoxColumn3.HeaderText = "课程名称";
       this.dataGridTextBoxColumn3.MappingName = "Name";
       this.dataGridTextBoxColumn3.Width = 150;
       // 
       // dataGridTextBoxColumn4
       // 
       this.dataGridTextBoxColumn4.Format = "";
       this.dataGridTextBoxColumn4.FormatInfo = null;
       this.dataGridTextBoxColumn4.HeaderText = "教师名称";
       this.dataGridTextBoxColumn4.MappingName = "TeachName";
       this.dataGridTextBoxColumn4.Width = 90;
       // 
       // dataGridTextBoxColumn5
       // 
       this.dataGridTextBoxColumn5.Format = "";
       this.dataGridTextBoxColumn5.FormatInfo = null;
       this.dataGridTextBoxColumn5.HeaderText = "学分";
       this.dataGridTextBoxColumn5.MappingName = "Credit";
       this.dataGridTextBoxColumn5.Width = 10;
       // 
       // dataGridTextBoxColumn6
       // 
       this.dataGridTextBoxColumn6.Format = "";
       this.dataGridTextBoxColumn6.FormatInfo = null;
       this.dataGridTextBoxColumn6.HeaderText = "实际人数";
       this.dataGridTextBoxColumn6.MappingName = "RealNum";
       this.dataGridTextBoxColumn6.Width = 10;
       // 
       // dataGridTextBoxColumn7
       // 
       this.dataGridTextBoxColumn7.Format = "";
       this.dataGridTextBoxColumn7.FormatInfo = null;
       this.dataGridTextBoxColumn7.HeaderText = "额定人数";
       this.dataGridTextBoxColumn7.MappingName = "TOTALNUM";
       this.dataGridTextBoxColumn7.Width = 10;
       // 
       // dataGridTextBoxColumn8
       // 
       this.dataGridTextBoxColumn8.Format = "";
       this.dataGridTextBoxColumn8.FormatInfo = null;
       this.dataGridTextBoxColumn8.HeaderText = "上课时间";
       this.dataGridTextBoxColumn8.MappingName = "TimeText";
       this.dataGridTextBoxColumn8.Width = 120;
       // 
       // dataGridTextBoxColumn9
       // 
       this.dataGridTextBoxColumn9.Format = "";
       this.dataGridTextBoxColumn9.FormatInfo = null;
       this.dataGridTextBoxColumn9.HeaderText = "上课地点";
       this.dataGridTextBoxColumn9.MappingName = "Room";
       this.dataGridTextBoxColumn9.Width = 120;
       // 
       // dlgPreview
       // 
       this.dlgPreview.AutoScrollMargin = new System.Drawing.Size(0, 0);
       this.dlgPreview.AutoScrollMinSize = new System.Drawing.Size(0, 0);
       this.dlgPreview.ClientSize = new System.Drawing.Size(400, 300);
       this.dlgPreview.Document = this.printCourseDoc;
       this.dlgPreview.Enabled = true;
       this.dlgPreview.Icon = ((System.Drawing.Icon)(resources.GetObject("dlgPreview.Icon")));
       this.dlgPreview.Location = new System.Drawing.Point(230, 20);
       this.dlgPreview.MinimumSize = new System.Drawing.Size(375, 250);
       this.dlgPreview.Name = "dlgPreview";
       this.dlgPreview.TransparencyKey = System.Drawing.Color.Empty;
       this.dlgPreview.Visible = false;
       // 
       // printCourseDoc
       // 
       this.printCourseDoc.BeginPrint += new System.Drawing.Printing.PrintEventHandler(this.printCourseDoc_BeginPrint);
       this.printCourseDoc.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.printCourseDoc_PrintPage);
       // 
       // dlgPrintSetup
       // 
       this.dlgPrintSetup.Document = this.printCourseDoc;
       // 
       // dlgPrint
       // 
       this.dlgPrint.Document = this.printCourseDoc;
       // 
       // printPreviewDialog1
       // 
       this.printPreviewDialog1.AutoScrollMargin = new System.Drawing.Size(0, 0);
       this.printPreviewDialog1.AutoScrollMinSize = new System.Drawing.Size(0, 0);
       this.printPreviewDialog1.ClientSize = new System.Drawing.Size(400, 300);
       this.printPreviewDialog1.Enabled = true;
       this.printPreviewDialog1.Icon = ((System.Drawing.Icon)(resources.GetObject("printPreviewDialog1.Icon")));
       this.printPreviewDialog1.Location = new System.Drawing.Point(176, 176);
       this.printPreviewDialog1.MinimumSize = new System.Drawing.Size(375, 250);
       this.printPreviewDialog1.Name = "printPreviewDialog1";
       this.printPreviewDialog1.TransparencyKey = System.Drawing.Color.Empty;
       this.printPreviewDialog1.Visible = false;
       // 
       // frmCourseTable
       // 
       this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
       this.ClientSize = new System.Drawing.Size(416, 293);
       this.Controls.Add(this.dgClasses);
       this.Controls.Add(this.toolBar1);
       this.Name = "frmCourseTable";
       this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
       this.Text = "课程列表";
       this.Load += new System.EventHandler(this.frmCourseTable_Load);
       ((System.ComponentModel.ISupportInitialize)(this.dgClasses)).EndInit();
       this.ResumeLayout(false);  }
      #endregion
      

  2.   

    private void toolBar1_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
      {
       try
       {
        if(e.Button == btnPreView)
        {
         dlgPreview.ShowDialog();//打印预览
        }
        if(e.Button == btnPrintSetup)
        {
         dlgPrintSetup.ShowDialog();//打印设置
        }
        if(e.Button == btnPrint)
        {
         if(dlgPrint.ShowDialog() == DialogResult.OK)
         {
          printCourseDoc.Print();//打印
         }
        }
        if(e.Button == btnSave)
        {
         SaveFileDlg();//保存
        }
        if(e.Button == btnClose)
        {
         this.Close();//关闭
        }
       }
       catch(Exception ex)
       {
        MessageBox.Show(ex.Message, "打印出现错误",
         MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
       }
      }  protected void SaveFileDlg()
      {//这里保存文件的代码未完成
      }  protected void SaveFile()
      {//这里保存文件的代码未完成
      }  //从“打印设置PrintSetup”中获取相关参数,进行打印
      //这里有个问题:如果选择纸张大小为“自定义大小”,那么在PrintPreview的时候就会出错,为什么呢?
      private void printCourseDoc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
      {
       int Cols = 0;
       x = e.MarginBounds.Left;//纸张可写处的最左边
       y = e.MarginBounds.Top;//纸张可写处的最上端   IsFullPage(dgTable, PrintingPageNumber, PrintRecordNumber);   
          
       int columnCounts = 0;//定义DataGrid中的列数
       string[] colText = new string[columnCounts];//定义一个与DataGrid中的列数相同大小的数组,存放其中的字段名称
       string[] colID = new string[columnCounts];//定义一个存放字段属性值(如ID,TeacherName)等的数组,打印DataGrid中的数据时要用到
       int[] colSize = new int[columnCounts];//定义一个存放各个字段大小的数组   //取得DataGrid中的列数,以及各个列的名称,并赋给ColText数组
       foreach(DataGridTableStyle myGridStyle in dgClasses.TableStyles)
       {
        string[] headerText = new string[myGridStyle.GridColumnStyles.Count];
        string[] headerID = new string[myGridStyle.GridColumnStyles.Count];
        int[] columnSize = new int[myGridStyle.GridColumnStyles.Count];    //遍历dgClasses中的每个表所有的字段
        foreach(DataGridColumnStyle myColumnStyle in myGridStyle.GridColumnStyles)
        {
         headerText[columnCounts++] = myColumnStyle.HeaderText;//获得字段名称,如“学号”等
         headerID[columnCounts-1] = myColumnStyle.MappingName;//获取字段属性值,如“ID”等
         columnSize[columnCounts-1] = myColumnStyle.Width;
        }
        colText = headerText;//赋给前者
        colID = headerID;//赋给前者
        colSize = columnSize;//赋给前者
       }
       
       try
       {
        //将当前页分成基本的单元
        if(printCourseDoc.DefaultPageSettings.Landscape == true)
        {
         X_unit = e.MarginBounds.Height / (columnCounts + 5);//[横向打印时]除以“要打印的字段总数+5”,得到纵向间隔大小
         Y_unit = e.MarginBounds.Width / printLines;
        }
        else
        {
         X_unit = e.MarginBounds.Width / (columnCounts + 5);//[纵向打印]除以“要打印的字段总数+5”,得到横向间隔大小
         Y_unit = e.MarginBounds.Height / printLines;
        }
       }
       catch(Exception ex)
       {
        Common.ShowErrorDetail(ex.Message);
       }
       
       //打印文档标题,自定义
       string strPrintTitle = dgClasses.CaptionText;//文档标题,这里为“课程列表”
       DrawPoint = new PointF(x + e.MarginBounds.Width/2 - 50, y );//设定标题的打印位置坐标(x,y)=(矩形最左边坐标+矩形的宽度的一半,矩形最上边的坐标)
       e.Graphics.DrawString(strPrintTitle, PrintFont("宋体", 20), DrawBrush, DrawPoint);//打印文档标题
       y += Y_unit * 2;//打印完标题,往下移动两个“Y_unit”单位   //打印列名
       for(Cols=0;Cols<columnCounts;Cols++)
       {
        if(Cols == 0)
        {
         DrawPoint.X = X_unit;//如果是第一列,则从X_unit开始
        }
        else
        {
         DrawPoint.X += colSize[Cols-1] + X_unit;//如果是第二个以后,则从每次往前移动一个单元格和前一个的字段大小
        }
        DrawPoint.Y = y;//设定当前列名的打印位置
                    e.Graphics.DrawString(colText[Cols], PrintFont("宋体", 10), DrawBrush, DrawPoint);//打印所有列名
       }   DrawPoint = new PointF(X_unit, y);
       DrawLine(DrawPoint, e);//在列名下面打印一条黑色直线
       y += Y_unit;//打印完全部列名,往下移动“Y_unit”个单位 
       //打印DataGrid中的数据
       while(PrintingLine<PageRecordNumber)
       {
        DataRow dgRow = dgTable.Rows[PrintRecordComplete];
        IsFullPage(dgTable, PrintingPageNumber, PrintRecordNumber);//判断最后一页的记录数是否可以打满一页,否则出错。    for(Cols=0;Cols<columnCounts;Cols++)
        {
         if(Cols == 0)
         {
          DrawPoint.X = X_unit;
         }
         else
         {
          DrawPoint.X += colSize[Cols-1] + X_unit;
         }
         DrawPoint.Y = y;
         e.Graphics.DrawString(dgRow[colID[Cols]].ToString(), PrintFont("宋体", 10), DrawBrush, DrawPoint);
        }
        y += Y_unit;//打印完一行,就往下移动“Y_unit”个单位
        PrintingLine += 1;//正在打印的行数+1
        PrintRecordComplete += 1;//已经打印的记录数+1    if(y > e.MarginBounds.Bottom)
        {
         DrawPoint.X = X_unit * 1;
         DrawPoint.Y = y;
         DrawLine(DrawPoint, e);//在每个页面的底尾打印一条黑色直线     e.HasMorePages = true;//如果已经到达纸张底部,则开始新页     PrintingPageNumber += 1;//正要打印的页数+1
         PageRecordNumber = 0;//将当前要打印的行数置0
         PrintingLine = 0;//将正在打印的行数置0
         return;
        }
       }   e.HasMorePages = false;
      }  //只调用一次,用于初始化
      private void printCourseDoc_BeginPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
      {
       dgTable = (DataTable)dgClasses.DataSource;
       ColsCount = dgTable.Columns.Count;   //当前页是横向还是纵向打印,并计算当前页总共可以打印的行数
       if(printCourseDoc.DefaultPageSettings.Landscape == false)
       {
        printLines = printCourseDoc.DefaultPageSettings.PaperSize.Height / (PrintFont("宋体", 10).Height + 10);//纵向打印的情况
       }
       else
       {
        printLines = printCourseDoc.DefaultPageSettings.PaperSize.Width / (PrintFont("宋体", 10).Height + 10);//横向打印的情况
       }
      }  //计算,余下的记录条数是否还可以在一页打印,不满一页时为假
      protected void IsFullPage(DataTable dgTable, int PrintingPageNumber, int PrintRecordNumber)
      {
       //计算,余下的记录条数是否还可以在一页打印,不满一页时为假
       if(dgTable.Rows.Count - PrintingPageNumber * PrintRecordNumber >= PrintRecordNumber)
       {
        PageRecordNumber = PrintRecordNumber;//余下的条数还可以打满一页,将“每页可打印条数”赋给“当前要打印的行数”
       }
       else
       {
        PageRecordNumber = (dgTable.Rows.Count - PrintingPageNumber * PrintRecordNumber) % PrintRecordNumber;
       }
      }  //Draw a line with a black pen.
      protected void DrawLine(PointF point, System.Drawing.Printing.PrintPageEventArgs e)
      {
       Pen blackPen = new Pen(System.Drawing.Color.Black, 1);
       e.Graphics.DrawLine(blackPen, point.X, point.Y + PrintFont("宋体", 10).Height, point.X * ColsCount, point.Y + PrintFont("宋体", 10).Height);
      }  //返回字体设置
      protected Font PrintFont(string font, int size)
      {
       return new Font(font,size);
      }
     }
    }
      

  3.   


    真麻烦,还要分成三次发布~
    另外,我的msn是[email protected],欢迎版上的c#爱好者和我交流,我很希望能向各位学习提高
      

  4.   

    flygoldfish(长江支流) ,你好啊!
    很巧的是,在我写完这些粗陋的代码的时候,我才看到你发布的金质打印通及源码
    我也很仔细地看了,也进行了详细的测试和分析……感觉您的代码真的非常的好,我都感觉自己无法面对我那粗陋的代码了……不过可惜的是,其中的一个组件GoldPrint.dll你没能公开源码,对我们这些渴望学习更上一层楼的人来说,真是一大遗憾啊……
    至于你说的封装,我会努力尝试的,希望得到你的指导和支持!谢谢!
      

  5.   

    To:lynn0821(林子)楼主也无需谦虚,你能做成这一步也就可以做的比我更好。只要了解了打印及分页的原理,我想大家都会比我做得更好,只是需要时间去重构。我那个完全开源的打印足可以学习了,那也是GoldPrint.dll的核心源码。如果大家想交流打印,可以到如下网址阅读我写的文并下载源码。www.webmis.com.cn
      

  6.   

    谢谢大家的支持!
    五一放假这几天,由于出去玩了,所以没有做得更好
    至于 xiaomatian(趴趴熊◎%#……※  说的写成类库,我还不是很理解
    至于 flygoldfish(长江支流) 说的那些,我会花时间去看的。
    谢谢你们。我希望能有高手将其优化,因为我自己实在能力有限……
      

  7.   

    TO: fastyou(不甘落后-鸵鸟也可以飞) 
    我才初学c#,以后请多指教啊~
      

  8.   

    及人之所急啊,我会好好看的,我加尼msn了,以后多帮助阿!
      

  9.   

    最近才把论文忙完,周末就要回学校了
    还没有来得及写啊……不过我已经学着写了一个将DataGrid中的数据写入Excel的类库ToExcelDLL.dll——其实很简单,我都不好意思拿出来了,只是我想通过它来认识更多的高手(大家不要笑话我的水平啊:0~)借口是一个DataTable类型的参数,使用代码如下:
    DataTable dgTable;ToExcelDLL.myExcel myExcel = new ToExcelDLL.myExcel();myExcel.ToExcel(dgTable);
    //ToExcelDLL为nameplace,myExcel为类名,ToExcel 为其中的方法
    如果需要的朋友留个联系方式,如果有太多email的话,希望能有一个群发无限多地址的邮件软件来帮我……
    嘿嘿~
      

  10.   

    最好是能做成类,这样调用起来要方便一些。刚接触打印,打算做一个打印listview分页的类,再做一个直接打印任意多项目、不会页的类。看到楼主好文,应该可以借鉴许多,呵呵。谢谢了。等我看完了,再来发点扁言