Componente para Importar dados do Excel
Olá Pessoal!!
Alguem sabe de algum componente para importar dados do excel?
Orbigado
Gilberto.
Alguem sabe de algum componente para importar dados do excel?
Orbigado
Gilberto.
Gmgj2000
Curtidas 0
Respostas
Ricardo.vano
27/10/2004
Conheço duas formas de importar dados do excel:
1-) Através do ADO, porém ele importa todas as informações e a primeira linha do excel se tranforma em nome de campo.
2-) Através dos componentes da paleta Server. Com estes componentes é possível selecionar quais células deverão ser importadas. Tem bastante conteúdo sobre o assunto no fórum. Pesquise com estas palavras-chave que fica fácil:
- ExcelWorksheet
- ExcelApplication
- Workbook
Abaixo está um exemplo de código utilizando os componetes da paleta server:
procedure TFormImportar.Button2Click(Sender: TObject);
var
Workbook : _Workbook;
lcid : Integer;
I, J : Integer;
Segunda_Subida: Integer;
TURNO1, TURNO2, TURNO3 : array[1..8] of integer;
begin
lcid := GetUserDefaultLCID;
ExcelApplication1.Visible[lcid] := true;
ExcelApplication1.DisplayAlerts[lcid] := false;
Workbook := ExcelApplication1.Workbooks.Open(EditNomeArquivo.Text, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, lcid);
ExcelWorksheet1.ConnectTo(WorkBook.Worksheets[4] as _Worksheet);
ExcelWorksheet1.Activate(lcid);
ExcelApplication1.ScreenUpdating[lcid] := true;
// TURNO 3 - 22:00 ÀS 06:00
TURNO3[6] := ExcelWorksheet1.Range[´B31´, ´B31´].Value2;
TURNO3[7] := ExcelWorksheet1.Range[´B32´, ´B32´].Value2;
TURNO3[8] := ExcelWorksheet1.Range[´B33´, ´B33´].Value2;
TURNO3[1] := ExcelWorksheet1.Range[´B10´, ´B10´].Value2;
TURNO3[2] := ExcelWorksheet1.Range[´B11´, ´B11´].Value2;
TURNO3[3] := ExcelWorksheet1.Range[´B12´, ´B12´].Value2;
TURNO3[4] := ExcelWorksheet1.Range[´B13´, ´B13´].Value2;
TURNO3[5] := ExcelWorksheet1.Range[´B14´, ´B14´].Value2;
// TURNO 1 - 06:00 ÀS 14:00
TURNO1[1] := ExcelWorksheet1.Range[´B15´, ´B15´].Value2;
TURNO1[2] := ExcelWorksheet1.Range[´B16´, ´B16´].Value2;
TURNO1[3] := ExcelWorksheet1.Range[´B17´, ´B17´].Value2;
TURNO1[4] := ExcelWorksheet1.Range[´B18´, ´B18´].Value2;
TURNO1[5] := ExcelWorksheet1.Range[´B19´, ´B19´].Value2;
TURNO1[6] := ExcelWorksheet1.Range[´B20´, ´B20´].Value2;
TURNO1[7] := ExcelWorksheet1.Range[´B21´, ´B21´].Value2;
TURNO1[8] := ExcelWorksheet1.Range[´B22´, ´B22´].Value2;
// TURNO 2 - 14:00 ÀS 22:00
TURNO2[1] := ExcelWorksheet1.Range[´B23´, ´B23´].Value2;
TURNO2[2] := ExcelWorksheet1.Range[´B24´, ´B24´].Value2;
TURNO2[3] := ExcelWorksheet1.Range[´B25´, ´B25´].Value2;
TURNO2[4] := ExcelWorksheet1.Range[´B26´, ´B26´].Value2;
TURNO2[5] := ExcelWorksheet1.Range[´B27´, ´B27´].Value2;
TURNO2[6] := ExcelWorksheet1.Range[´B28´, ´B28´].Value2;
TURNO2[7] := ExcelWorksheet1.Range[´B29´, ´B29´].Value2;
TURNO2[8] := ExcelWorksheet1.Range[´B30´, ´B30´].Value2;
end;
1-) Através do ADO, porém ele importa todas as informações e a primeira linha do excel se tranforma em nome de campo.
2-) Através dos componentes da paleta Server. Com estes componentes é possível selecionar quais células deverão ser importadas. Tem bastante conteúdo sobre o assunto no fórum. Pesquise com estas palavras-chave que fica fácil:
- ExcelWorksheet
- ExcelApplication
- Workbook
Abaixo está um exemplo de código utilizando os componetes da paleta server:
procedure TFormImportar.Button2Click(Sender: TObject);
var
Workbook : _Workbook;
lcid : Integer;
I, J : Integer;
Segunda_Subida: Integer;
TURNO1, TURNO2, TURNO3 : array[1..8] of integer;
begin
lcid := GetUserDefaultLCID;
ExcelApplication1.Visible[lcid] := true;
ExcelApplication1.DisplayAlerts[lcid] := false;
Workbook := ExcelApplication1.Workbooks.Open(EditNomeArquivo.Text, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, lcid);
ExcelWorksheet1.ConnectTo(WorkBook.Worksheets[4] as _Worksheet);
ExcelWorksheet1.Activate(lcid);
ExcelApplication1.ScreenUpdating[lcid] := true;
// TURNO 3 - 22:00 ÀS 06:00
TURNO3[6] := ExcelWorksheet1.Range[´B31´, ´B31´].Value2;
TURNO3[7] := ExcelWorksheet1.Range[´B32´, ´B32´].Value2;
TURNO3[8] := ExcelWorksheet1.Range[´B33´, ´B33´].Value2;
TURNO3[1] := ExcelWorksheet1.Range[´B10´, ´B10´].Value2;
TURNO3[2] := ExcelWorksheet1.Range[´B11´, ´B11´].Value2;
TURNO3[3] := ExcelWorksheet1.Range[´B12´, ´B12´].Value2;
TURNO3[4] := ExcelWorksheet1.Range[´B13´, ´B13´].Value2;
TURNO3[5] := ExcelWorksheet1.Range[´B14´, ´B14´].Value2;
// TURNO 1 - 06:00 ÀS 14:00
TURNO1[1] := ExcelWorksheet1.Range[´B15´, ´B15´].Value2;
TURNO1[2] := ExcelWorksheet1.Range[´B16´, ´B16´].Value2;
TURNO1[3] := ExcelWorksheet1.Range[´B17´, ´B17´].Value2;
TURNO1[4] := ExcelWorksheet1.Range[´B18´, ´B18´].Value2;
TURNO1[5] := ExcelWorksheet1.Range[´B19´, ´B19´].Value2;
TURNO1[6] := ExcelWorksheet1.Range[´B20´, ´B20´].Value2;
TURNO1[7] := ExcelWorksheet1.Range[´B21´, ´B21´].Value2;
TURNO1[8] := ExcelWorksheet1.Range[´B22´, ´B22´].Value2;
// TURNO 2 - 14:00 ÀS 22:00
TURNO2[1] := ExcelWorksheet1.Range[´B23´, ´B23´].Value2;
TURNO2[2] := ExcelWorksheet1.Range[´B24´, ´B24´].Value2;
TURNO2[3] := ExcelWorksheet1.Range[´B25´, ´B25´].Value2;
TURNO2[4] := ExcelWorksheet1.Range[´B26´, ´B26´].Value2;
TURNO2[5] := ExcelWorksheet1.Range[´B27´, ´B27´].Value2;
TURNO2[6] := ExcelWorksheet1.Range[´B28´, ´B28´].Value2;
TURNO2[7] := ExcelWorksheet1.Range[´B29´, ´B29´].Value2;
TURNO2[8] := ExcelWorksheet1.Range[´B30´, ´B30´].Value2;
end;
GOSTEI 0
Wgm8
27/10/2004
Estou usando o seguinte comando:
for I:= 0 to xf.Workbook.Sheets.Count - 1 do
begin
CellsCount:= CellsCount + xf.Workbook.Sheets[I].Cells.Count;
Colunas := Colunas + xf.Workbook.Sheets[I].Columns.Count;
Linhas := Linhas + xf.Workbook.Sheets[I].Rows.Count;
end;
Ele me retorna o numero de celulas preenchidas.
Tb me retorna o numero de colunas correto.
So o numero de linhas que não dá certo.
Alguem pode me ajudar?
Linhas := Linhas + xf.Workbook.Sheets[I].Rows.Count;
Qual o erro na linha acima?
By
for I:= 0 to xf.Workbook.Sheets.Count - 1 do
begin
CellsCount:= CellsCount + xf.Workbook.Sheets[I].Cells.Count;
Colunas := Colunas + xf.Workbook.Sheets[I].Columns.Count;
Linhas := Linhas + xf.Workbook.Sheets[I].Rows.Count;
end;
Ele me retorna o numero de celulas preenchidas.
Tb me retorna o numero de colunas correto.
So o numero de linhas que não dá certo.
Alguem pode me ajudar?
Linhas := Linhas + xf.Workbook.Sheets[I].Rows.Count;
Qual o erro na linha acima?
By
GOSTEI 0
Martins
27/10/2004
Bem interessante as tecnicas de vcs, eu utilizo os componentes da Paleta Server tb, mas já fiz com ADO e colocando um ClientDatSet para armazenar os dados a serem transferidos.
GOSTEI 0