现有大量的excle数据要通过客户端上传到数据库里,客户端用delphi 7.0做的

解决方案 »

  1.   

    我是先转成access,再从access中转到sqlserver中的
      

  2.   

    偶经常做excel数据倒入数据库的小程序。下面是一段示例代码,看是否对你有用:    try
          coinitialize(nil);
          ExcelApp := CreateOleObject('Excel.Application');
          ExcelApp.Visible := False;
          ExcelApp.WorkBooks.Open(fileName);
          ProgressBar1.Position := 0;
          ProgressBar1.Max := ExcelApp.ActiveSheet.UsedRange.Rows.Count;
          iRow  := 2;
          fidDept :=  DM.GetObjectIDFromCb(cmbDept);
          fidArea :=  DM.GetObjectIDFromCb(cmbArea);
          fidPro  :=  DM.GetObjectIDFromCb(cmbPro);      while (iRow <= ExcelApp.ActiveSheet.UsedRange.Rows.Count)  do
          begin
              //读取Excel数据...
              FillChar(curRoute, sizeof(TRecRoute), char(0));
              strMemo  :=   '建立时间:'  +  Trim(ExcelApp.Cells[iRow + 1, 4].Value)  + ' ' +
                            '跨越地区:'  +  Trim(ExcelApp.Cells[iRow + 1, 5].Value)  + ' ' +
                            '开关编号:'  +  Trim(ExcelApp.Cells[iRow + 1, 7].Value);          strIncept             :=  Trim(ExcelApp.Cells[iRow + 1, 17].Value);
              strName               :=  Trim(ExcelApp.Cells[iRow + 1, 2].Value);
              curRoute.StrCode      :=  Trim(ExcelApp.Cells[iRow + 1, 1].Value);
              curRoute.StrName      :=  strName;
              curRoute.LineBegin    :=  GetIncept(strName,strIncept,0) ;
              curRoute.LineEnd      :=  GetIncept(strName,strIncept,1) ;
              curRoute.LineLong     :=  GetFloat(Trim(ExcelApp.Cells[iRow + 1, 6].Value));
              curRoute.LoopLong     :=  0.00;
              curRoute.AVLevel      :=  GetFloat(Trim(ExcelApp.Cells[iRow + 1, 3].Value));
              curRoute.BeginTime    :=  GetDate(Trim(ExcelApp.Cells[iRow + 1, 8].Value));
              curRoute.StrMemo      :=  strMemo;
              ProgressBar1.Position :=   iRow;
              iRow := iRow + 1;
              if  curRoute.StrName <> '' then
              begin
                if not InsertOneRoute(curRoute) then  break;
              end;
           end;
           if curRoute.StrName <> '' then
           begin
              InsertOneRoute(curRoute);
           end;
           ProgressBar1.Position := ProgressBar1.Max;
          finally
             btnExecute.Enabled := True;
             ExcelApp.WorkBooks.Close;
             ExcelApp.Quit;
             ExcelApp := NULL;
             CoUninitialize;
           end;
      

  3.   

    给一段我的代码,希望对你有用
    procedure TFrmSh04020100.Button1Click(Sender: TObject);
    {const
        BeginRow = 2; BeginCol = 1;
    var
        Excel: OleVariant;
        iRow, iCol, ID: integer;
        sDtPublish, sYear, sMedia: string;
        strPSQL: string; }
    begin    inherited;
       { if not OpenDialog1.Execute then exit;
        //ShowMessage(OpenDialog1.FileName);    try
            Excel := CreateOLEObject('Excel.Application');
        except
            Application.MessageBox('Excel没有安装!', '提示信息', MB_OK + MB_ICONASTERISK + MB_DEFBUTTON1 + MB_APPLMODAL);
            Exit;
        end;    Excel.Visible := False;
        Excel.WorkBooks.Open(OpenDialog1.FileName);    with cdsExcel do
        begin
            Close;
            CommandText := ' Select * from THD_RPTSCRIPTS ';
            Open;
            strPSQL := 'Select Max(ISCRIPTID)+1 As IMax From THD_RPTSCRIPTS';
            ID := funGetMaxIntID(cdsTemp, strPSQL);
            try
                iRow := BeginRow;
                iCol := BeginCol;
                while trim(Excel.WorkSheets[1].Cells[iRow, iCol + 1].value) <> '' do begin                Append;
                    FieldByName('ISCRIPTID').AsInteger := ID;
                    FieldByName('STITLE').AsString := trim(Excel.WorkSheets[1].Cells[iRow, iCol + 1].value);
                    FieldByName('SSCRIPTKIND').Asstring := trim(Excel.WorkSheets[1].Cells[iRow, iCol + 2].value);
                    FieldByName('SWRITER').Asstring := trim(Excel.WorkSheets[1].Cells[iRow, iCol + 3].value);                FieldByName('SWRITERDEPID').AsString := trim(Excel.WorkSheets[1].Cells[iRow, iCol + 4].value);                sMedia := trim(Excel.WorkSheets[1].Cells[iRow, iCol + 5].value);
                    if sMedia = '' then
                    begin
                        FieldByName('SMEDIA').AsString := sMedia;
                        FieldByName('SIFADOPTED').AsString := '否';
                    end
                    else
                    begin
                        FieldByName('SMEDIA').AsString := sMedia;
                        FieldByName('SIFADOPTED').AsString := '是';
                    end;                //FieldByName('DTPUBLISH').AsString := trim(Excel.WorkSheets[1].Cells[iRow, iCol + 6].value);
                    sDtPublish := trim(Excel.WorkSheets[1].Cells[iRow, iCol + 6].value);
                    FieldByName('DTPUBLISH').AsString := sDtPublish;
                    if sDtPublish <> '' then
                    begin
                        FieldByName('SIFADOPTED').AsString := '是';
                        sYear := Copy(sDtPublish, 1, 4);
                        FieldByName('SYEAR').AsString := sYear;
                    end
                    else
                    begin
                        FieldByName('SIFADOPTED').AsString := '否';
                        FieldByName('SYEAR').AsString := cbYear.Text;
                    end;                FieldByName('SMEMO').AsString := trim(Excel.WorkSheets[1].Cells[iRow, iCol + 7].value);                iRow := iRow + 1;
                    iCol := BeginCol;                ID := ID + 1;                Post;
                end;
                Excel.Quit;
                if ApplyUpdates(-1) = 0 then
                begin
                    Application.MessageBox('导入数据成功!', '提示信息', MB_OK + MB_ICONASTERISK + MB_DEFBUTTON1 + MB_APPLMODAL);
                end
                else
                begin
                    Application.MessageBox('导入数据失败!', '提示信息', MB_OK + MB_ICONASTERISK + MB_DEFBUTTON1 + MB_APPLMODAL);
                end;
            except
                Application.MessageBox('导入数据出错!请检查文件的格式是否正确!', '提示信息', MB_OK + MB_ICONASTERISK + MB_DEFBUTTON1 + MB_APPLMODAL);
                Excel.Quit;
            end;
        end;