读取excel表,因为无法事先确定excel表有几个字段,想动态在access创建表,然后把excel表数据导进去

解决方案 »

  1.   

    直接adoconnection链接excel,然后就可以向一般数据库那样访问其中的字段数量等信息,根据这些信息去创建表
      

  2.   

    获取字段名的参考代码var
      path,s:string;
      con:Boolean;
      i,j:Integer;
    begin
      path:=ExtractFilePath(Application.ExeName);
      self.ADOConnection3.Connected:=False;
      self.ADOConnection3.ConnectionString:='Provider=MSDASQL.1;Persist Security Info=False;'+
                                            'Data Source=study;Extended Properties="DSN=study;DBQ='+
                                            path+'study;DefaultDir='+path+';DriverId=790;FIL=excel 8.0;'+
                                            'MaxBufferSize=2048;PageTimeout=5;";Initial Catalog='+path+'study';
      try
        self.ADOConnection3.Connected:=True;
        con:=True;
      except
        con:=False;
      end;
      if con=False then
        Exit;
      self.ADOQuery4.Close;
      self.ADOQuery4.Connection:=self.ADOConnection3;
      Self.ADOQuery4.SQL.Clear;
      self.ADOQuery4.SQL.Add('select * from [sheet1$]');//读取sheet1工作表
      self.ADOQuery4.Open;  i:=self.ADOQuery4.Fields.Count;
      for j:=0 to i-1 do
      begin
        s:=self.ADOQuery4.Fields[j].FieldName;//字段名
        self.ListBox1.Items.Add(s);
      end;