如何用QuickReport实现对ListView内容的打印我的联系方法:[email protected]
MSN: [email protected]

解决方案 »

  1.   

    //自己写:uses Printers;const ChiefTitle:string='大标题;
          InferiorTitle:string='小标题;function TMainForm.PrintListview(OListView: TListView):boolean;
    //-----------------------------------------------------
    var
      pWidth, pHeight, i: Integer;
      v, h: Real;
      CurItem,iColumnCount: Integer;
      aCols: array of Integer;
      iTotColsWidth, iInnerWidth, TopMarg, LinesOnPage, CurLine, TekstHeight, CurCol: Integer;
      CurRect: TRect;
      CurStr: string;
      CurLeft, NumPages, TmpPos: Integer;
    begin
      result:=false;
      if PrintDialog1.Execute then
      begin
        iColumnCount:= oListview.Columns.Count;
        SetLength(aCols,oListview.Columns.Count+ 1);
        Printer.Title := '打印报表-我是我'; //提交给打印机的名称
        Printer.Copies := 1;
        Printer.Orientation := poPortrait; //报表是垂直显示的
        Printer.BeginDoc;
        pHeight := Printer.PageHeight; //当前打印页的高度
        pWidth := Printer.PageWidth;   //当前打印页的宽度
        v := (pHeight + (2 * GetDeviceCaps(Printer.Handle, PHYSICALOFFSETY))) / (29.7 * 0.95);
        //0.95 用来给不同机器打印机做调整因素
        h := (pWidth + (2 * GetDeviceCaps(Printer.Handle, PHYSICALOFFSETX))) / 21;
        // 计算总的宽度
        iTotColsWidth := 0;
        for i := 0 to iColumnCount - 1 do
          iTotColsWidth := iTotColsWidth + oListView.Columns[i].Width;
        //计算间距
        aCols[0] := Round(1.5 * h); //left margin
        aCols[iColumnCount + 0] := pWidth - Round(1.5 * h); //rigth margin
        iInnerWidth := aCols[iColumnCount + 0] - aCols[0]; // space between margins
        //计算每行开始
        for i := 0 to iColumnCount - 1 do
          aCols[i + 1] := aCols[i] + Round(oListView.Columns[i].Width / iTotColsWidth * iInnerWidth);
        TopMarg := Round(2.5 * v);
        with Printer.Canvas do
        begin
          Font.Size := 10;
          Font.Style := [];
          Font.Name := 'Times New Roman';
          Font.Color := RGB(0, 0, 0);
          TekstHeight := Printer.Canvas.TextHeight('dummy');
          LinesOnPage := Round((PHeight - (5 * v)) / TekstHeight);
          NumPages := 1;
          // 得到所有的打印机列表
          while (NumPages * LinesOnPage) < oListView.Items.Count do
            inc(NumPages);
          // 开始打印
          CurLine := 0;
          for CurItem := 0 to oListView.Items.Count - 1 do
          begin
            if (CurLine > LinesOnPage) or (CurLine = 0) then
            begin
              if (CurLine > LinesOnPage) then Printer.NewPage;
              CurLine := 1;
              if Printer.PageNumber = NumPages then
              begin
                MoveTo(aCols[1], topMarg);
                for i := 1 to iColumnCount - 1 do
                begin
                  LineTo(aCols[i], TopMarg + (TekstHeight * (oListView.Items.Count - CurItem + 2)));
                  MoveTo(aCols[i + 1], topMarg);
                end;
              end
              else
              begin
                //在数据中移动垂直线 <显示分割线>
                for i := 1 to iColumnCount - 1 do
                begin
                  MoveTo(aCols[i], topMarg);
                  LineTo(aCols[i], TopMarg + (TekstHeight * (LinesOnPage + 1)));
                end;
              end;          Font.Style := [fsBold];
              // 打印listview的columns项目
              for i := 0 to iColumnCount - 1 do
              begin
                TextRect(Rect(aCols[i] + Round(0.1 * h), TopMarg - Round(0.1 * v), aCols[i + 1] - Round(0.1 * h)
                  , TopMarg + TekstHeight - Round(0.1 * v)), ((aCols[i + 1] - aCols[i]) div 2) +
                  aCols[i] - (TextWidth(oListview.Columns.Items[i].Caption) div 2),
                  TopMarg - Round(0.1 * v), oListview.Columns.Items[i].Caption);
              end;          // 在column headers之下拖动水平线
              MoveTo(aCols[0] - Round(0.1 * h), TopMarg + TekstHeight - Round(0.05 * v));
              LineTo(aCols[iColumnCount] + Round(0.1 * h), TopMarg + TekstHeight - Round(0.05 * v));          //打印日期和页码
              Font.Size := 8;
              Font.Style := [];
              TmpPos := (TextWidth('日期: ' + DateToStr(Date) + ' 页码: ' +
                IntToStr(Printer.PageNumber) + ' / ' + IntToStr(NumPages))) div 2;          TmpPos := PWidth - Round(1.5 * h) - (TmpPos * 2);          Font.Size := 8;
              Font.Style := [];
              TextOut(TmpPos,printer.PageHeight-Round(0.5 * v), '日期: ' + DateToStr(Date) +
                ' 页码: ' + IntToStr(Printer.PageNumber) + ' / ' + IntToStr(NumPages));          // 打印报表主要标题
              Font.Size := 18;
              TextOut((printer.pagewidth - TextWidth(ChiefTitle)) div 2, Round(0.9 * v),ChiefTitle);
                // 打印报表次要标题
              Font.Size := 14;
              TextOut((printer.pagewidth - TextWidth(InferiorTitle)) div 2, Round(1.6 * v),InferiorTitle);
              Font.Size := 10;
              Font.Style := [];
            end;        CurRect.Top := TopMarg + (CurLine * TekstHeight);
            CurRect.Bottom := TopMarg + ((CurLine + 1) * TekstHeight);        // 打印listview的内容
            for CurCol := -1 to iColumnCount - 2 do
            begin
              CurRect.Left := aCols[CurCol + 1] + Round(0.1 * h);
              CurRect.Right := aCols[CurCol + 2] - Round(0.1 * h);
              try
                if CurCol = -1 then
                  CurStr := oListView.Items[CurItem].Caption
                else
                  CurStr := oListView.Items[CurItem].SubItems[CurCol];
              except
                CurStr := '';
              end;
              CurLeft := CurRect.Left; // 排的左边
              //把数据写到TextRect
              TextRect(CurRect, CurLeft, CurRect.Top, CurStr);        end;
            Inc(CurLine);
          end;
        end;
         Printer.EndDoc;
         result:=true;
      end;end;
      

  2.   

    用stringband可以實現你的要求﹐看看它的幫助你就知道聽。
      

  3.   

    谁能介绍一下初学delphi做报表打印用什么报表工具?