要用QReport中的控件,你需要作报表。

解决方案 »

  1.   

    用Excel打印DBGrid中的内容:
    procedure TBaseFrm.Excel1Click(Sender: TObject);
    var
      ExcelApp, EWorkBook, EWorkSheet: OleVariant;
      sRange: string;
      iCols, i, iVisible, j, k: Integer;
      oPrintGrid: TDBGrid;
    begin
      oPrintGrid := FGFrame1.DBGrid1;
      iCols := oPrintGrid.FieldCount;
      iVisible := 0;
      j := 0;
      k := 0;  for i := 0 to iCols - 1 do // Iterate
      begin
        if not oPrintGrid.Columns[i].Visible then //确定非可见列的数目
          inc(iVisible);
      end; // for  iCols := iCols - iVisible;  if iCols > 256 then
      begin
        MessageBox(Handle, '总列数大于256,已超出Excel的可用列范围!',
          MBCS, MB_OK + MB_ICONSTOP);
        Exit;
      end;  if CheckExcel then
      begin
        try // statements to try
          ExcelApp := CreateOleObject('Excel.Application'); //生成Excel1Click对象
          ExcelApp.Caption := Caption; //设定工作簿标题
          ExcelApp.SheetsInNewWorkbook := 1; //设置新增工作簿包含的Sheet的数目
          EWorkBook := ExcelApp.WorkBooks.Add; //新增工作簿
          EWorkSheet := ExcelApp.ActiveSheet;      {页面设置}
    //      EWorkSheet.PageSetup.CenterHeader := '报表演示'; //页眉
          EWorkSheet.PageSetup.CenterFooter := '第&P页'; //页脚
          EWorkSheet.PageSetup.HeaderMargin := 1 / 0.035; //页眉到顶端的边距
          EWorkSheet.PageSetup.FooterMargin := 1 / 0.035; //页脚到顶端的边距
          EWorkSheet.PageSetup.TopMargin := 1 / 0.035; //顶边距
          EWorkSheet.PageSetup.BottomMargin := 1 / 0.035; //底边距
          EWorkSheet.PageSetup.LeftMargin := 1 / 0.035; //左边距
          EWorkSheet.PageSetup.RightMargin := 1 / 0.035; //右边距
          EWorkSheet.PageSetup.CenterHorizontally := True; //页面水平居中
    //      EWorkSheet.PageSetup.CenterVertically := True; //页面垂直居中      {表头部分}
          if iCols <= 6 then
          begin
            {标题}
            sRange := 'A1:F1'; //确定标题合并范围
            EWorkSheet.Range[sRange].MergeCells := True; //合并单元格
            ExcelApp.Selection.HorizontalAlignment := $FFFFEFF4; //水平居中
            ExcelApp.Selection.VerticalAlignment := $FFFFEFF4; //垂直居中
            ExcelApp.Selection.Value := Caption; //设置标题格式
            ExcelApp.Selection.Font.Name := '华文中宋';
            ExcelApp.Selection.Font.Size := 20;        {表头}
            EWorkSheet.Range['A2:B2'].MergeCells := True;
            ExcelApp.Cells[2, 1].Value := '操作员:' + AppInfo.Uname;
            sRange := 'C2:D2';
            EWorkSheet.Range[sRange].MergeCells := True;
            ExcelApp.Cells[2, 3].Value := '打印日期:';
            ExcelApp.Cells[2, 3].HorizontalAlignment := $FFFFEFC8; //靠右
            sRange := 'E2:F2';
            EWorkSheet.Range[sRange].MergeCells := True;
            ExcelApp.Cells[2, 5].Value := DateToStr(Date);
            ExcelApp.Cells[2, 5].HorizontalAlignment := $FFFFEFDD; //靠左
          end
          else begin
            EWorkSheet.PageSetup.Orientation := 2; //横打        {标题}
            sRange := 'A1:' + NumToLetter(iCols) + '1'; //确定标题合并范围
            EWorkSheet.Range[sRange].MergeCells := True; //合并单元格
            ExcelApp.Selection.HorizontalAlignment := $FFFFEFF4; //水平居中
            ExcelApp.Selection.VerticalAlignment := $FFFFEFF4; //垂直居中
            ExcelApp.Selection.Value := Caption; //设置标题格式
            ExcelApp.Selection.Font.Name := '华文中宋';
            ExcelApp.Selection.Font.Size := 20;        {表头}
            EWorkSheet.Range['A2:B2'].MergeCells := True;
            ExcelApp.Cells[2, 1].Value := '操作员:' + AppInfo.Uname;
            sRange := NumToLetter(iCols - 3) + '2:' + NumToLetter(iCols - 2) + '2';
            EWorkSheet.Range[sRange].MergeCells := True;
            ExcelApp.Cells[2, iCols - 3].Value := '打印日期:';
            ExcelApp.Cells[2, iCols - 3].HorizontalAlignment := $FFFFEFC8; //靠右
            sRange := NumToLetter(iCols - 1) + '2:' + NumToLetter(iCols) + '2';
            EWorkSheet.Range[sRange].MergeCells := True;
            ExcelApp.Cells[2, iCols - 1].Value := DateToStr(Date);
            ExcelApp.Cells[2, iCols - 1].HorizontalAlignment := $FFFFEFDD; //靠左
          end;      {列标部分}
          k := 1;
          for i := 0 to oPrintGrid.FieldCount - 1 do // Iterate
          begin
            if oPrintGrid.Columns[i].Visible then
            begin
              ExcelApp.Cells[3, k].Value := oPrintGrid.Columns[i].Title.Caption;
              ExcelApp.Cells[3, k].Font.Bold := True;
              ExcelApp.Cells[3, k].HorizontalAlignment := $FFFFEFF4;
              EWorkSheet.Columns[k].ColumnWidth := oPrintGrid.Columns[i].Width / 8;
              Inc(k);
            end;
          end; // for      {输出数据}
          j := 4;
          k := 1;
          if TargetTb.RecordCount <= MExcelRows then //在最大打印行数范围内,不用换页
          begin
            TargetTb.First;
            while not TargetTb.Eof do
            begin
              for i := 0 to oPrintGrid.FieldCount - 1 do // Iterate
              begin
                if oPrintGrid.Columns[i].Visible then
                begin
                  ExcelApp.Cells[j, k].Value := oPrintGrid.Fields[i].Value;
                  Inc(k);
                end;
              end; // for
              TargetTb.Next;
              k := 1;
              Inc(j);
            end; // while
          end
          else begin //超出最大打印行数,换页打印
            {先复制一张格式WorkSheet}
            EWorkSheet := EWorkBook.WorkSheets[2];
            EWorkBook.WorkSheets[1].Copy(EWorkSheet);
          end;      {显示并预览报表}
          ExcelApp.Visible := True;
          EWorkBook.PrintPreview;      {释放资源}
          ExcelApp := Unassigned;
          EWorkBook := Unassigned;
          EWorkSheet := Unassigned;
        except
          ExcelApp := Unassigned;
          EWorkBook := Unassigned;
          EWorkSheet := Unassigned;
        end; // try/except
      end;
    end;