如果是EXCEL的话,比较容易.
unit rexls;interfaceuses comobj,Sysutils,dialogs,db,forms,windows;
procedure readxls(f:Tfilename;dsource:Tdataset;id:string);
implementationprocedure readxls(f:Tfilename;dsource:Tdataset;Id:string);
var xlapplication,sheet:variant;
I,row:integer;
fieldnames:array [1..10] of string;
begin
try
xlapplication:=createoleobject('Excel.application');
xlapplication.visible:=false;
except
application.messagebox('您的系统没有安装Excel,请安装后再使用本功能!','读取文件失败',mb_iconerror+mb_ok);
xlapplication.free;
abort;
end;
try
try
xlapplication.workbooks.open(f);
except
application.MessageBox('打开文件失败!','',mb_iconerror+mb_ok);
end;
sheet:=xlapplication.workbooks[1].worksheets[1];
I:=1;
if dsource.FieldCount>5 then
begin
row:=2;
while (VarAsType(sheet.cells[row,5],varstring)<>'') and (VarAsType(sheet.cells[row,6],varstring)<>'') do
begin
dsource.Insert;
dsource.FieldByName('outtag').asstring:=VarAsType(sheet.cells[row,1],varstring);
dsource.fieldbyname('cust').asstring:=VarAsType(sheet.cells[row,4],varstring);
dsource.fieldbyname('itemcode').asstring:=VarAsType(sheet.cells[row,5],varstring);
dsource.fieldbyname('colcode').asstring:=VarAsType(sheet.cells[row,6],varstring);
dsource.fieldbyname('citem').asstring:=VarAsType(sheet.cells[row,7],varstring);
dsource.fieldbyname('ccolor').asstring:=VarAsType(sheet.cells[row,8],varstring);
dsource.fieldbyname('qty').asstring:=VarAsType(sheet.cells[row,11],varstring);
dsource.fieldbyname('mz').asstring:=VarAsType(sheet.cells[row,12],varstring);
//dsource.fieldbyname('radio').asstring:=VarAsType(sheet.cells[row,14],varstring);
dsource.fieldbyname('ddh').asstring:=VarAsType(sheet.cells[row,2],varstring);
dsource.FieldByName('unit').asstring:='M';
dsource.fieldbyname('type').asstring:='A';
dsource.fieldbyname('vqty').asfloat:=0;
dsource.Post;
inc(row);
end;
end
else //用于对帐
begin
row:=4;
while (VarAsType(sheet.cells[row,1],varstring)<>'') do
begin
dsource.Insert;
dsource.FieldByName('type').asstring:=VarAsType(sheet.cells[row,1],varstring);
dsource.fieldbyname('item').asstring:=VarAsType(sheet.cells[row,2],varstring);
dsource.fieldbyname('color').asstring:=VarAsType(sheet.cells[row,3],varstring);
dsource.fieldbyname('mqty').asinteger:=VarAsType(sheet.cells[row,4],varinteger);
dsource.fieldbyname('wqty').asfloat:=VarAsType(sheet.cells[row,5],varsingle);
dsource.Post;
inc(row);
end;
end;
finally
xlapplication.displayalerts:=false;
xlapplication.workbooks.close;
xlapplication.quit;
end;end;
unit rexls;interfaceuses comobj,Sysutils,dialogs,db,forms,windows;
procedure readxls(f:Tfilename;dsource:Tdataset;id:string);
implementationprocedure readxls(f:Tfilename;dsource:Tdataset;Id:string);
var xlapplication,sheet:variant;
I,row:integer;
fieldnames:array [1..10] of string;
begin
try
xlapplication:=createoleobject('Excel.application');
xlapplication.visible:=false;
except
application.messagebox('您的系统没有安装Excel,请安装后再使用本功能!','读取文件失败',mb_iconerror+mb_ok);
xlapplication.free;
abort;
end;
try
try
xlapplication.workbooks.open(f);
except
application.MessageBox('打开文件失败!','',mb_iconerror+mb_ok);
end;
sheet:=xlapplication.workbooks[1].worksheets[1];
I:=1;
if dsource.FieldCount>5 then
begin
row:=2;
while (VarAsType(sheet.cells[row,5],varstring)<>'') and (VarAsType(sheet.cells[row,6],varstring)<>'') do
begin
dsource.Insert;
dsource.FieldByName('outtag').asstring:=VarAsType(sheet.cells[row,1],varstring);
dsource.fieldbyname('cust').asstring:=VarAsType(sheet.cells[row,4],varstring);
dsource.fieldbyname('itemcode').asstring:=VarAsType(sheet.cells[row,5],varstring);
dsource.fieldbyname('colcode').asstring:=VarAsType(sheet.cells[row,6],varstring);
dsource.fieldbyname('citem').asstring:=VarAsType(sheet.cells[row,7],varstring);
dsource.fieldbyname('ccolor').asstring:=VarAsType(sheet.cells[row,8],varstring);
dsource.fieldbyname('qty').asstring:=VarAsType(sheet.cells[row,11],varstring);
dsource.fieldbyname('mz').asstring:=VarAsType(sheet.cells[row,12],varstring);
//dsource.fieldbyname('radio').asstring:=VarAsType(sheet.cells[row,14],varstring);
dsource.fieldbyname('ddh').asstring:=VarAsType(sheet.cells[row,2],varstring);
dsource.FieldByName('unit').asstring:='M';
dsource.fieldbyname('type').asstring:='A';
dsource.fieldbyname('vqty').asfloat:=0;
dsource.Post;
inc(row);
end;
end
else //用于对帐
begin
row:=4;
while (VarAsType(sheet.cells[row,1],varstring)<>'') do
begin
dsource.Insert;
dsource.FieldByName('type').asstring:=VarAsType(sheet.cells[row,1],varstring);
dsource.fieldbyname('item').asstring:=VarAsType(sheet.cells[row,2],varstring);
dsource.fieldbyname('color').asstring:=VarAsType(sheet.cells[row,3],varstring);
dsource.fieldbyname('mqty').asinteger:=VarAsType(sheet.cells[row,4],varinteger);
dsource.fieldbyname('wqty').asfloat:=VarAsType(sheet.cells[row,5],varsingle);
dsource.Post;
inc(row);
end;
end;
finally
xlapplication.displayalerts:=false;
xlapplication.workbooks.close;
xlapplication.quit;
end;end;
excel中可以直接用ado连接:
select * form 表单名称