Bueno en realidad no es el algoritmo, pero ayuda el código; y si, usa funciones OLE. Pero en realidad sería estupendo alguna guía sobre el ahorro de recursos, cuándo aplicarlas y todo eso. El documento en excel posee más de 10000 filas (y en aumento) y 16 campos cada una.
Este es el código, disculpen el erróneo concepto de "algoritmo" para este ejemplo
.
Código Delphi
[-]begin
Button1.Enabled := False;
xls := CreateOleObject('Excel.Application');
xls.WorkBooks.Open( OpenDialog1.FileName );
xls.Visible := false;
fLoop := true;
iFila := 2;
while fLoop do
begin
if VarToStr( xls.WorkSheets[1].Cells[iFila,1].Value ) = '' then
fLoop := false
else
begin
inc(iFila);
Label1.Caption := 'Procesando ...';
Label1.Update;
end;
end;
Total:=iFila;
ProgressBar1.Max:=iFila;
try fLoop := true;
iFila := 2; while fLoop do
begin
if VarToStr( xls.WorkSheets[1].Cells[iFila,1].Value ) = '' then
fLoop := false
else
begin
Dia:=StrtoDate(VarToStr( xls.WorkSheets[1].Cells[iFila,1].Value));
Loteria:=VarToStr( xls.WorkSheets[1].Cells[iFila,2].Value);
Query1.Close;
Query1.SQL.Clear;
Query1.SQL.Text:='SELECT * FROM Tabla WHERE (Campo1 = :C1) AND (Campo2 = :C2)';
Query1.SQL.Text:=Format(Consulta,[QuotedSTr(Loteria)]);
Query1.ParamByName('C1').asString := Campo1;
Query1.ParamByName('C2').asDate := Campo2;
Query1.RequestLive := true;
Query1.Open;
if Query1.IsEmpty then
Begin
Query2.Close;
Query2.SQL.Clear;
Query2.SQL.Text:='SELECT Campo3 FROM Tabla WHERE (Campo3 = :C3)';
Query2.ParamByName('Lot').asString := Campo1;
Query2.RequestLive := true;
Query2.Open;
if Query2.IsEmpty then
Begin
Query2.Insert;
Query2.FieldByName('Campo2').AsString:=C2;
Query2.Post;
end;
Query1.Insert;
Query1.FieldByName('Campo0').AsInteger := Table1.RecordCount+1;
Query1.FieldByName('Campo1').AsDateTime := StrtoDate(VarToStr( xls.WorkSheets[1].Cells[iFila,1].Value));
Query1.FieldByName('Campo3').AsString := VarToStr( xls.WorkSheets[1].Cells[iFila,2].Value);
triple:=VarToStr( xls.WorkSheets[1].Cells[iFila,3].Value);
Query1.FieldByName('C4').AsString := completarceros(triple);
triple:=VarToStr( xls.WorkSheets[1].Cells[iFila,4].Value);
Query1.FieldByName('C5').AsString :=completarceros(triple);
triple:=VarToStr( xls.WorkSheets[1].Cells[iFila,5].Value);
Query1.FieldByName('C6').AsString :=completarceros(triple);
triple:=VarToStr( xls.WorkSheets[1].Cells[iFila,6].Value);
Query1.FieldByName('C7').AsString :=completarceros(triple);
triple:=VarToStr( xls.WorkSheets[1].Cells[iFila,7].Value);
Query1.FieldByName('C8').AsString :=completarceros(triple);
triple:=VarToStr( xls.WorkSheets[1].Cells[iFila,8].Value);
Query1.FieldByName('C9').AsString :=completarceros(triple);
triple:=VarToStr( xls.WorkSheets[1].Cells[iFila,9].Value);
Query1.FieldByName('C10').AsString :=completarceros(triple);
triple:=VarToStr( xls.WorkSheets[1].Cells[iFila,10].Value);
Query1.FieldByName('C11').AsString :=completarceros(triple);
triple:=VarToStr( xls.WorkSheets[1].Cells[iFila,11].Value);
Query1.FieldByName('C12').AsString :=completarceros(triple);
Query1.Post;
end;
end;
inc(iFila);
Porc:=IFila*100/Total;
Label1.Caption := FloattoStr(Trunc(Porc))+'%';
ProgressBar1.StepIt;
Label1.Update;
end
finally
xls.Quit
end;