[已到期] [未解决]
如何用。NET实现报表打印?详细!谢!
提问人:李颖颖  回答:3  浏览:1142  提问时间:2010/1/17 11:36:27  到期时间:2010/1/22 11:36:27  悬赏分:10+5+20
在。NET环境下,如何实现报表的打印,是需要安装特殊的配置文件?
补充问题 我来回答
此问题的回答(2)
张雪峰
请说明用的什么软件创建的报表啊。
回答者:张雪峰  2010/1/17 17:22:43
肖皓
using System;
  using System.Windows.Forms;
  using System.Drawing;
  using System.Drawing.Printing;
  using System.Data;
  using System.Collections;
  using DataLibrary;
  namespace ControlLibrary
  {
  /// 
  /// DataGrid打印
  /// 
  public class DataGridPrinter
  {
  private PrintDocument ThePrintDocument;
  private DataTable TheTable;
  private DataGridTheDataGrid;
  public int RowCount = 0;
  private const int kVerticalCellLeeway = 10;
  public int PageNumber = 1;
  public ArrayList Lines = new ArrayList();
  public int header=0;
  public int footer=0;
  DataCatena DataCatena1;
  public int PageWidth;
  public int PageHeight;
  public int TopMargin;
  public int BottomMargin;
  int GridWidth = 0;
  public int LeftMargin;
  public int RightMargin;
  public string PaperName;
  public int pagecount;
  private int rows_page;
  public bool showfooter;
  public int bodylength
  {
  get
  {
  if(this.PageNumber
  return (1+this.rows_page) * ((int)(TheDataGrid.Font.SizeInPoints)+ kVerticalCellLeeway);
  else
  return (this.TheTable.Rows.Count + 1 -(this.rows_page*(this.PageNumber-1))) * ( (int)
  (TheDataGrid.Font.SizeInPoints)+ kVerticalCellLeeway);
  }
  }
  public DataGridPrinter(DataGrid aGrid, PrintDocument aPrintDocument,string theform,int Header,int Footer)
  {
  //
  // TODO: Add constrUCtor logic here
  //
  this.header=Header;
  this.footer=Footer;
  this.showfooter=true;
  TheDataGrid = aGrid;
  ThePrintDocument = aPrintDocument;
  if(theform!="")
  {
  DataCatena1=new DataCatena();
  DataView myDV=DataCatena1.GetDataView("select * from yh_bbdy where bm='"+theform +"'");
  this.PaperName=myDV[0]["zm"].ToString();
  PaperSize pkSize;
  if(myDV[0]["fx"].ToString()=="True")
  this.ThePrintDocument.DefaultPageSettings.Landscape=true;
  else
  this.ThePrintDocument.DefaultPageSettings.Landscape=false;
  int found=0;
  for (int i = 0; i 
  {
  pkSize = this.ThePrintDocument.PrinterSettings.PaperSizes[i];
  if(pkSize.PaperName==this.PaperName)
  {
  this.ThePrintDocument.DefaultPageSettings.PaperSize=pkSize;
  found=1;
  i=this.ThePrintDocument.PrinterSettings.PaperSizes.Count;
  if(this.ThePrintDocument.DefaultPageSettings.Landscape)
  {
  PageHeight = pkSize.Width;
  PageWidth = pkSize.Height;
  }
  else
  {
  PageWidth = pkSize.Width;
  PageHeight = pkSize.Height;
  }
  }
  }
  if(found==0)
  {
  if(this.ThePrintDocument.DefaultPageSettings.Landscape)
  {
  PageHeight = Int32.Parse(myDV[0]["zk"].ToString());
  PageWidth = Int32.Parse(myDV[0]["zc"].ToString());
  }
  else
  {
  PageWidth = Int32.Parse(myDV[0]["zk"].ToString());
  PageHeight = Int32.Parse(myDV[0]["zc"].ToString());
  }
  }
  TopMargin = Int32.Parse(myDV[0]["sk"].ToString());
  BottomMargin = Int32.Parse(myDV[0]["xk"].ToString());
  RightMargin = Int32.Parse(myDV[0]["rightk"].ToString());
  LeftMargin = Int32.Parse(myDV[0]["leftk"].ToString());
  }
  else
  {
  PageWidth = ThePrintDocument.DefaultPageSettings.PaperSize.Width;
  PageHeight = ThePrintDocument.DefaultPageSettings.PaperSize.Height;
  TopMargin = ThePrintDocument.DefaultPageSettings.Margins.Top;
  BottomMargin = ThePrintDocument.DefaultPageSettings.Margins.Bottom;
  RightMargin = ThePrintDocument.DefaultPageSettings.Margins.Right;
  LeftMargin = ThePrintDocument.DefaultPageSettings.Margins.Left;
  }
  PageWidth -= this.LeftMargin ;
  PageWidth -= this.RightMargin;
  this.rows_page=(int)((float)(this.PageHeight-this.TopMargin-this.BottomMargin-this.footer-this.header)
  /(TheDataGrid.Font.SizeInPoints + kVerticalCellLeeway));
  for (int k = 0; k 
  {
  GridWidth += TheDataGrid.TableStyles[0].GridColumnStyles[k].Width;//TheTable.Columns[k].ToString();
  }
  }
  public void DrawHeader(Graphics g)
  {
  SolidBrush ForeBrush = new SolidBrush(TheDataGrid.HeaderForeColor);
  SolidBrush BackBrush = new SolidBrush(Color.White); //(TheDataGrid.HeaderBackColor);
  Pen TheLinePen = new Pen(TheDataGrid.GridLineColor, 1);
  StringFormat cellformat = new StringFormat();
  cellformat.Trimming = StringTrimming.EllipsisCharacter;
  cellformat.FormatFlags = StringFormatFlags.NoWrap StringFormatFlags.LineLimit;
  cellformat.Alignment= StringAlignment.Center;
  
  int columnwidth = 0;
  int initialRowCount = RowCount;
  // draw the table header
  float startXPosition = this.LeftMargin;//TheDataGrid.Location.X;
  RectangleF nextcellbounds = new RectangleF(0,0, 0, 0);
  /* 
  这一段是画表头底色的代码
  RectangleF HeaderBounds= new RectangleF(0, 0, 0, 0);
  HeaderBounds.X = this.LeftMargin; //TheDataGrid.Location.X;
  HeaderBounds.Y =+ (RowCount - initialRowCount) * (TheDataGrid.Font.SizeInPoints+ kVerticalCellLeeway);
  HeaderBounds.Height = TheDataGrid.Font.SizeInPoints + kVerticalCellLeeway;
  HeaderBounds.Width = PageWidth;
  */
  float y1=(float)(header+this.TopMargin);
  g.DrawLine(TheLinePen, this.LeftMargin, y1, this.LeftMargin + this.PageWidth, y1);
  y1=y1+ (float)(TheDataGrid.Font.SizeInPoints + kVerticalCellLeeway);
  g.DrawLine(TheLinePen, this.LeftMargin, y1, this.LeftMargin + this.PageWidth, y1);
  // g.FillRectangle(BackBrush, HeaderBounds);
  for (int k = 0; k 
  {
  columnwidth = TheDataGrid.TableStyles[0].GridColumnStyles[k].Width * PageWidth / this.GridWidth;
  string nextcolumn = TheDataGrid.TableStyles[0].GridColumnStyles[k].HeaderText;
  //TheTable.Columns[k].ToString(); + (RowCount - initialRowCount) * (TheDataGrid.Font.SizeInPoints
  + kVerticalCellLeeway)
  RectangleF cellbounds = new RectangleF(startxposition, header + TopMargin+2 ,
  columnwidth, 
  TheDataGrid.HeaderFont.SizeInPoints + kVerticalCellLeeway-2);
  nextcellbounds = cellbounds;
  if (startxposition + columnwidth 
  {
  g.DrawString(nextcolumn, TheDataGrid.HeaderFont, ForeBrush, cellbounds, cellformat);
  }
  startxposition = startxposition + columnwidth;
  }
  y1=y1+ (float)(TheDataGrid.Font.SizeInPoints + kVerticalCellLeeway);
  g.DrawLine(TheLinePen, this.LeftMargin, y1, this.LeftMargin + this.PageWidth, y1);
  //if (TheDataGrid.GridLineStyle != DataGridLineStyle.None)
  // g.DrawLine(TheLinePen, TheDataGrid.Locatio
回答者:肖皓  2010/1/18 10:59:27
同类快到期问题