使用 ADOConnection1.Execute('select * into personal FROM [excel 8.0;database='+pathstring+'].[sheet1$]');语句导入 Excel 表格时!如果ACCESS数据库中已经存在相同名称的表,则会提示“表 Personal 已存在”!,但是通过 ACCESS 本身导入数据时,可以选择新建或者覆盖原表,如何用 DELPHI 来解决覆盖原表的问题呢?

解决方案 »

  1.   

    你先尝试一下能否对Personal进行select查询, 如果能查询说明它已经存在了,就弹出对话框要求操作者选择是否覆盖。
      

  2.   

    可以举例说明吗?ok999ok(FreeMan)
      

  3.   

    但是我现在遇到这样一个问题了:
    button2为导入按钮,button3为打印按钮,我的程序如下:
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, DB, ADODB,
      Buttons, frxClass, frxDBSet, frxDCtrl;type
      TForm1 = class(TForm)
        ADOConnection1: TADOConnection;
        OpenDialog1: TOpenDialog;
        Button2: TButton;
        ADOQuery1: TADOQuery;
        Edit1: TEdit;
        Label1: TLabel;
        Edit2: TEdit;
        Label2: TLabel;
        ComboBox1: TComboBox;
        ADOConnection2: TADOConnection;
        Button3: TButton;
        SpeedButton1: TSpeedButton;
        SpeedButton2: TSpeedButton;
        Label3: TLabel;
        frxDBDataset1: TfrxDBDataset;
        frxDialogControls1: TfrxDialogControls;
        frxReport1: TfrxReport;
        procedure Button2Click(Sender: TObject);
        procedure Button3Click(Sender: TObject);
        procedure SpeedButton1Click(Sender: TObject);
        procedure SpeedButton2Click(Sender: TObject);
        procedure ComboBox1Select(Sender: TObject);
       
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;
      Sql,filename,sheetname: string;implementation{$R *.dfm}procedure TForm1.Button2Click(Sender: TObject);
    var
      sSql, mdbpath, xlspath: string;
    begin
      if (trim(edit1.Text) = '') or (trim(edit2.Text) = '')or(trim(combobox1.Text) = '') then
      begin
        MessageBox(GetActiveWindow(), '请正确选择相关路径!', '警告', MB_OK +
          MB_ICONWARNING);
        exit;
      end;
      mdbpath := trim(edit1.Text);          //MDB路径
      xlspath := trim(edit2.Text);         //Excel路径
      sheetname:= trim(combobox1.Text);
      ADOConnection1.Connected := False;
      try
        ADOConnection1.ConnectionString :=
          'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=' +
          mdbpath + ';Persist Security Info=False';
        ADOConnection1.Connected := true;
        sSql:='drop table tablename';
        try
            adoconnection1.Connected:=false;
            adoconnection1.Execute(sSql);
            adoconnection1.Connected:=true;
        except
        end;
        try
          adoquery1.Close;
          adoquery1.SQL.Clear;
          sSql :='select * into tablename  FROM [excel 8.0;database=' + xlspath + '].['+sheetname+']';
          adoquery1.Parameters.Clear;
          adoquery1.ParamCheck := false;
          adoquery1.SQL.Text := sSql;
          adoquery1.Execsql;
          MessageBox(GetActiveWindow(), '批量导入EXCEL成功!', '警告', MB_OK +
            MB_ICONWARNING);    except
          MessageBox(GetActiveWindow(), '批量导入EXCEL失败!', '警告', MB_OK +
            MB_ICONWARNING);
        end;
      except
        MessageBox(GetActiveWindow(), '连接ACCESS失败!', '警告', MB_OK +
          MB_ICONWARNING);
      end;
    end;
    procedure TForm1.Button3Click(Sender: TObject);
    begin    adoquery1.Active:=FALSE;
        adoquery1.SQL.Clear;
        adoquery1.SQL.Add('select * from tablename');
        adoquery1.Active:=TRUE;
        frxDBDataset1.DataSet:=adoquery1;
        frxreport1.LoadFromFile('bb.fr3');
        frxReport1.ShowReport;
       // ShowModal;end;procedure TForm1.SpeedButton1Click(Sender: TObject);
    begin
         OpenDialog1.Title := '请选择相应的MDB文件';
      OpenDialog1.Filter := 'access (*.mdb)|*.mdb';
      if OpenDialog1.Execute then
        edit1.Text := OpenDialog1.FileName;
    end;procedure TForm1.SpeedButton2Click(Sender: TObject);
    begin
          OpenDialog1.Title := '请选择相应的Excel文件';
      OpenDialog1.Filter := 'Excel(*.xls)|*.xls';
      if OpenDialog1.Execute then
        edit2.Text := OpenDialog1.FileName;
        ADOConnection2.ConnectionString:='Provider=Microsoft.Jet.OLEDB.4.0;Data Source=' +edit2.text + ';Extended Properties=excel 8.0;Persist Security Info=False';
        ADOConnection2.connected:=true;
        ADOConnection2.GetTableNames(combobox1.items);
    end;procedure TForm1.ComboBox1Select(Sender: TObject);
    begin
        sheetname:=combobox1.text;
    end;end.
    问题:直接按button3能打印,按button2后再按button3则不能打印,请赐教。