delphi,导入Excel文件,未声明的标识符

我正在关注关于将Excel文件导入到Delphi的教程 。 当我尝试编译文件时,我一直在收到有关未声明标识符的错误消息。 据我所知,这个表格是名称,应该是在项目中可用的。 我将不胜感激有关修复此错误的任何build议。

unit sample_map; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ComCtrls, Data.DB, Data.Win.ADODB, Vcl.AppEvnts, Vcl.Grids, Vcl.DBGrids, Vcl.ExtCtrls, Vcl.DBCtrls, Vcl.StdCtrls, UWebGMapsCommon, System.Generics.Collections, UWebGMaps; type TForm1 = class(TForm) PageControl1: TPageControl; TabSheet1: TTabSheet; TabSheet2: TTabSheet; Button1: TButton; DataSource1: TDataSource; ADOConnection1: TADOConnection; ADOQuery1: TADOQuery; DBNavigator1: TDBNavigator; ApplicationEvents1: TApplicationEvents; StatusBar1: TStatusBar; DBGrid1: TDBGrid; Edit1: TEdit; Label1: TLabel; Label2: TLabel; Edit2: TEdit; Label3: TLabel; ComboBox1: TComboBox; procedure FormCreate(Sender: TObject); procedure Button1Click(Sender: TObject); procedure ApplicationEvents1Exception(Sender: TObject; E: Exception); private procedure TForm1.FetchData; begin StatusBar1.SimpleText:=''; ConnectToExcel; AdoQuery1.Close; AdoQuery1.SQL.Text:=Edit2.Text; try AdoQuery1.Open; except ShowMessage('Unable to read data from Excel, make sure the query ' + Edit1.Text + ' is meaningful!'); raise; end; end; public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.ApplicationEvents1Exception(Sender: TObject; E: Exception); begin StatusBar1.SimpleText := E.Message; end; procedure TForm1.Button1Click(Sender: TObject); var openDialog : TOpenDialog; // Open dialog variable strConn : WideString; // Declare wide string for the connection FileName: string; 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 .Excel and .pas files to be selected openDialog.Filter := 'Excel 2003|*.xls|Excel 2007 and older|*.xlsx'; // Select pascal files as the starting filter type openDialog.FilterIndex := 2; // Procedure to read the Excel file FileName := ''; if PromptForFileName(FileName, // Chosen filename holder 'Excel 2003 and older|*.xls|Excel 2007 and older|*.xlsx', // Filter(s) (optional) '.xlsx', // Default extension (opt) 'Choose file', // Dialog title (opt) GetCurrentDir, // Initial dir (opt) False) then // Is it a save dlg? (opt) begin strConn := 'Provider=Microsoft.Jet.OLEDB.4.0;' + 'Data Source=' + FileName + ';' + 'Extended Properties=Excel 8.0;'; // Connect the Excel file AdoConnection1.Connected:=False; AdoConnection1.ConnectionString:=strConn; // Insert the file name to the dialog box and so forth Edit1.Text := FileName; // Ad worksheets to the combo box end else ShowMessage('Dialog cancelled.'); end; procedure TForm1.FormCreate(Sender: TObject); begin AdoConnection1.LoginPrompt := False; AdoQuery1.Connection := AdoConnection1; DataSource1.DataSet := AdoQuery1; DBGrid1.DataSource := DataSource1; DBNavigator1.DataSource := DataSource1; end; end. 

你已经把TForm1.FetchData的实现放在错误的地方。 它不能放在类声明中。 它必须在单元的实现部分。 你的代码应该是这样的:

 interface .... type TForm1 = class(TForm) .... // IDE fields here private procedure FetchData; end; .... implementation .... procedure TForm1.FetchData; begin .... body of function here end; 

您链接到的教程包含一个完整的单元。 我build议你把那个完整的单元中的代码和你所生成的代码进行比较。

你已经在接口部分包含了一个方法的实现。 更正接口部分,使其如下所示:

 Private Procedure FetchData; Public End; 

然后将剩下的代码移到实现部分,如下所示:

  procedure TForm1.FetchData; begin StatusBar1.SimpleText:=''; ConnectToExcel; AdoQuery1.Close; AdoQuery1.SQL.Text:=Edit2.Text; try AdoQuery1.Open; except ShowMessage('Unable to read data from Excel, make sure the query ' + Edit1.Text + ' is meaningful!'); raise; end; end;