dbgrid只能把数据一个一个的插入EXCEL中

解决方案 »

  1.   

    XlsApp : OleVariant;
        XlsSheet: OleVariant;
        I,J : Integer;
        SelField,SelRec : Integer;
        PrintAll : Boolean;
      if DBGrid1.SelectedRows.Count = 0 then
      begin
        if MessageDlg('当前没有选定打印的记录,是否全部打印?',mtconfirmation,[Mbyes,Mbno],0) = mrno then
        begin
          Application.MessageBox(pchar('没有选定的数据记录,打印终止!'),pchar('注意'),mb_ok);
          Abort;
        end else
          PrintAll := true;    //全部打印标志
      end;  try
        if VarIsEmpty(XlsApp) then
          XlsApp := Createoleobject('Excel.Application');
          XLsApp.Workbooks.Add;
          XlsSheet := XLsApp.Worksheets['Sheet1'];
      except
        Application.MessageBox(pchar('Ms Excel 无法启动,数据转入终止!'),pchar('注意'),mb_ok);
        Abort;
      end;  SelField := 0;
      for  J := 0 to Dbgrid1.Columns.Count-1 do
      begin
        if (dbgrid1.Columns[j].Visible<>false) and (dbgrid1.Columns[j].color<>clgray) then
        begin
          SelField := SelField + 1;
          XlsSheet.Cells[3, SelField] := dbgrid1.Columns[j].Title.Caption;
        end;
      end;  SelRec := 0;
      with dm1.QryMain do
      begin
        first;
        for  I := 0 to RecordCount-1 do
        begin
          if not PrintAll then
          begin
            if not DBGrid1.SelectedRows.CurrentRowSelected then
            begin
              Next;
              Continue
            end;
          end;      SelRec := SelRec + 1;
          SelField := 0;
          for J := 0 to Dbgrid1.Columns.Count-1 do
          begin
            if (dbgrid1.Columns[j].Visible<>false) and (dbgrid1.Columns[j].color<>clAqua) then
            begin
              SelField  := SelField + 1;
              if StrToIntDef(Fields[J].AsString, -1) <> -1 then
                XlsSheet.Cells[selRec + 3, SelField] := '''' + Fields[J].Asstring
              else
                XlsSheet.Cells(selRec + 3, SelField) := Fields[J].Asstring
            end;
          end;
          Next;
        end;
      end;
      XlsApp.Visible := true;
      

  2.   

    DevExpress Grid 的cxGrid可以