如何用QuickReport把StringGrid的内容打印出来?

解决方案 »

  1.   

    procedure PrintStringGrid(Grid: TStringGrid; Title: string; 
      Orientation: TPrinterOrientation); 
    var 
      P, I, J, YPos, XPos, HorzSize, VertSize: Integer; 
      AnzSeiten, Seite, Zeilen, HeaderSize, FooterSize, ZeilenSize, FontHeight: Integer; 
      mmx, mmy: Extended; 
      Footer: string; 
    begin 
      HeaderSize := 100;
      FooterSize := 200; 
      ZeilenSize := 36; 
      FontHeight := 36;   Printer.Orientation := Orientation;
      Printer.Title  := Title; 
      Printer.BeginDoc; 
      mmx := GetDeviceCaps(Printer.Canvas.Handle, PHYSICALWIDTH) /
        GetDeviceCaps(Printer.Canvas.Handle, LOGPIXELSX) * 25.4;
      mmy := GetDeviceCaps(Printer.Canvas.Handle, PHYSICALHEIGHT) / 
        GetDeviceCaps(Printer.Canvas.Handle, LOGPIXELSY) * 25.4;   VertSize := Trunc(mmy) * 10; 
      HorzSize := Trunc(mmx) * 10; 
      SetMapMode(Printer.Canvas.Handle, MM_LOMETRIC);   Zeilen := (VertSize - HeaderSize - FooterSize) div ZeilenSize;
      if Grid.RowCount mod Zeilen <> 0 then 
        AnzSeiten := Grid.RowCount div Zeilen + 1 
      else 
        AnzSeiten := Grid.RowCount div Zeilen;   Seite := 1; 
      for P := 1 to AnzSeiten do 
      begin 
        Printer.Canvas.Font.Height := 48; 
        Printer.Canvas.TextOut((HorzSize div 2 - (Printer.Canvas.TextWidth(Title) div 2)), 
          - 20,Title); 
        Printer.Canvas.Pen.Width := 5; 
        Printer.Canvas.MoveTo(0, - HeaderSize); 
        Printer.Canvas.LineTo(HorzSize, - HeaderSize); 
        Printer.Canvas.MoveTo(0, - VertSize + FooterSize); 
        Printer.Canvas.LineTo(HorzSize, - VertSize + FooterSize); 
        Printer.Canvas.Font.Height := 36; 
        Footer := 'Seite: ' + IntToStr(Seite) + ' von ' + IntToStr(AnzSeiten); 
        Printer.Canvas.TextOut((HorzSize div 2 - (Printer.Canvas.TextWidth(Footer) div 2)), 
          - VertSize + 150,Footer); 
        Printer.Canvas.Font.Height := FontHeight; 
        YPos := HeaderSize + 10; 
        for I := 1 to Zeilen do
        begin 
          if Grid.RowCount >= I + (Seite - 1) * Zeilen then 
          begin 
            XPos := 0; 
            for J := 0 to Grid.ColCount - 1 do 
            begin 
              Printer.Canvas.TextOut(XPos, - YPos, 
                Grid.Cells[J, I + (Seite - 1) * Zeilen - 1]); 
              XPos := XPos + Grid.ColWidths[J] * 3; 
            end; 
            YPos := YPos + ZeilenSize; 
          end; 
        end; 
        Inc(Seite); 
        if Seite <= AnzSeiten then Printer.NewPage; 
      end; 
      Printer.EndDoc; 
    end;
      

  2.   

    为什么非得用qreport呢,你可以在报表里设计和DBGIRD的数据库是一个,然后打印。不过建议使用FR,里面专门有一个控件,PRINTGRID,和PRINTTABEL可以实现你的要求
      

  3.   

    全局变量 i
    var
      i : integer;QuickRep1的onBeforePrint事件:  i:=0;QuickRep1的onNeedData事件:
      MoreData := (i<=StringGrid1.RowCount - 1);
      if MoreData then
      begin
        QRLabel1.Caption:=StringGrid1.Cells[0,i];
        QRLabel2.Caption:=StringGrid1.Cells[1,i];
        QRLabel3.Caption:=StringGrid1.Cells[2,i];
        QRLabel4.Caption:=StringGrid1.Cells[3,i];
        QRLabel5.Caption:=StringGrid1.Cells[4,i];
        QRLabel6.Caption:=StringGrid1.Cells[5,i];
        QRLabel12.Caption:=StringGrid1.Cells[6,i];
        QRLabel13.Caption:=StringGrid1.Cells[7,i];
        ......//看你需要    Inc(i);
      end;
      

  4.   

    用ehlib控件搞定好了
    http://www.2ccc.com/dl.asp?url=http://downloads.2ccc.com/vcl/grids/EhLib.v3.4.hh.by.Netrobo.rar它自代这个功能,还有更好用的呢
      

  5.   

    Win2000+SP4下通过,可以根据条改变某一行的颜色:
    TQuickRep的OnNeedData事件,注意思:不要指定任何数据源!procedure TReportPreviewForm.ListRepNeedData(Sender: TObject;
      var MoreData: Boolean);
    begin
    MoreData:=(I<=MainForm.StringGrid1.RowCount-1);
    IDLabel.Caption:=Trim(MainForm.StringGrid1.Cells[0,i]);
    AreaLabel.Caption:=Trim(MainForm.StringGrid1.Cells[1,i]);
    ItemNameLabel.Caption:=Trim(MainForm.StringGrid1.Cells[2,i]);
    if Trim(MainForm.StringGrid1.Cells[1,i])='' then
      ItemNameLabel.Alignment:=taRightJustify
    else
      ItemNameLabel.Alignment:=taLeftJustify;
    SortNameLabel.Caption:=Trim(MainForm.StringGrid1.Cells[3,i]);
    PinPaiLabel.Caption:=Trim(MainForm.StringGrid1.Cells[4,i]);
    DanWeiLabel.Caption:=Trim(MainForm.StringGrid1.Cells[5,i]);
    PriceLabel.Caption:=Trim(MainForm.StringGrid1.Cells[6,i]);
    ICountLabel.Caption:=Trim(MainForm.StringGrid1.Cells[7,i]);
    MoneyLabel.Caption:=Trim(MainForm.StringGrid1.Cells[8,i]);
    QRShape201.Brush.Color:=MainForm.StringGrid1.Colors[8,i];
    if (Trim(MainForm.ItemList.Cells[1,i])='')and(i<>MainForm.StringGrid1.RowCount-1) then
      QRShape1.Brush.Color:=clBlue; //变色;
    i:=i+1; //I为全局变量,初值为:0;
    end;