关于用ADODATASET打开的EXCEL文件后,怎么读取第一个工作部问题!

fs959 2003-08-12 05:11:01
我用ADOdateSet连接Excel文件后,怎么动态给ADODateSet的CommandText赋值来打开Excel文件的第一个工作部?
主要问题就是如何能行到EXCEL文件第一个工作部的名称!
...全文
48 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
nizijian 2003-08-13
  • 打赏
  • 举报
回复
我自己写了一个类
你看看吧,主要用的是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.
DWGZ 2003-08-13
  • 打赏
  • 举报
回复
var;
slTables: TStringList;
begin
slTables := TStringList.Create;
ADOCOnnection.GetTableNames(slTables);
...
ADODataSet1.CommandText := 'SELECT * FROM ' + slTables[x];

....
DWGZ 2003-08-13
  • 打赏
  • 举报
回复
var;
slTables: TStringList;
begin
slTables := TStringList.Create;
ADOCOnnection.GetTableNames(slTables);
...
ADODataSet1.CommandText := 'SELECT * FROM ' + slTables[x];

....
liushiboy 2003-08-13
  • 打赏
  • 举报
回复
建个OLE对象可以得到工作部的名称
fs959 2003-08-13
  • 打赏
  • 举报
回复
主要问题就是如何能得到EXCEL文件第一个工作薄的名称!
有谁知道呀?

5,388

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧