Fórum Pesquisas #6470
04/06/2009
0
Diogenes Dourado
Curtir tópico
+ 0Posts
04/06/2009
Wesley Yamazack
Segue abaixo rotina para poder ler uma planilha do Excel.
function TForm1.Xls_To_StringGrid(AGrid: TStringGrid; AXLSFile: string): Boolean;
const
xlCellTypeLastCell = $0000000B;
var
XLApp, Sheet: OLEVariant;
RangeMatrix: Variant;
x, y, k, r: Integer;
begin
Result := False;
// Create Excel-OLE Object
XLApp := CreateOleObject('Excel.Application');
try
// Hide Excel
XLApp.Visible := False;
// Open the Workbook
XLApp.Workbooks.Open(AXLSFile);
// Sheet := XLApp.Workbooks[1].WorkSheets[1];
Sheet := XLApp.Workbooks[ExtractFileName(AXLSFile)].WorkSheets[1];
// In order to know the dimension of the WorkSheet, i.e the number of rows
// and the number of columns, we activate the last non-empty cell of it
Sheet.Cells.SpecialCells(xlCellTypeLastCell, EmptyParam).Activate;
// Get the value of the last row
x := XLApp.ActiveCell.Row;
// Get the value of the last column
y := XLApp.ActiveCell.Column;
// Set Stringgrid's row &col dimensions.
AGrid.RowCount := x;
AGrid.ColCount := y;
// Assign the Variant associated with the WorkSheet to the Delphi Variant
RangeMatrix := XLApp.Range['A1', XLApp.Cells.Item[X, Y]].Value;
// Define the loop for filling in the TStringGrid
k := 1;
repeat
for r := 1 to y do
AGrid.Cells[(r - 1), (k - 1)] := RangeMatrix[K, R];
Inc(k, 1);
AGrid.RowCount := k + 1;
until k > x;
// Unassign the Delphi Variant Matrix
RangeMatrix := Unassigned;
finally
// Quit Excel
if not VarIsEmpty(XLApp) then
begin
// XLApp.DisplayAlerts := False;
XLApp.Quit;
XLAPP := Unassigned;
Sheet := Unassigned;
Result := True;
end;
end;
end;
Para usar a mesma faça o seguinte
Xls_to_StringGrid(StringGrid, CaminhoDoarquivo);
=============================================
procedure TForm1.Button1Click(Sender: TObject);
var
I : Integer;
begin
StringGrid1.Cells[0,0] := 'ID';
StringGrid1.Cells[1,0] := 'Descricao';
For I := 1 to StringGrid1.RowCount -2 do
begin
StringGrid1.Cells[1,I] := GetID(UpperCase(StringGrid1.Cells[0,I])); //Valor que você quer localizar no teu BD
end;
end;
No Clique do Botão Faça isso
=============================================
function TForm1.GetID(ID: String): String;
Var
xQryWork : TSqlQuery;
begin
xQryWork := TSqlQuery.Create(Self);
xQryWork.SQLConnection := Conexao;
begin
with xQryWork do
begin
SQL.Add(' SELECT ID,DESCRICAO, CPF FROM DBA_EGK.CLIENTE C ');
SQL.Add(' WHERE C.ID = :ID ');
Params.ParamByName('ID').AsString := ID;
Open;
Result := FieldByName('Descricao').AsString; // Valor que você quer retornar e exibir na tua StringGrid
end;
FreeAndNil(xQryWork);
end;
Espero que ajude, se adapte e entenda o código. Qualquer coisa estamos a disposição.
Att,
Wesley Y
Gostei + 0
05/06/2009
Diogenes Dourado
Grato Diogenes
Gostei + 0
05/06/2009
Wesley Yamazack
Você tem que declarar no private, ou no public do teu formulário. Ex. :
unit Unit1;
interface
Type
TForm1 = class(TForm)
private
function Xls_To_StringGrid(AGrid: TStringGrid; AXLSFile: string): Boolean;
protected
public
published
end;
implementation
end.
Att,
Wesley Y
Gostei + 0
05/06/2009
Diogenes Dourado
Gostei + 0
08/06/2009
Wesley Yamazack
Consegui fazer o exemplo? Sua dúvida foi esclarecida ? Podemos fechar o chamado ?
Att,
Wesley Y
Gostei + 0
08/06/2009
Diogenes Dourado
Gostei + 0
Clique aqui para fazer login e interagir na Comunidade :)