將乙個excel**當作是乙個資料庫,利用ado元件實現獲取excel中的資料顯示到介面中,**如下
unit unit1;
inte***ce
uses
windows, messages, sysutils, variants, classes, graphics, controls, forms,
dialogs, db, adodb, grids, dbgrids,comobj;
type
tform1 = class(tform)
con1: tadoconnection;
dbgrd1: tdbgrid;
ds1: tdatasource;
qry1: tadoquery;
procedure formcreate(sender: tobject);
private
public
end;
var form1: tform1;
implementation
procedure tform1.formcreate(sender: tobject);
var excel: olevariant;
excelversion: string;
filename,sheet1: string;
rs,rs1,rs2: _recordset ;
begin
filename:='c:\users\liangcc34589\desktop\測試資料夾\匯出學生資訊.xls';
sheet1:= 'sheet1';
//excel2003及早期的版本
con1.connectionstring:='provider=microsoft.jet.oledb.4.0;data source='+
filename+';extended properties=excel 8.0;'+
'persist security info=false;';
//excel2007及以後的版本
con1.loginprompt:=false;
con1.connected:=true;
qry1.connection:=con1;
qry1.close;
qry1.sql.clear;
qry1.sql.add('select * from ['+sheet1+'$]');
qry1.active:=true;
rs1:=qry1.recordset;
dbgrd1.columns[0].width :=64;
dbgrd1.columns[1].width :=64;
dbgrd1.columns[2].width :=64;
dbgrd1.columns[3].width :=64;
dbgrd1.columns[4].width :=64;
dbgrd1.columns[5].width :=64;
end;
end.
實現效果 Delphi ADO 學習筆記
delphi ado 學習筆記 by shaoyun 2010 03 16 方法一 需要用到4個元件 例項名 元件名 面板 con1 tadoconnection dbgo ds1 tdatasource data access tbl1 tadotable dbgo dbgrd1 tdbgrids...
Delphi7使用ADO直接連線Excel讀取資料
我之前是連線成功的,現在不知道為什麼怎樣連都失敗。最後才知道是office版本的問題,office2007已經是基於xml的。和以前的不一樣。excel2003的連線字串 provider microsoft.jet.oledb.4.0 password data source sfilename ...
exce 操作總結
data excel type ole2 object,excel object macro type ole2 object,macro object w book type ole2 object,list of workbooks book type ole2 object,workbook ...