调试时出错( ExcelApp := CreateOleObject('Excel.Application');) 这一行,请问哪里不对
代码如下
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,comobj, ADODB, DB;type
TForm1 = class(TForm)
OpenDialog1: TOpenDialog;
Button1: TButton;
ADOConnection1: TADOConnection;
ADODataSet1: TADODataSet;
ADOTable1: TADOTable;
ADOQuery1: TADOQuery;
ADOConnection2: TADOConnection;
ADOQuery2: TADOQuery;
ADOTable2: TADOTable;
ADODataSet2: TADODataSet;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
var excelx,excely : string;
begin try ExcelApp := CreateOleObject('Excel.Application'); WorkBook := ExcelApp.WorkBooks.Open(opendialog.FileName);//使用opendialog对话框指定
//excel档路径 ExcelApp.Visible := false; ExcelRowCount := WorkBook.WorkSheets[1].UsedRange.Rows.Count; for i := 1 to excelrowcount + 1 do begin
excelx := excelapp.Cells[i,1].Value; excely := excelapp.Cells[i,2].Value; if ((excelapp.Cells[i,1].Value = '') and (ExcelApp.Cells[i,2].Value = '')) then
//指定excel档的第 i 行 ,第 1,2(看情况而定)行如果为空就退出,这样的设定,最好是你的档案力这两行//对应数据库中不能为空的数据 exit else with query2 do begin close;
sql.clear;
sql.add(insert into test(name,address) values(:name,:address));
parambyname('name').asstring := excelx;//excel档的第一列插入到test表的 name栏位;
parambyname('address').asstring := excely;//excel档的第二列插入到test表的 address 栏位;
execsql;
end;
end; finally WorkBook.Close; ExcelApp.Quit; ExcelApp := Unassigned; WorkBook := Unassigned; end; end;end.
代码如下
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,comobj, ADODB, DB;type
TForm1 = class(TForm)
OpenDialog1: TOpenDialog;
Button1: TButton;
ADOConnection1: TADOConnection;
ADODataSet1: TADODataSet;
ADOTable1: TADOTable;
ADOQuery1: TADOQuery;
ADOConnection2: TADOConnection;
ADOQuery2: TADOQuery;
ADOTable2: TADOTable;
ADODataSet2: TADODataSet;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
var excelx,excely : string;
begin try ExcelApp := CreateOleObject('Excel.Application'); WorkBook := ExcelApp.WorkBooks.Open(opendialog.FileName);//使用opendialog对话框指定
//excel档路径 ExcelApp.Visible := false; ExcelRowCount := WorkBook.WorkSheets[1].UsedRange.Rows.Count; for i := 1 to excelrowcount + 1 do begin
excelx := excelapp.Cells[i,1].Value; excely := excelapp.Cells[i,2].Value; if ((excelapp.Cells[i,1].Value = '') and (ExcelApp.Cells[i,2].Value = '')) then
//指定excel档的第 i 行 ,第 1,2(看情况而定)行如果为空就退出,这样的设定,最好是你的档案力这两行//对应数据库中不能为空的数据 exit else with query2 do begin close;
sql.clear;
sql.add(insert into test(name,address) values(:name,:address));
parambyname('name').asstring := excelx;//excel档的第一列插入到test表的 name栏位;
parambyname('address').asstring := excely;//excel档的第二列插入到test表的 address 栏位;
execsql;
end;
end; finally WorkBook.Close; ExcelApp.Quit; ExcelApp := Unassigned; WorkBook := Unassigned; end; end;end.
WorkBook := ExcelApp.WorkBooks.Open(opendialog.FileName);
Sheet: OleVariant;这两个都加进去了,还是出现以上错误
ExcelRowCount:= WorkBook.workSheets[1].UsedRange.Rows.Count;