因为数据不在同一表里,导出excel的时候速度好慢,哪位兄台有办法解决吗?

解决方案 »

  1.   

    直接查询导出
    给你一个函数
    procedure TCustomCommonRoutine.SaveToExcel(FDataSet: array of TDataSet;
      Title, SubTitle, FileName: string);
    var
      i, j: Integer;
      sList: TStringList;
    begin
      sList := TStringList.Create;
      sList.Clear;
      if length(FDataSet) > 0 then
      begin
        sList.Add('<HTML>');
        sList.Add(#9 + '<HEAD>');
        sList.Add(#9 +
          '<meta http-equiv="content-type" content="text/html; charset=gb2312">');
        sList.Add(#9#9 + '<H2 align="center">');
        sList.Add(Title);
        sList.Add(#9#9 + '</H2>');
        sList.Add(#9#9 + '<H3 align="center">');
        sList.Add(SubTitle);
        sList.Add(#9#9 + '</H3>');
        sList.Add(#9 + '</HEAD>');
        sList.Add('<body>');
        sList.Add('<table width="100%" border="1">');
        for i := 0 to length(FDataSet) - 1 do
        begin
          with FDataSet[i] do
          begin
            sList.Add(#9 + '<tr>');
            for j := 0 to FDataSet[i].FieldCount - 1 do
            begin
              if FDataSet[i].Fields[j].Visible = true then
              begin
                sList.Add(#9#9 + '<td>');
                sList.Add(#9#9 + FDataSet[i].Fields[j].DisplayName);
                sList.Add(#9#9 + '</td>');
              end;
            end;
            sList.Add(#9 + '</tr>');
            if FDataSet[i].RecordCount > 0 then
            begin
              FDataSet[i].First;
              while not FDataSet[i].Eof do
              begin
                sList.Add(#9 + '<tr>');
                for j := 0 to FDataSet[i].FieldCount - 1 do
                begin
                  if fDataSet[i].Fields[j].Visible = true then
                  begin
                    sList.Add(#9#9 + '<td>');
                    sList.Add(#9#9 + trim(FDataSet[i].Fields[j].AsString));
                    sList.Add(#9#9 + '</td>');
                  end;
                end;
                sList.Add(#9 + '</tr>');
                FDataSet[i].next;
              end;
            end
            else
            begin
              sList.Add(#9 + '<tr>');
              for j := 0 to FDataSet[i].FieldCount - 1 do
              begin
                if fDataSet[i].Fields[j].Visible = true then
                begin
                  sList.Add(#9#9 + '<td>');
                  sList.Add(#9#9 + '');
                  sList.Add(#9#9 + '</td>');
                end;
              end;
              sList.Add(#9 + '</tr>');
            end;
          end;
        end;
        sList.Add('</table>');
        sList.Add('</body>');
        sList.SaveToFile(FileName+'.xls');
      end;
      sList.Free;
    end;
      

  2.   

    好,我叫他们试下,你们是哪里的,很强吧,加我QQ吧,我在这里不知道要怎么送分,要不我送你们QQ秀吧~!16842357是我的QQ号码~!
      

  3.   

    這裡也有一個函數:function dbgridtoexcelsheet1(v_gld: TDBGrid; sheet: olevariant): boolean;
    var
      row, col, fieldindex: integer;
      bk: tbook;
      f: string;
    begin
      result := false;
      if (not v_gld.DataSource.DataSet.active)or(V_gld.DataSource.DataSet.FieldCount=0)  then
        exit;  bk := v_gld.DataSource.DataSet.getbook;
      v_gld.DataSource.DataSet.disablecontrols;
      sheet.activate;
      try
        // 列標題
        row := 1;
        col := 1;
        for fieldindex := 0 to v_gld.fieldCount - 1 do
        begin
          sheet.cells(row, col) := v_gld.Columns[fieldindex].FieldName;
          inc(col);
        end;
        // 表內容
        v_gld.DataSource.DataSet.first;
        while not v_gld.DataSource.DataSet.eof do
        begin
          row := row + 1;
          col := 1;
          for fieldindex := 0 to v_gld.FieldCount - 1 do
          begin
            f := v_gld.Columns[fieldindex].FieldName;
            if (f = '提單編碼')or(f='操作網點')or(f='寄件網點')or(f='發生網點')or(f='加盟商')or(f='提單號') then          sheet.cells(row, col) := '''' + v_gld.DataSource.DataSet.FieldByName(f).asstring
            else
              sheet.cells(row, col) := v_gld.DataSource.DataSet.FieldByName(f).asstring;
            inc(col);
          end;
          v_gld.DataSource.DataSet.next;
        end;    result := true;
      finally
        v_gld.DataSource.DataSet.gotobook(bk);
        v_gld.DataSource.DataSet.enablecontrols;
      end;
    end;
    //將數據導出excel中去
    function GridToExcel1(v_gld: TDBGrid; visible: boolean; excelfilename:
      string;strname:string): boolean;
    var
      excelobj, excel, workbook, sheet: olevariant;
      // oldcursor: tcursor;
      savedialog: tsavedialog;
    begin
      result := false;
      //oldcursor := screen.cursor;
      screen.cursor := crhourglass;
      try
        excelobj := createoleobject('excel.sheet');
        excel := excelobj.application;
        excel.visible := visible;
        workbook := excel.workbooks.add;
        workbook.sheets[1].name:=strname;
        sheet := workbook.sheets[1];
      except
        messagebox(getactivewindow, '無法調用mircorsoft excel! ' + chr(13) + chr(10)
          +
          '請檢查是否安裝了mircorsoft excel。', '提示', mb_ok + mb_iconinformation);
        screen.cursor := crdefault;
        exit;
      end;
      if(v_gld.DataSource.DataSet.Active=true) then
     // if (getselft.DataSource1.DataSet.Active=true) then
      begin
        v_gld.DataSource.DataSet.First;
        //getselft.DataSource1.DataSet.First;
        result := dbgridtoexcelsheet1(v_gld, sheet);
      if result then
        if not visible then
        begin
          if excelfilename <> '' then
            workbook.saveas(filename := excelfilename)
          else
          begin
            savedialog := tsavedialog.create(nil);
            savedialog.filter := 'microsoft excel 文件|*.xls';
            result := savedialog.execute;
            updatewindow(getactivewindow);
            if result then
              workbook.saveas(filename := savedialog.filename);
            savedialog.free;
          end;
          excel.quit;
        end;
      screen.cursor :=crdefault;
    end;
    end;