***********从excel表中读数据到oracle表中的问题,高手指点迷津***********
我有一个表company(company_id,company_name),类型分别为varchar2(8),varchar2(64),我要把excel表中保存的两列数据导到数据库表中,
excel中保存两列数据,如:
10222 集团公司
10223 水厂
出错信息如下:
Project Project2.exe raised exception class EVariantTypeCastError with message 'Cound not convert variant of type (String) into (Double)'. Process stopped . Use Step or Run to continue.
程序如下:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
comobj,Dialogs, DB, ADODB, StdCtrls;
type
TForm1 = class(TForm)
ADOConnection1: TADOConnection;
Button1: TButton;
OpenDialog1: TOpenDialog;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
adocompany:TADOQuery;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
adoconnection1.Connected:=true;
adocompany:=TADOQuery.Create(self);
adocompany.Connection:=adoconnection1;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
row:integer;
ExcelID: Variant;
begin
if Opendialog1.Execute then
begin
try
row:=1;
ExcelID := CreateOleObject( 'Excel.Application' );
ExcelID.Caption := '应用程序调用 Microsoft Excel';
ExcelID.WorkBooks.Add;
ExcelID.WorkBooks.Open( OpenDialog1.FileName);
ExcelID.WorkSheets[1].Activate;
while ExcelID.cells[row,1].value <> '' do
begin
adocompany.Close;
adocompany.SQL.Clear;
adocompany.SQL.Add('insert into shzf.COMPANY(COMPANY_ID,COMPANY_NAME) values (:s1,:s2);
adocompany.Parameters.ParamByName('s1').Value:=ExcelID.cells[row,1].value;
adocompany.Parameters.ParamByName('s2').Value:=ExcelID.cells[row,2].value;
adocompany.ExecSQL;
row:=row+1;
end;
adocompany.Close;
showmessage('导入成功');
finally
ExcelID.WorkBooks.close;
ExcelID.quit;
end;
end;
end;
end.