关于用ADODATASET打开的EXCEL文件后,怎么读取第一个工作部问题! 我用ADOdateSet连接Excel文件后,怎么动态给ADODateSet的CommandText赋值来打开Excel文件的第一个工作部?主要问题就是如何能行到EXCEL文件第一个工作部的名称! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 主要问题就是如何能得到EXCEL文件第一个工作薄的名称!有谁知道呀? 建个OLE对象可以得到工作部的名称 var; slTables: TStringList;begin slTables := TStringList.Create; ADOCOnnection.GetTableNames(slTables); ... ADODataSet1.CommandText := 'SELECT * FROM ' + slTables[x]; .... var; slTables: TStringList;begin slTables := TStringList.Create; ADOCOnnection.GetTableNames(slTables); ... ADODataSet1.CommandText := 'SELECT * FROM ' + slTables[x]; .... 我自己写了一个类你看看吧,主要用的是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. [求助]dll文件编写 如何调整TQRChart的Axis的title和label之间的距离? 如何在一个BitMap对象上画一个半透明的块,然后再块上显示文本? 跪求-'怎么让RadioGroup里每一项的字符如lable般支持分行显示?' 如何模拟按下组合键 alt+down 和 alt + a 作毕业设计,要求写报表组件 请问:这样的SQL语句在Delphi怎么写? 如何知道windows当前被激活的窗口的类名(或标题名)? 请问如何操作Dbgrid控件 shellexecute 启动邮件编辑器 有关类型转换的问题? ClearCommError的作用 ---- 急急!!!
有谁知道呀?
slTables: TStringList;
begin
slTables := TStringList.Create;
ADOCOnnection.GetTableNames(slTables);
...
ADODataSet1.CommandText := 'SELECT * FROM ' + slTables[x]; ....
slTables: TStringList;
begin
slTables := TStringList.Create;
ADOCOnnection.GetTableNames(slTables);
...
ADODataSet1.CommandText := 'SELECT * FROM ' + slTables[x]; ....
你看看吧,主要用的是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.