用delphi将EXCEL导入数据库三

单元接口部分引用 comobj 单元(uses )
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 query1 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;
赞同
8
| 评论(2)

转载于:https://www.cnblogs.com/mingdep/archive/2012/01/31/2333735.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值