以下代码可获得 Excel 2000文档中Sheet页名称, 若您使用的是Excel XP, 则需对接口函数的参数数量作适当修改.
uses
Excel2000; // 若Delphi加载的是Excel 2000控件
ExcelXP; // 若Delphi加载的是ExcelXP控件
//
// declaration ofConponents needed as below
//
ExcelConn: TADOConnection;
ExcelApplication1: TExcelApplication;
ExcelWorksheet1: TExcelWorksheet;
ExcelWorkbook1: TExcelWorkbook;
function GetFirstSheetName (FileName:
string):
string;
begin
ExcelConn.Close;
try
try
ExcelApplication1.Connect;
ExcelWorkbook1.ConnectTo (
// 下面接口函数的参数数量因Excel版本不同而不同
ExcelApplication1.Workbooks.Open (FileName,
EmptyParam, True, EmptyParam, EmptyParam, EmptyParam, EmptyParam,
EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, 0));
ExcelWorksheet1.ConnectTo (ExcelWorkbook1.Sheets[1]
as _WorkSheet);
Result := ExcelWorksheet1.Name;
except
end;
finally
// 前面语句会打开该文档, 这里必须关闭它
ExcelWorkbook1.Close;
ExcelApplication1.Disconnect;
ExcelConn.Close;
end;
end;