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

Delphi,导入Excel文件,未声明的标识符,excel,delphi,import,undeclared-identifier,Excel,Delphi,Import,Undeclared Identifier,我正在关注About.com上关于将Excel文件导入Delphi的内容。当我试图编译该文件时,我不断收到一条关于未声明标识符的错误消息。据我所知,该表格的名称和项目中可用的,因为它应该是。我将感谢任何关于修复此错误的建议 unit sample_map; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl

我正在关注About.com上关于将Excel文件导入Delphi的内容。当我试图编译该文件时,我不断收到一条关于未声明标识符的错误消息。据我所知,该表格的名称和项目中可用的,因为它应该是。我将感谢任何关于修复此错误的建议

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;

链接到的教程包含完整的单元。我建议您将该完整单元中的代码与生成的代码进行比较。

您将
TForm1.FetchData的实现放在了错误的位置。不能将其放置在类声明中。它必须在单元的执行部分。您的代码应该如下所示:

interface

....

type
  TForm1 = class(TForm)
    .... // IDE fields here
  private
    procedure FetchData;
  end;

....

implementation

....

procedure TForm1.FetchData;
begin
  .... body of function here
end;

链接到的教程包含完整的单元。我建议您将该完整单元中的代码与生成的代码进行比较。

您已经在接口部分包含了方法的实现。更正接口部分,使其内容如下:-

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;

您已经在接口部分包含了方法的实现。更正接口部分,使其内容如下:-

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;

编译器所说的未声明的标识符是什么?显然是TForm1它的过程TForm1.FetchData;,谢谢:)编译器所说的未声明的标识符是什么?显然是TForm1它的过程TForm1.FetchData;,谢谢:)它成功了,但似乎还有另一个错误。编译器返回:[dcc32 Error]sample_map.pas(43):E2029'=”预期值,但在公共声明后找到了“END”。显然,您的代码还有另一个问题。请记住,我们无法再看到您的代码。我写的答案就是你问的问题的答案。至于你的新问题,请尝试调试它。为什么编译器会期望
=
而不是
结束
?你可以看到代码。你觉得怎么样?大卫,谢谢你的评论。我知道,我的代码中有很多小错误。它工作正常,但似乎还有另一个错误。编译器返回:[dcc32 Error]sample_map.pas(43):E2029'=”预期值,但在公共声明后找到了“END”。显然,您的代码还有另一个问题。请记住,我们无法再看到您的代码。我写的答案就是你问的问题的答案。至于你的新问题,请尝试调试它。为什么编译器会期望
=
而不是
结束
?你可以看到代码。你觉得怎么样?大卫,谢谢你的评论。我知道,代码中有很多小错误。