求助:
非常非常急,各位大哥请给个把 DBGRID 里数据导入到 excel 的例子,好吗?
谢谢!

解决方案 »

  1.   

    把DGBridEh改成DBGrid就行啦。procedure ExportDBGridToExcel(Grid: TDBGridEh; DisableScreenUpdating: Boolean;
      ReportCaption, ReportMemo, ReportTtl: string; AddChart: Boolean);
    const
      CLASS_ExcelApplication: TGUID = '{00024500-0000-0000-C000-000000000046}';
    var
      ExcelApp, Sheet, Chart: OleVariant;
      Unknown: IUnknown;
      BM: TBookStr;
      Col, Row: Integer;
      I: Integer;
    begin
      if (Grid.DataSource <> nil) and (Grid.DataSource.DataSet <> nil) then
        with Grid.DataSource.DataSet do
        begin
          try
            if not Succeeded(GetActiveObject(CLASS_ExcelApplication, nil, Unknown)) then
              Unknown := CreateComObject(CLASS_ExcelApplication);
          except
            raise Exception.Create('不能启动 Microsoft Excel,请确认 Microsoft Excel 已正确安装在本机上');
          end;
          ExcelApp := Unknown as IDispatch;
          ExcelApp.Visible := True;
          ExcelApp.Workbooks.Add;
          if DisableScreenUpdating then
            ExcelApp.ScreenUpdating := False;
          DisableControls;
          try
            BM := Book;
            First;
            Row := 4;
            Col := 1;
            for I := 0 to Grid.Columns.Count - 1 do
            begin
              if Grid.Columns[I].Visible then
                ExcelApp.Cells[Row, Col] := Grid.Columns[I].Title.Caption;
              Inc(Col);
            end;
            Inc(Row);
            while not Eof do
            begin
              Col := 1;
              for I := 0 to Grid.Columns.Count - 1 do
              begin
                if Grid.Columns[I].Visible then
                  if Grid.Columns[I].Field <> nil then
                    ExcelApp.Cells[Row, Col] := Grid.Columns[I].Field.DisplayText;
                Inc(Col);
              end;
              Inc(Row);
              Next;
            end;
            Col := 1;
            for I := 0 to Grid.Columns.Count - 1 do
            begin
              if Grid.Columns[I].Visible then
                ExcelApp.Columns[Col].AutoFit; ;
              Inc(Col);
            end;
            ExcelApp.Cells[1, 1] := ReportCaption;
            ExcelApp.Cells[2, 1] := ReportMemo;
            ExcelApp.Cells[3, 1] := ReportTtl;
            Book := BM;
          finally
            EnableControls;
            if not ExcelApp.ScreenUpdating then
              ExcelApp.ScreenUpdating := True;
          end;
        end;
      if AddChart then
      begin
        Sheet := ExcelApp.ActiveSheet;
        Sheet.Range[Sheet.Cells[4, 1], Sheet.Cells[Row - 1, Col - 1]].Select;
        Chart := ExcelApp.ActiveWorkbook.Charts.Add;
        Chart.SetSourceData(
          Sheet.Range[Sheet.Cells[4, 1], Sheet.Cells[Row - 1, Col - 1]], EmptyParam);
        Chart.Location(1, EmptyParam);
      end;
      ExcelApp.ActiveWorkbook.PrintPreview;
    end;
      

  2.   

    DGBridEh组件的Eh控件包本身就有提供一个导出的组件,直接拖出来放到DGBridEh上面就可以在代码中加上一句话就好了哦……
      

  3.   

    xjjrocker(了无痕)  哪個控件﹖﹖我剛裝了DBGRIDEH﹐但不知道哪個控件可以提供導出﹐只知道打印的printDbgrideh
      

  4.   

    楼主有DGBridEh组件的安装文件吗?发给我好吗?我也要用到将DBGrid里的数据导出到excel里,谢谢啊,
      

  5.   

    看错了,不是楼主,是yoyoxu(),呵呵,不管是谁,只要有的都可以发给我,感谢……