Esto funciona en Delphi 6.
Ten en cuenta que donde se ejecute debe estar el Excel instalado.
También puede haber diferencias con las diferentes versiones de Excel.
Además de hacerlo así, quizás puedas acceder a estos datos generando un ODBC y leyendo como si fuera una tabla de una base de datos.
Código Delphi
[-]
procedure TFMRegInventario.TBDescargaExcelClick(Sender: TObject);
var
Excel : variant;
i, id_a : integer;
Articulo, UnidadesExcel, Archivo : string;
Existe, Cancelar : boolean;
Unidades : double;
begin
inherited;
Cancelar := False;
OpenDialog1.FilterIndex := 1;
OpenDialog1.InitialDir := REntorno.DirectorioLector;
if OpenDialog1.Execute then
begin
Archivo := OpenDialog1.FileName;
DMRegInventario.NotasLote := False;
try
try
Excel := CreateOleObject('Excel.Application');
Excel.WorkBooks.Open(Archivo);
for i := 1 to Excel.Rows.CurrentRegion.Rows.Count do
begin
if (not Cancelar) then
begin
try
Articulo := Excel.Range['a' + IntToStr(i)].Value;
except
Articulo := '';
end;
try
UnidadesExcel := Excel.Range['b' + IntToStr(i)].Value;
except
UnidadesExcel := '0';
end;
if (Articulo > '') then
begin
id_a := DameIDArticulo(Articulo);
Unidades := StrToFloatDef(UnidadesExcel, 0);
Precio := StrToFloatDef(PrecioExcel, 0);
if (id_a > 0) then
begin
end
else
begin
if (MessageDlg(Format(_('No existe Ariculo' + #13 + 'Código: %s - Stock: %s' + #13 + '¿Desea continuar?'), [Articulo, UnidadesExcel]), mtWarning, [mbYes, mbNo], 0) = mrNo) then
Cancelar := True;
end;
end;
end;
end;
except
on e: Exception do
MessageDlg(_('ERROR Imposible conectar con Excel') + #13 + e.Message, mtError, [mbOK], 0);
end;
finally
Excel.Quit;
end;
end;
end;