***********从excel表中读数据到oracle表中的问题,高手指点迷津***********

blue_bird3000 2003-09-04 09:11:22
我有一个表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.
...全文
42 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
linuxyf 2003-09-07
  • 打赏
  • 举报
回复
试试这样
procedure TForm1.Button1Click(Sender: TObject);
var
row:integer;
ExcelID: Variant;
begin
if Opendialog1.Execute then
begin
try
adoconnection1.BeginTrans;
row:=1;
ExcelID := CreateOleObject( 'Excel.Application' );
ExcelID.Caption := '应用程序调用 Microsoft Excel';
ExcelID.WorkBooks.Add;
ExcelID.WorkBooks.Open( OpenDialog1.FileName);
ExcelID.WorkSheets[1].Activate;
adocompany.Close;
while string(ExcelID.cells[row,1].value) <> '' do
begin
adocompany.SQL.Clear;
adocompany.SQL.Add('insert into shzf.company (company_id, company_name) values (:s1,:s2)');
adocompany.Parameters.ParamByName('s1').Value:=string(ExcelID.cells[row,1].value);
adocompany.Parameters.ParamByName('s2').Value:=string(ExcelID.cells[row,2].value);
row:=row+1;
adocompany.ExecSQL;
end;
adocompany.Close;
adoconnection1.CommitTrans;
showmessage('导入成功');
except
adoconnection1.RollbackTrans;
showmessage('导入不成功');
end;
ExcelID.WorkBooks.close;
ExcelID.quit;
end;

end;
sixgj 2003-09-06
  • 打赏
  • 举报
回复
顶……

5,930

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧