Fórum Delphi - Excel com várias abas #599300
19/12/2018
0
No código abaixo
Excel: Variant;
Excel := CreateOleObject(''''Excel.Application'''');
Excel.Visible := True;
Excel.Workbooks.Add;
Excel.Workbooks[1].WorkSheets[1].Name := ''''Aluno'''';
Application.BringToFront;
Excel.WorkBooks[1].Sheets[1].Cells[ContaLinha, 1] := ''''IdAluno'''';
Excel.WorkBooks[1].Sheets[1].Cells[ContaLinha, 2] := ''''Matricula'''';
Excel.WorkBooks[1].Sheets[1].Cells[ContaLinha, 1] := ''''2514'''';
Excel.WorkBooks[1].Sheets[1].Cells[ContaLinha, 2] := ''''JOSE'''';
Agora como faço para logo abaixo desse código inserir outra aba(planilha) e continuar inserindo nela?
Leandro Carvalho
Curtir tópico
+ 1Post mais votado
22/12/2018
var
Excel: variant;
Plan: variant;
begin
Excel := CreateOleObject('Excel.Application');
Excel.Visible := True;
Excel.Workbooks.Add;
Application.BringToFront;
Plan := Excel.WorkBooks[1].Sheets[1];
Plan.Name := 'Aluno';
Plan.Cells[1, 1] := 'IdAluno';
Plan.Cells[1, 2] := 'Matricula';
Plan.Cells[2, 1] := '2514';
Plan.Cells[2, 2] := 'JOSE';
Plan := Excel.Workbooks[1].Sheets.Add(null, Plan); // adiciona planilha no fim (fica ativa)
Plan.Name := '2o. Aluno';
Plan.Cells[1, 1] := '2o. IdAluno';
Plan.Cells[1, 2] := '2o. Matricula';
Plan.Cells[2, 1] := '2o. 2515';
Plan.Cells[2, 2] := '2o. MARIA';
Plan := Excel.Workbooks[1].Sheets.Add(null, Plan); // adiciona planilha no fim (fica ativa)
Plan.Name := '3o. Aluno';
Plan.Cells[1, 1] := '3o. IdAluno';
Plan.Cells[1, 2] := '3o. Matricula';
Plan.Cells[2, 1] := '3o. 2516';
Plan.Cells[2, 2] := '3o. PEDRO';
Plan := Excel.WorkBooks[1].Sheets[1]; // volto para a planilha 1
Plan.Cells[3, 1] := '2517';
Plan.Cells[3, 2] := 'ANTONIO';
Plan.Cells[4, 1] := '2518';
Plan.Cells[4, 2] := 'JOANA';
end;Emerson Nascimento
Gostei + 2
Mais Posts
20/12/2018
Leandro Carvalho
Alguém pode me ajudar.
Por favor.
Gostei + 0
20/12/2018
Flavio Silva
Excel.Workbooks[2]
Gostei + 0
20/12/2018
Leandro Carvalho
<br />
Isso não funciona.<br />
Já tentei várias coisas e não dá certo.
Gostei + 0
20/12/2018
Emerson Nascimento
Excel := CreateOleObject('Excel.Application');
Excel.Visible := True;
Excel.Workbooks.Add;
Excel.Workbooks[1].WorkSheets[1].Name := 'Aluno';
Application.BringToFront;
Excel.WorkBooks[1].Sheets[1].Cells[ContaLinha, 1] := 'IdAluno';
Excel.WorkBooks[1].Sheets[1].Cells[ContaLinha, 2] := 'Matricula';
Excel.WorkBooks[1].Sheets[1].Cells[ContaLinha, 1] := '2514';
Excel.WorkBooks[1].Sheets[1].Cells[ContaLinha, 2] := 'JOSE';
Excel.Workbooks[1].Sheets.Add; // adiciona planilha
Excel.WorkBooks[1].Sheets[2].Cells[ContaLinha, 1] := 'IdAluno Plan2';
Excel.WorkBooks[1].Sheets[2].Cells[ContaLinha, 2] := 'Matricula Plan2';
Excel.WorkBooks[1].Sheets[2].Cells[ContaLinha, 1] := '2514 plan2';
Excel.WorkBooks[1].Sheets[2].Cells[ContaLinha, 2] := 'JOSE plan2';Gostei + 0
21/12/2018
Leandro Carvalho
Emerson....
Essa linha
Excel.Workbooks[1].Sheets.Add;
Funcionou, criou uma nova aba, mas as informações continuou incluindo a aba anterior.
Gostei + 0
21/12/2018
Leandro Carvalho
Gostei + 0
21/12/2018
Emerson Nascimento
Excel.WorkBooks[1].Sheets[ 2 ].Cells[1, 1] := 'IdAluno Plan2';
ou
Excel.WorkBooks[1].WorkSheets[ 2 ].Cells[1, 1] := 'IdAluno Plan2';
Note que agora estou apontando para a segunda planilha Sheets[2]
Gostei + 0
21/12/2018
Leandro Carvalho
Excel.WorkBooks[1].Sheets[ 2 ].Cells[1, 1] := 'IdAluno Plan2';
ou
Excel.WorkBooks[1].WorkSheets[ 2 ].Cells[1, 1] := 'IdAluno Plan2';
Note que agora estou apontando para a segunda planilha Sheets[2]
Gostei + 0
21/12/2018
Emerson Nascimento
var
Excel: variant;
Plan: variant;
begin
Excel := CreateOleObject('Excel.Application');
Excel.Visible := True;
Excel.Workbooks.Add;
Application.BringToFront;
Plan := Excel.WorkBooks[1].Sheets[1];
Plan.Name := 'Aluno';
Plan.Cells[1, 1] := 'IdAluno';
Plan.Cells[1, 2] := 'Matricula';
Plan.Cells[2, 1] := '2514';
Plan.Cells[2, 2] := 'JOSE';
Plan := Excel.Workbooks[1].Sheets.Add(null, Plan); // adiciona planilha no fim (fica ativa)
Plan.Name := '2o. Aluno';
Plan.Cells[1, 1] := '2o. IdAluno';
Plan.Cells[1, 2] := '2o. Matricula';
Plan.Cells[2, 1] := '2o. 2515';
Plan.Cells[2, 2] := '2o. MARIA';
Plan := Excel.Workbooks[1].Sheets.Add(null, Plan); // adiciona planilha no fim (fica ativa)
Plan.Name := '3o. Aluno';
Plan.Cells[1, 1] := '3o. IdAluno';
Plan.Cells[1, 2] := '3o. Matricula';
Plan.Cells[2, 1] := '3o. 2516';
Plan.Cells[2, 2] := '3o. PEDRO';
end;Gostei + 0
26/12/2018
Leandro Carvalho
var
Excel: variant;
Plan: variant;
begin
Excel := CreateOleObject('Excel.Application');
Excel.Visible := True;
Excel.Workbooks.Add;
Application.BringToFront;
Plan := Excel.WorkBooks[1].Sheets[1];
Plan.Name := 'Aluno';
Plan.Cells[1, 1] := 'IdAluno';
Plan.Cells[1, 2] := 'Matricula';
Plan.Cells[2, 1] := '2514';
Plan.Cells[2, 2] := 'JOSE';
Plan := Excel.Workbooks[1].Sheets.Add(null, Plan); // adiciona planilha no fim (fica ativa)
Plan.Name := '2o. Aluno';
Plan.Cells[1, 1] := '2o. IdAluno';
Plan.Cells[1, 2] := '2o. Matricula';
Plan.Cells[2, 1] := '2o. 2515';
Plan.Cells[2, 2] := '2o. MARIA';
Plan := Excel.Workbooks[1].Sheets.Add(null, Plan); // adiciona planilha no fim (fica ativa)
Plan.Name := '3o. Aluno';
Plan.Cells[1, 1] := '3o. IdAluno';
Plan.Cells[1, 2] := '3o. Matricula';
Plan.Cells[2, 1] := '3o. 2516';
Plan.Cells[2, 2] := '3o. PEDRO';
Plan := Excel.WorkBooks[1].Sheets[1]; // volto para a planilha 1
Plan.Cells[3, 1] := '2517';
Plan.Cells[3, 2] := 'ANTONIO';
Plan.Cells[4, 1] := '2518';
Plan.Cells[4, 2] := 'JOANA';
end;Deu certo.
Gostei + 0
Clique aqui para fazer login e interagir na Comunidade :)