谁知道怎么把excel表导入到dbgrid里面,最好有完整代码,救命了,谢谢(100分)

  • 主题发起人 主题发起人 yiwan10000
  • 开始时间 开始时间
Y

yiwan10000

Unregistered / Unconfirmed
GUEST, unregistred user!
谁知道怎么把excel表导入到dbgrid里面,最好有完整代码,救命了,谢谢
 
使用ado的方式打开excel,指定该数据源和dbgrid连接就可以。
给你一段代码参考。
function InputDataFromExcel(FileName:string; tablename:string; Append_flag:Boolean; keyFieldName:string):integer;
var no,i,j:integer;
tt:TStrings;
SheetName:string; //
begin
with datam.DataM1 do //首先查找数据库中是否已经存在。如果存在,则修改已经存在的数据。
begin
ExcelConnection.Close;
ExcelConnection.ConnectionString:='Provider=MSDASQL.1;Persist Security Info=False;Extended Properties="DSN=Excel Files;'+
'DBQ='+FileName+';DriverId=790;MaxBufferSize=2048;PageTimeout=5;"';
ExcelConnection.Open;
tt:=tstringlist.Create;
ExcelConnection.GetTableNames(tt,true);
SheetName:=tt.Strings[0];
ExcelQuery1.Close;
ExcelQuery1.SQL.Clear;
ExcelQuery1.SQL.Add('select * from ['+SheetName+'] '); //全部数据
ExcelQuery1.Open; //打开EXCEL文件.
if ExcelQuery1.IsEmpty then Exit; //excel中无数据,无法载入.
 
谢谢,不过怎么调用用,能不能给个做好的例子,我菜,谢谢
 
接受答案了.
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
D
回复
0
查看
1K
DelphiTeacher的专栏
D
I
回复
0
查看
676
import
I
后退
顶部