J'essaie d'utiliser TOpenDialog afin de passer le chemin du fichier sélectionné à AdoConection et de charger le contenu du fichier Excel dans la table. Je tente actuellement le code ci-dessous, mais la dernière partie du code ne se connecte pas à l'Excel renvoyant une erreur: [Erreur dcc32] sample_map.pas (80): E2010 Types incompatibles: 'string' et 'TOpenDialog'Passer le chemin du fichier dans Delphi à partir de TOpenDialog en tant que chaîne
procedure TForm1.Button1Click(Sender: TObject);
var
openDialog : TOpenDialog; // Open dialog variable
strConn : WideString; // Declare wide string for the connection
begin
// Create the open dialog object - assign to our open dialog variable
openDialog := TOpenDialog.Create(self);
// Set up the starting directory to be the current one
openDialog.InitialDir := GetCurrentDir;
// Only allow existing files to be selected
openDialog.Options := [ofFileMustExist];
// Allow only .dpr and .pas files to be selected
openDialog.Filter :=
'Excel 2003 and older|*.xls|Excel 2007 and older|*.xlsx';
// Select pascal files as the starting filter type
openDialog.FilterIndex := 2;
// Display the open file dialog
if openDialog.Execute
then ShowMessage('File : '+openDialog.FileName)
else ShowMessage('Open file was cancelled');
// Free up the dialog
openDialog.Free;
// Connect the Excel file
strConn:='Provider=Microsoft.Jet.OLEDB.4.0;' +
'Data Source=' + openDialog + ';' +
'Extended Properties=Excel 8.0;';
AdoConnection1.Connected:=False;
AdoConnection1.ConnectionString:=strConn;
end;
Oui, la clé ici est que vous avez manqué la propriété Filename lors de l'ajout du nom de fichier sélectionné dans la chaîne de connexion. –