我用ADOdateSet连接Excel文件后,怎么动态给ADODateSet的CommandText赋值来打开Excel文件的第一个工作部?
主要问题就是如何能行到EXCEL文件第一个工作部的名称!

解决方案 »

  1.   

    主要问题就是如何能得到EXCEL文件第一个工作薄的名称!
    有谁知道呀?
      

  2.   

    建个OLE对象可以得到工作部的名称
      

  3.   

    var;
      slTables: TStringList;
    begin
      slTables := TStringList.Create;
      ADOCOnnection.GetTableNames(slTables);
      ...
      ADODataSet1.CommandText := 'SELECT * FROM ' + slTables[x];  ....
      

  4.   

    var;
      slTables: TStringList;
    begin
      slTables := TStringList.Create;
      ADOCOnnection.GetTableNames(slTables);
      ...
      ADODataSet1.CommandText := 'SELECT * FROM ' + slTables[x];  ....
      

  5.   

    我自己写了一个类
    你看看吧,主要用的是OLE可以读出Excel中的很多东西。unit UAppendExcel;interface
      uses classes,comobj,variants,dialogs,SysUtils;
     type
       TTargetFile= class
      Protected
       GetFileCols: integer;
       GetFileRows: integer;
       GetTargetFile: string;
      // GetLastLine: string;
       SetActiveSheet: string;
       GetSheets: TStringList;
       GetColsNames: TStringList;
       GetSheetsCount: integer;
       ExcelApp : Variant;
       procedure GetColNames(cols: integer);
      public
       property  TargetFile: string read GetTargetFile write GetTargetFile;
       property  ActiveSheet: string write SetActiveSheet;
       property  FileCols: integer read GetFileCols;
       property  FileRows: integer read GetFileRows;
      //property  LastLine: string read GetLastLine;
       property  Sheets: TStringList read GetSheets;
       property  ColsNames: TStringList read GetColsNames;
       property  SheetsCount: integer read GetSheetsCount;
       constructor  Create;virtual;
       procedure Refresh(TargetFile: string);
       destructor Destroy; override;
       procedure InsertExcel(NumColumns: integer;FileName: string);
     end;
    implementation{ TTargetFile }constructor TTargetFile.Create;
    begin
     inherited;
     ExcelApp := CreateOleObject( 'Excel.Application' );
     GetSheets:= TStringList.Create;
     GetColsNames:= TStringList.Create;
    end;destructor TTargetFile.Destroy;
    begin
     GetSheets.Free;
     GetColsNames.Free;
     inherited;
    end;procedure TTargetFile.GetColNames(cols: integer);
    var
      FirstCharIndex,SecondCharIndex,i,j: integer;
    begin
      GetColsNames.Clear;
      FirstCharIndex:= Cols div 26;
      SecondCharIndex:= Cols mod 26;
      for i:=0 to SecondCharIndex-1 do
        GetColsNames.Add(chr(65+i));
      if  FirstCharIndex>0 then
      begin
        for i:=0 to FirstCharIndex-1 do
          for j:=0 to SecondCharIndex-1 do
          begin
            GetColsNames.Add(chr(65+i)+chr(65+j));
          end;
      end;end;procedure TTargetFile.InsertExcel(NumColumns: integer; FileName: string);
    var
      vActiveWorkBook: Variant;
      i: integer;
    begin
      try
      try
        if VarIsEmpty(vActiveWorkBook) = True then
           ExcelApp := CreateOleObject( 'Excel.Application' );
      except
        ShowMessage('您的机器里未安装Microsoft Excel');
      end;
       ExcelApp.WorkBooks.Open(FileName);
       vActiveWorkBook:=ExcelApp.ActiveWorkBook;
       if not(SetActiveSheet='') then
        ExcelApp.WorkSheets[SetActiveSheet].Activate;
      ExcelApp.ActiveSheet.Columns[NumColumns].Insert;
      GetFileRows:= ExcelApp.ActiveSheet.UsedRange.Rows.Count;
      ExcelApp.Cells[1,1].Value:= '临时ID';
      for i:=2 to  GetFileRows do
        ExcelApp.Cells[i,1].Value:= IntToStr(i-1);
     finally
       vActiveWorkBook.close ;
       vActiveWorkBook:= Unassigned;
       ExcelApp.Quit;
       ExcelApp:=Unassigned;
     end;
    end;procedure TTargetFile.Refresh(TargetFile: string);
    var
      vActiveWorkBook: Variant;
      i: integer;
    begin
     GetTargetFile:= TargetFile;
     try
      try
        if VarIsEmpty(vActiveWorkBook) = True then
           ExcelApp := CreateOleObject( 'Excel.Application' );
      except
        ShowMessage('您的机器里未安装Microsoft Excel');
      end;
       ExcelApp.WorkBooks.Open(GetTargetFile);
       vActiveWorkBook:=ExcelApp.ActiveWorkBook;
       GetSheetsCount:= vActiveWorkBook.Sheets.Count;
       Sheets.Clear;
       for i:=1 to GetSheetsCount do
        GetSheets.Add(vActiveWorkBook.Sheets[i].Name);
       if not(SetActiveSheet='') then
        ExcelApp.WorkSheets[SetActiveSheet].Activate;
       GetFileCols:= ExcelApp.ActiveSheet.UsedRange.Columns.Count;
       GetFileRows:= ExcelApp.ActiveSheet.UsedRange.Rows.Count;
       //GetLastLine:=
       GetColNames(GetFileCols);
     finally
       vActiveWorkBook.close ;
       vActiveWorkBook:= Unassigned;
       ExcelApp.Quit;
       ExcelApp:=Unassigned;
     end;
    end;end.