请高手赐教
  本人第一次做导入
希望给我具体的代码和步骤!
先谢谢各位大虾了

解决方案 »

  1.   

    你是 EXCEL 导入 到 数据库里 还是什么?
      

  2.   

    三种方法:
    1、通过Excel的VBA实现数据的导入。
    2、通过Delphi的TExcelApplication、TExcelWorkBook和TExcelWorksheet等控件实现数据导入。
    3、通过Dev Express的TcxSpreadSheetBook控件实现数据的导入。
      

  3.   

    procedure TFrmMain.btnOpenXlsClick(Sender: TObject);
    var
      ExcelApp,WorkBook,worksheet: OLEVariant;
      col,row:integer;
      i,j:integer; 
    begin
        try
          ExcelApp:=CreateOleObject('Excel.Application');
          WorkBook:=CreateOleobject('Excel.Sheet');
        except
          application.Messagebox('无法打开Xls文件,请确认已经安装EXCEL.','', mb_OK+mb_IconStop);
          Exit;
        end;
          ExcelApp.Visible := false;
        if opgxls.Execute then
        begin
          try
            adotable2.DisableControls;
            workBook:= ExcelApp.workBooks.Open(OpgXls.FileName);
          except
            Application.MessageBox('你没有选中表','',mb_ok+mb_iconstop);
            exit;
          end;       worksheet:=ExcelApp.workbooks[1].worksheets[1];
           col:=worksheet.usedrange.columns.count;
           row:=worksheet.usedrange.rows.count;
         try
          For i:=3 to row do
          begin
            adotable2.Append;
            For j:=1 to col do
                if j<=adotable2.FieldCount  then
                  adotable2.Fields[j-1].Value:=worksheet.cells[i,j].value;
                  adotable2.post;
          end;
          finally
            adotable2.EnableControls;
            Application.ProcessMessages;
            ExcelApp:=Unassigned;
            workBook.close;
          end;
        end;end;
      

  4.   

    wpyyl 
       为什么会出现 could not convert variant of  type(olestr) into type(boolean)
    的错误啊
    我是先把数据库导出到excel,再把sql中的数据删掉,再进行导入的
    格式应该一致的啊
      

  5.   

    问下wpyyl 
      如何关闭excel啊
    你这样做的话,进程里一直有excel的进程
      

  6.   

    用delphi操操操操作作作作excelhttp://blog.chinaunix.net/u/28618/showart_365771.html
      

  7.   

    TSMExportFromExcel TSImportFromExcel
      

  8.   

    我写过关闭word的,excel没写过,不过应该类似吧,可以参考下.
    var
     ns:word;begin
    ns:=wddonotsavechanges;
    wdapp.quit(ns,emptyparam,emptyparam);//wdapp:wordapplication
    mydoc:=nil;//mydoc:worddocument
    end;
      

  9.   

    现在机子上还没DELPHI,不知到execel的不关闭常量怎么写的,你得自己查看下了,只记得以前写的其中有个关闭word的,上面的.
      

  10.   

    有兩種方法
    1.用ADO連接EXCEL,當數據源來進行數據庫的操作。
    2.通過OLE的建立讀寫EXCEL文件。建議采用第二種方法,因為第一種方法在讀取EXCEL時,如果EXCEL表格的某一字段類型是數字時,將會讀取不到,
    因為這時只能處理字符型的。
      

  11.   


    还可以通过 ADO,将EXCEL作为一个数据库,通过 遍历数据集,然后 循环插入var tmpsource,tmpdest:tadoquery;
        i:integer;
        reccount:string;
    begin
      dm_main.adocnmain.BeginTrans;
      tmpsource:=tadoquery.Create(nil);
      tmpsource.ConnectionString:='provider=microsoft.jet.oledb.4.0;data source='+edt_file.Text+
                                    ';extended properties =excel 8.0;persist security info=false';
      tmpdest:=tadoquery.Create(nil);
      tmpdest.ConnectionString:=adocnmain.ConnectionString;  try
        with tmpsource do
          begin
          sql.Clear;
          sql.Add('select * from [sheet1$]');
          open;
          end;
        gauge1.MaxValue :=tmpsource.RecordCount;
        tmpsource.First;
        reccount:=inttostr(tmpsource.RecordCount);
        //ShowMessage(reccount);
        try
          for i:=0 to tmpsource.RecordCount-1 do
          begin
          with tmpdest do
            begin
            sql.Clear;
            sql.Add('insert into table(f1,f2,f3)');
            sql.Add('values (:v1,:v2,:v3)');
            parameters.ParamByName('v1').Value :=tmpsource.FieldByName('f1').AsString;
            parameters.ParamByName('v2').Value:=tmpsource.FieldByName('f2').AsString;
            parameters.ParamByName('v3').Value:=tmpsource.FieldByName('f3').AsString;
            execsql;
            end;
          tmpsource.Next;
          gauge1.AddProgress(gauge1.MaxValue div tmpsource.RecordCount);
          end;
          adocnmain.CommitTrans;
          application.MessageBox('数据导入成功!','提示',mb_ok+mb_iconinformation);
          close;
        except
          on e:excpetion do
          begin
            adocnmain.RollbackTrans;
            application.MessageBox('数据导入失败!请确定文件存在并且格式正确!','警告',mb_ok+mb_iconwarning);
          end;
        end;
      finally
        tmpsource.Free;
        tmpdest.Free;
      end;
      

  12.   

    用一些兼容excel的控件,节省时间和精力。如ejun.cn,供参考。可惜是收费的