请问通过delphi7设计如何将Excel表里的数据导入到sql数据库已有的表中来?,最好能实现:点击‘导入’选择excel表,设置列名和sql表匹配然后导入,请给代码或原程序   不胜感激!

解决方案 »

  1.   

    单元接口部分引用 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;
      

  2.   

       我把你的代码 复制 到了一个按扭里面 编译时出现如下 错误提示:请问我该怎么做?
    [Error] Unit1.pas(33): Undeclared identifier: 'ExcelApp'
    [Error] Unit1.pas(35): Undeclared identifier: 'WorkBook'
      

  3.   

    http://blog.csdn.net/zxf_feng/archive/2008/03/03/2142264.aspx
    看看这个,可以根据修改成你需要的
      

  4.   

    没有引用comobj 
      肯定提示 错误[Error] Unit1.pas(33): Undeclared identifier: 'ExcelApp' 
    [Error] Unit1.pas(35): Undeclared identifier: 'WorkBook'要在单元顶部,uses的列表中加入uses form,system.....,comobj;
      

  5.   

    热干面那不是已经给你写出代码了,已经告诉你要引用 comobj了
    uses comobj;后即可。
      

  6.   

    倒塌……
    好吧,以后就叫热干面算了……
    还有,麻烦请楼主看清楚再做……
    uses都没写,怎么可能是完整的代码……
      

  7.   

    呵呵
    TO:gyk120
    写着方便,表介意,祝你考个好成绩,:)
      

  8.   

    uses comobj   我写了 ,可还是这个问题:
    [Error] Unit1.pas(33): Undeclared identifier: 'ExcelApp' 
    [Error] Unit1.pas(35): Undeclared identifier: 'WorkBook'
      

  9.   

     form写了没?如果还不行看看这里
    http://download.csdn.net/source/341797
    下载这个源码看看
      

  10.   

    在编译的时候我用了:uses comobj   但在编译时  下面两条还是同不过
    try ExcelApp := CreateOleObject('Excel.Application'); WorkBook := ExcelApp.WorkBooks.Open(opendialog.FileName);//使用opendialog对话框指定 
    还是提示:
    [Error] Unit1.pas(37): Undeclared identifier: 'ExcelApp'
    请问我那里错了?  
    还有别的代码或是给我一个源码看看 谢谢了
      

  11.   

    我实在是 搞不来 哪为好心人  能否 重新给我 一段代码,最好把编译通过后的 所有代码给我一个,或发给我一个delphi程序 谢谢了 ,我快急死了!!! 邮箱:[email protected]
      

  12.   

    http://download.csdn.net/source/341797
    这个源码试过没?
      

  13.   

    procedure TForm1.Button1Click(Sender: TObject); 
    var 
      sql:string ; 
    begin 
    try 
      ADOConnection1.Provider :='Microsoft.Jet.OLEDB.4.0;Data Source=C:\temp\Temp.xls;Extended ' + 
         'Properties="Excel 8.0;HDR=Yes;";Persist Security Info=False' ; 
      ADOConnection1.Connected := False ; 
      ADOConnection1.Connected := True ; 
      sqll := 'SELECT * into SQLEXCEL FROM ' + 
        'OpenDataSource(''Microsoft.Jet.OLEDB.4.0'',''Data Source="C:\temp\Temp.xls";Extended Properties="Excel 5.0;HDR=Yes;";Persist Security Info=False'')...sheet1$' ; 
      ADOConnection1.Execute(sql ) ; 
      Application.MessageBox('导出数据成功  !', PChar('数据导入导出提示'), MB_OK + 
        MB_ICONINFORMATION + MB_TOPMOST); 
    except 
      Application.MessageBox('导出数据失败  !', PChar('数据导入导出提示'), MB_OK + 
      MB_ICONINFORMATION + MB_TOPMOST); 
    end; 
    end; //-----------------------------------------------------------------------------------------uses ComObj;
    procedure TForm1.Button1Click(Sender: TObject);
    var
    a_cc: TStrings;
    ExcelApp: Variant;
    begin
    if OpenDialog1.Execute then begin
       {打开保存一次EXCEL文件,为何要保存一次呢?如果是其它程序用第三方控件导出的EXCEL文件,最好做此步,否则容发生错误}
        ExcelApp := CreateOleObject( 'Excel.Application' );
        ExcelApp.WorkBooks.Open(OpenDialog1.FileName);
        ExcelApp.ActiveWorkBook.Save;
        ExcelApp.Quit;
        varclear(ExcelApp);
        
       a_cc :=TStringList.Create;
        {ADOConn是数据库连接控件ADOConnection}
        ADOConn.ConnectionString := 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=' + OpenDialog1.FileName + ';Extended Properties=excel 8.0;Persist Security Info=false';
        ADOConn.Connected := True;
        ADOConn.GetTableNames(a_cc);{将EXCEL文件中的表格名单赋值于a_cc变量}
        with adoquery2 begin
             Close;
             sql.text:='select * from ['+a_cc[0]+']';//得到EXCEL文件中的第一个表格的数据
              open;
          while not eof begin
          adoquery1.Append;
          adoquery1.FieldByName('xf_a').AsString:=FieldValues['工号'];
          adoquery1.FieldByName('xf_b').AsString:=FieldValues['姓名'];
          adoquery1.FieldByName('xf_c').AsString:=FieldValues['部门'];    
          adoquery1.post;  
          next;
          end;
          close;
        end;
         
       adoconn.Connected:=false;
       a_cc.Free;
       messagebox(handle,'数据处理完毕','提示',MB_OK+MB_ICONINFORMATION);
     end;
    end;
    你试试这个……
      

  14.   

    前面的确实是没有定义,在Var后面声明一下就行了:
    var 
      excelx,excely : string; 
      ExcelApp : Variant;