查遍了论坛上的所有帖子,都是将Delphi中数据导入到Excel中,我想问问如何在Delphi中读取Excel数据啊!
我的想法是用TExcelApplication控件打开制定的Excel文件,然后将数据读入到StringGrid控件中,可是用ExcelApplication.Cells[1,1].values不行啊。总说“Class does not have a default property”
请问一下怎么能够读出Excel数据啊!!!???
我的想法是用TExcelApplication控件打开制定的Excel文件,然后将数据读入到StringGrid控件中,可是用ExcelApplication.Cells[1,1].values不行啊。总说“Class does not have a default property”
请问一下怎么能够读出Excel数据啊!!!???
ExcelApplication1.Connect;
ExcelApplication1.Visible[0]:True;
ExcelApplication1.Workbooks.Add(NULL,0);
ExcelWorkbook1.ConnectTo(ExcelApplication1.Workbooks[1]);
ExcelWorkbook1.ConnectTo(ExcelWorkbook1.Sheets[1] as
_WorkSheet);
ExcelSheet1.Cells.Item[X,Y]//就是你要访问的单元格了!!!!!
end;
xlApp.AlertBeforeOverwriting[0] := False;
xlbook.ConnectTo(xlApp.Workbooks.Open(FNEdit.Filename,NULL,false,NULL,NULL,NULL,NULL,NULL,NULL,
NULL,NULL,NULL,NULL,0));
xlSheet.ConnectTo(xlBook.Sheets[1] as _WorkSheet);
edit1.Text := format('%d,%d',[xlSheet.UsedRange[0].Rows.Count, xlSheet.UsedRange[0].Columns.Count]);
rowcount:=xlSheet.UsedRange[0].Rows.Count;
for i:=3 to rowcount-1 do
begin
aaa :=xlsheet.Cells.Item[i,1].value ;
bbb :=xlsheet.Cells.Item[i,2].value ;
end;
i,j:integer;
q_name,E_name:string;
begin
screen.cursor:=crHourGlass ;
Indata.ConnectionString :=
'Provider=Microsoft.Jet.OLEDB.4.0;Data Source='+filename+';Extended Properties=excel 8.0;Persist Security Info=False';
Indata.TableName :='Sheet1$';
Indata.TableDirect :=true;
try
Indata.Active :=true;
except
application.MessageBox('打开数据表出错!','信息提示',64);
exit;
end;
dm.ADOConnection.BeginTrans ;
try
Indata.First ;
while not Indata.Eof do
Begin
Adoquery1.Insert ;
if Adoquery1.FieldDefList.FieldDefs[0].Name='pro_no' then
Adoquery1.Fields[0].AsString:=Pro_no
else
Adoquery1.Fields[0].Value:=Indata.fieldbyname(Indata.FieldDefList.FieldDefs[0].Name).value;
i:=1;
while (i<(Adoquery1.FieldCount -Q_num)) do
Begin
if Adoquery1.FieldDefList.FieldDefs[i].Name='rpr_id' then
i:=i+1
else begin
E_name := Indata.FieldDefList.FieldDefs[i].Name;
q_name:=Adoquery1.FieldDefList.FieldDefs[i].Name;
if Adoquery1.FieldByName(q_name).DataType= ftDateTime then
Adoquery1.FieldByName(q_name).Value :=Indata.fieldbyname(E_name).AsDateTime;
if Adoquery1.FieldByName(q_name).DataType= ftBoolean then
begin
if Indata.fieldbyname(E_name).AsString='FALSE' then
Adoquery1.FieldByName(q_name).Value:=false
else
Adoquery1.FieldByName(q_name).Value:=true;
end else
Adoquery1.FieldByName(q_name).Value :=Indata.fieldbyname(E_name).Value;
i:=i+1;
end;
end;
application.ProcessMessages;
Indata.Next ;
Adoquery1.Post ;
end;
dm.ADOConnection.CommitTrans ;
application.MessageBox('数据导入成功!','信息提示',64);
screen.Cursor := crDefault;
except
application.MessageBox('数据导入失败!','信息提示',64);
dm.ADOConnection.RollbackTrans ;
screen.Cursor := crDefault;
end;
end;