我用Delphi 的 AdoQuery连接 Sql Server 2000 ,却不知道怎么把用Excel做好的
表导入到Sql中 搞了几天都弄不出来诶,不知道怎么用Delphi写程序来实现  各位高手能否帮帮我,小弟先谢了!!!!
 

解决方案 »

  1.   

    //将Excel中的数据导入StringGrid中
    Function ExcelToStringGrid(var StrGrid:TStringGrid;ExcelFileName:string):String;
        Function IsInIt(EmployeeNum:string):boolean;
        var
            i:integer;
            ret:boolean;
        begin
            ret:=False;
            for i:=0 to StrGrid.RowCount-1 do
              begin
                    if EmployeeNum=trim(StrGrid.Cells[0,i]) then
                      begin
                            ret:=True;
                            break;
                      end;
              end;
            result:=ret;
        end;
    var
        ExcelApplication: TExcelApplication;
        ExcelWorkbook: TExcelWorkbook;
        ExcelWorksheet: TExcelWorksheet;
        count,NowRow,i:integer;
        ret:String;
    begin
            ret:='导入成功';
            try
                    screen.Cursor :=crSQLWait;
                    if Not FileExists(ExcelFileName) then
                      begin
                            //文件不存在
                            ret:='文件不存在';
                            exit;
                      end;
                    //测试EXCEL是否安装
                    Try
                            ExcelApplication:=nil;
                            ExcelWorkbook:=nil;
                            ExcelWorksheet:=nil;
                            ExcelApplication:=TExcelApplication.Create(nil);
                            ExcelWorkbook :=TExcelWorkbook.Create(nil);
                            ExcelWorksheet :=TExcelWorksheet.Create(nil);
                            ExcelApplication.Connect;//EXCEL应用程序
                    Except
                            //连接Excel失败
                            ret:='连接Excel失败';
                            exit;
                    End;
                    //打开Excel时是否显示
                    ExcelApplication.Visible[0]:=False;
                    ExcelApplication.DisplayAlerts[0] :=False;
                    //打开指定表格
                    try
                            ExcelApplication.Workbooks.Open(ExcelFileName,
                            null,null,null,null,null,null,null,null,null,null,null,null,0);//打开指定的EXCEL 文件
                    except
                            //打开报表失败
                            ret:='打开报表失败';
                            exit;
                    end;
                    try
                            //ExcelWorkbook1与Eexcelapplication1建立连接
                            ExcelWorkbook.ConnectTo(ExcelApplication.Workbooks[1]);
                            //Excelworksheet1与Excelworkbook1建立连接
                            ExcelWorksheet.ConnectTo(ExcelWorkbook.Worksheets[1] as _Worksheet);
                    except
                            ret:='连接报表失败';
                            exit;
                    end;                {//判断文件是否已导入
                    if Trim(ExcelWorksheet.Cells.Item[2,1])='导入完毕' then
                      begin
                            //该报表已导入
                            ret:='该报表已导入';
                            exit;
                      end;}                try
                            //处理报表
                            Count:=3;
                            StrGrid.RowCount :=2;
                            StrGrid.FixedRows :=1;
                            StrGrid.ColCount :=3;
                            StrGrid.FixedCols :=0;
                    
                            for i:=0 to StrGrid.ColCount-1 do
                                StrGrid.Cells[i,1]:='';                        While Trim(ExcelWorksheet.Cells.Item[Count,1])<>'*****' do
                              begin
                                    Application.ProcessMessages;
                                    //如果超过一千则退出
                                    if Count>=1003 then
                                      begin
                                            Break;
                                      end;
                                    //导入数据
                                    if (Trim(ExcelWorksheet.Cells.Item[Count,1])<>'') and
                                       (Trim(ExcelWorksheet.Cells.Item[Count,2])<>'') and
                                       (Not IsInIt(Trim(ExcelWorksheet.Cells.Item[Count,1]))) then
                                      begin
                                            if (StrGrid.RowCount =2) and (StrGrid.Cells[0,1]='') then
                                              begin
                                                    NowRow:=1;
                                              end
                                            else
                                              begin
                                                    StrGrid.RowCount :=StrGrid.RowCount +1;
                                                    NowRow:=StrGrid.RowCount-1;
                                              end;
                                            //导入编号
                                            if length(Trim(ExcelWorksheet.Cells.Item[Count,1],True))>12 then
                                               StrGrid.Cells[0,NowRow]:=leftstr(Trim(ExcelWorksheet.Cells.Item[Count,1],True),12)
                                            else
                                               StrGrid.Cells[0,NowRow]:=Trim(ExcelWorksheet.Cells.Item[Count,1],True);
                                            //导入姓名
                                            if length(Trim(ExcelWorksheet.Cells.Item[Count,2],True))>10 then
                                               StrGrid.Cells[1,NowRow]:=Leftstr(Trim(ExcelWorksheet.Cells.Item[Count,2],True),10)
                                            else
                                               StrGrid.Cells[1,NowRow]:=Trim(ExcelWorksheet.Cells.Item[Count,2],True);
                                            //导入性别
                                            if Trim(ExcelWorksheet.Cells.Item[Count,3])<>'男' then
                                               StrGrid.Cells[2,NowRow]:='女'
                                            else
                                               StrGrid.Cells[2,NowRow]:='男';
                                      end;
                              
                                    Inc(Count);
                              end;                        //给已导入的文件进行标识
                            //ExcelWorksheet.Cells.Item[2,1]:='导入完毕';
                            //ExcelWorkbook.Save;
                    
                            //导入成功
                            ret:='导入成功';
                    except
                            ret:='导入出现异常错误';
                    end;
            finally
                    ExcelApplication.Disconnect;
                    ExcelApplication.Quit;
                    if ExcelApplication<>nil then
                      begin
                            ExcelApplication.Free;
                            ExcelApplication:=nil;
                      end;
                    if ExcelWorkbook<>nil then
                      begin
                            ExcelWorkbook.Free;
                            ExcelWorkbook:=nil;
                      end;
                    if ExcelWorksheet<>nil then
                      begin
                            ExcelWorksheet.Free;
                            ExcelWorksheet:=nil;
                      end;
                    result:=ret;
                    screen.Cursor :=crDefault;
            end;
    end;
    看看吧。
      

  2.   

    先谢谢了,你帮我大忙了
      
     好的,可以给你加分哈   用Dbgrid 做用相同吗?
      

  3.   

    select * into <table name> from [excel 8.0;database=<filename>].[<sheetname>$]
      

  4.   

    数据多不?
    用 fwjindream(梦中飞) 的方法速度应该比较慢!
      

  5.   

    把  excel改成 8.0 就可以按正常到入数据表一样到入的了
      

  6.   


      不见得好多哈 就是想用Delphi 的 AdoQuery连接 Sql Server 2000 ,写程序把用Excel做好的
    表导入到Sql中
      

  7.   

    用sql语句的话一句够了;看看sql的insert吧;当然你得把excel的数据一条一条的读出然后插入sql表中;