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

时间:2014-07-03 10:56:34

标签: excel delphi import undeclared-identifier

我在关于将Excel文件导入Delphi的About.com上关注tutorial。当我尝试编译文件时,我继续收到有关未声明标识符的错误消息。据我所知,表格是名称,在项目中应该是可用的。我将不胜感激任何有关修复此错误的建议。

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.

2 个答案:

答案 0 :(得分:3)

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

interface

....

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

....

implementation

....

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

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

答案 1 :(得分:1)

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

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;