Fórum exportar dados ado #424540

24/09/2012

0

Eu gostaria de exportar os dados cadastrados no meu sistema(delphi) para o excel.

FOI USADO:

ADOCONNECTION
ADOQUERY
DATASOURCE

BD: ACCESS.

lembrando que os codigos abaixo não deram certo
var linha, coluna : integer;
var planilha : variant;
var valorcampo : string;
begin
Query1.CachedUpdates := true;
planilha:= CreateoleObject('Excel.Application');
planilha.WorkBooks.add(1);
planilha.caption := Exportando dados do dbGrid para o Excel';
planilha.visible := true;

Query1.ApplyUpdates;
Query1.First;
for linha := 0 to Query1.RecordCount - 1 do
begin
for coluna := 1 to Query1.FieldCount do
begin
valorcampo := Query1.Fields[coluna - 1].AsString;
planilha.cells[linha + 2,coluna] := valorCampo;
end;
Query1.Next;
end;
for coluna := 1 to Query1.FieldCount do
begin
valorcampo := Query1.Fields[coluna - 1].DisplayLabel;
planilha.cells[1,coluna] := valorcampo;
end;
planilha.columns.Autofit;
Paulo

Paulo

Responder

Posts

24/09/2012

Claudia Nogueira

Veja se assim funciona:

Var
  planilha : variant;
  i, linha : Integer;
  valorcampo : String;
begin
  planilha := CreateoleObject('Excel.Application');
  planilha.WorkBooks.Add(1);
  planilha.caption := 'Exportando dados do dbGrid para o Excel';
  planilha.visible := True;
  linha := -1;
  if not Query1.IsEmpty then
    for i := 0 to Query1.FieldCount - 1 do
    begin
      planilha.cells[1,i+1] := Query1.Fields[i].FieldName;
    end;
  Query1.First;
  while not Query1.Eof do
  begin
    Inc(linha);
    for i := 0 to Query1.FieldCount -1 do
    begin
      planilha.cells[linha + 2,i+1] := Query1.Fields[i].AsString;
    end;
    Query1.Next;
  end;
  planilha.columns.Autofit;
Responder

Gostei + 0

25/09/2012

Alisson Santos

Gostaria de saber se conseguiu com o exemplo da nossa colega?
Responder

Gostei + 0

25/09/2012

Paulo

Gostaria de saber se conseguiu com o exemplo da nossa colega?



começa exportar, mais dá um erro( OLE error800AC472). Talvez seja a foto?

por favor me ajude.
Responder

Gostei + 0

25/09/2012

Paulo

Gostaria de saber se conseguiu com o exemplo da nossa colega?



começa exportar, mais dá um erro( OLE error800AC472). Talvez seja a foto?

por favor me ajude.


Tem como eu escolher os campos a serem exportados?
Responder

Gostei + 0

25/09/2012

Paulo

Veja se assim funciona:

Var
  planilha : variant;
  i, linha : Integer;
  valorcampo : String;
begin
  planilha := CreateoleObject('Excel.Application');
  planilha.WorkBooks.Add(1);
  planilha.caption := 'Exportando dados do dbGrid para o Excel';
  planilha.visible := True;
  linha := -1;
  if not Query1.IsEmpty then
    for i := 0 to Query1.FieldCount - 1 do
    begin
      planilha.cells[1,i+1] := Query1.Fields[i].FieldName;
    end;
  Query1.First;
  while not Query1.Eof do
  begin
    Inc(linha);
    for i := 0 to Query1.FieldCount -1 do
    begin
      planilha.cells[linha + 2,i+1] := Query1.Fields[i].AsString;
    end;
    Query1.Next;
  end;
  planilha.columns.Autofit;




começa exportar, mais dá um erro( OLE error800AC472). Talvez seja a foto?

por favor me ajude.


Tem como eu escolher os campos a serem exportados?
Responder

Gostei + 0

25/09/2012

Claudia Nogueira

Veja se assim funciona:

Var
  planilha : variant;
  i, linha : Integer;
  valorcampo : String;
begin
  planilha := CreateoleObject('Excel.Application');
  planilha.WorkBooks.Add(1);
  planilha.caption := 'Exportando dados do dbGrid para o Excel';
  planilha.visible := True;
  linha := -1;
  if not Query1.IsEmpty then
    for i := 0 to Query1.FieldCount - 1 do
    begin
      planilha.cells[1,i+1] := Query1.Fields[i].FieldName;
    end;
  Query1.First;
  while not Query1.Eof do
  begin
    Inc(linha);
    for i := 0 to Query1.FieldCount -1 do
    begin
      planilha.cells[linha + 2,i+1] := Query1.Fields[i].AsString;
    end;
    Query1.Next;
  end;
  planilha.columns.Autofit;




começa exportar, mais dá um erro( OLE error800AC472). Talvez seja a foto?

por favor me ajude.


Tem como eu escolher os campos a serem exportados?


Sim pode ser a foto. Você pode fazer um if pra verificar os campos.

Var
  planilha : variant;
  i, linha : Integer;
  valorcampo : String;
begin
  planilha := CreateoleObject('Excel.Application');
  planilha.WorkBooks.Add(1);
  planilha.caption := 'Exportando dados do dbGrid para o Excel';
  planilha.visible := True;
  linha := -1;
  if not Query1.IsEmpty then
    for i := 0 to Query1.FieldCount - 1 do
    begin
      if Query1.Fields[i].FieldName <> 'nome_do_campo' then
        planilha.cells[1,i+1] := Query1.Fields[i].FieldName;
    end;
  Query1.First;
  while not Query1.Eof do
  begin
    Inc(linha);
    for i := 0 to Query1.FieldCount -1 do
    begin
      if Query1.Fields[i].FieldName <> 'nome_do_campo' then
        planilha.cells[linha + 2,i+1] := Query1.Fields[i].AsString;
    end;
    Query1.Next;
  end;
  planilha.columns.Autofit;
Responder

Gostei + 0

25/09/2012

Paulo

Veja se assim funciona:

Var
  planilha : variant;
  i, linha : Integer;
  valorcampo : String;
begin
  planilha := CreateoleObject('Excel.Application');
  planilha.WorkBooks.Add(1);
  planilha.caption := 'Exportando dados do dbGrid para o Excel';
  planilha.visible := True;
  linha := -1;
  if not Query1.IsEmpty then
    for i := 0 to Query1.FieldCount - 1 do
    begin
      planilha.cells[1,i+1] := Query1.Fields[i].FieldName;
    end;
  Query1.First;
  while not Query1.Eof do
  begin
    Inc(linha);
    for i := 0 to Query1.FieldCount -1 do
    begin
      planilha.cells[linha + 2,i+1] := Query1.Fields[i].AsString;
    end;
    Query1.Next;
  end;
  planilha.columns.Autofit;




começa exportar, mais dá um erro( OLE error800AC472). Talvez seja a foto?

por favor me ajude.


Tem como eu escolher os campos a serem exportados?


Sim pode ser a foto. Você pode fazer um if pra verificar os campos.

Var
  planilha : variant;
  i, linha : Integer;
  valorcampo : String;
begin
  planilha := CreateoleObject('Excel.Application');
  planilha.WorkBooks.Add(1);
  planilha.caption := 'Exportando dados do dbGrid para o Excel';
  planilha.visible := True;
  linha := -1;
  if not Query1.IsEmpty then
    for i := 0 to Query1.FieldCount - 1 do
    begin
      if Query1.Fields[i].FieldName <> 'nome_do_campo' then
        planilha.cells[1,i+1] := Query1.Fields[i].FieldName;
    end;
  Query1.First;
  while not Query1.Eof do
  begin
    Inc(linha);
    for i := 0 to Query1.FieldCount -1 do
    begin
      if Query1.Fields[i].FieldName <> 'nome_do_campo' then
        planilha.cells[linha + 2,i+1] := Query1.Fields[i].AsString;
    end;
    Query1.Next;
  end;
  planilha.columns.Autofit;



Deu certo ok! obrigado.

eu gostaria tambem de colocar um progressbar.visible, em enquanto tiver exportando os dados para o excel. Como eu faço?
Responder

Gostei + 0

25/09/2012

Alisson Santos

no post https://www.devmedia.com.br/forum/viewtopic.asp?id=424577, já foi respondido para você a questão.
Verifique por favor
Responder

Gostei + 0

25/09/2012

Paulo

no post https://www.devmedia.com.br/forum/viewtopic.asp?id=424577, já foi respondido para você a questão.
Verifique por favor



foi respondido mais não deus certo. não visualizo o progress acompanhando a exportação.
Responder

Gostei + 0

Utilizamos cookies para fornecer uma melhor experiência para nossos usuários, consulte nossa política de privacidade.

Aceitar