急!请问在DELPHI中把EXCEL的表格数据导入在数据库中?(200分)

  • 主题发起人 主题发起人 jact007
  • 开始时间 开始时间
J

jact007

Unregistered / Unconfirmed
GUEST, unregistred user!
excel表格挺复杂的,如何将EXCEL中某些行或列的内容通过编程抓到数据库中.
如有兴趣,E_MAIL:jact007@21cn.com 联系!
 
With ExcelApp do //ExcelApp是通过OLE方法调用Excel服务,并假定已打开了相应的excel文件。
begin
SqlString:='insert into table values('+Cells[1,1].Value+','+Cells[1,2].Value+...+Cells[1,6].Value+')'; //假定Excel表中一行有6列,插入这一行的数值
AdoCommand1.CommandText:=SqlString;
AdoCommand1.Execute;
end;
 
两个TAdoDataSet,一个连接数据库,一个连接Excel,
循环insert即可
 
我以前做过,给你一段代码,希望能给你带来点启示。
procedure TForm1.SpeedButton1Click(Sender: TObject);
var
i,j:integer;
aa,bb:string;
Myarray:array[1..100,1..2]of integer;

begin
if ListBox1.ItemIndex>0 then
ListBox1.ItemIndex:=ListBox1.ItemIndex-1;
label3.Caption:=inttostr(ListBox1.ItemIndex);
//建立数据导入的联结关系,得到字段对应数组。
//array〔i,1〕 记录目的字段;array〔i,2〕记录EXCEl中的列数。
for i:=0 to listbox1.Items.Count-1 do begin
myarray[i+1,1]:=i+1;
for j:=1 to ExcelWorksheet1.Cells.Columns.Count do begin
aa:=ExcelWorksheet1.Cells.item[1,j];
if trim(aa)='' then break;
if listbox2.Items=aa then
myarray[i+1,2]:=j;
end;
end;

//数据导入
i:=1;
query1.Active:=true;
query1.Edit;
while trim(ExcelWorksheet1.Cells.item[i+1,1])<>'' do begin
aa:=ExcelWorksheet1.Cells.item[i+1,1];
if query1.Locate('ID',aa,[loCaseInsensitive])then begin
query1.edit;
for j:=1 to listbox1.Items.Count do begin
if myarray[j,2]<>0 then
Query1.Fields[j-1].Value:=ExcelWorksheet1.Cells.item[i+1,myarray[j,2]];
end;
// showmessage('有重复记录。')
end else begin
query1.edit;
query1.insert;
for j:=1 to listbox1.Items.Count do begin
if myarray[j,2]<>0 then
Query1.Fields[j-1].Value:=ExcelWorksheet1.Cells.item[i+1,myarray[j,2]];
end;
end;
Label3.caption:=inttostr(i);
i:=i+1;

end;
query1.post;
showmessage('共导入'+inttostr(i-1)+'条员工信息。');


end;
 
用OLE就行了,然后使用循环把内容读到数据库中.
 
如何從excel文件(此文件存儲位置不固定)打開哩?也就是說:用代碼來寫一段去打開一`個excel文
件該如何做?
 
多人接受答案了。
 
后退
顶部