我不懂啊,高手指教

解决方案 »

  1.   

    用数组接收行不
    for j:=1 to 50 do  //50行
    begin
    num[j-1]:=trim(excelapplication1.Cells.Item[3,j]);   //第3列   
    end;       
      

  2.   

    uses comobj;
    定义string类型的字串或者数组都可以,用之来接受数据。
    比如:
    procedure TForm1.Button3Click(Sender: TObject);
    var
      excelx,excely,sqlStr:string;
      StrValue:array [1..24] of string;
      tmpStr:integer;
      ExcelApp,WorkBook:Variant;
      i,j,ExcelRowCount:integer;
    begin
      if Trim(ExcelPath)='' then
      begin
        MessageBox(handle,'没有选中Excel文件!','警告信息',MB_Ok or MB_ICONWARNING);
        Exit;
      end;
      try
        ExcelApp := CreateOleObject('Excel.Application');    
        WorkBook := ExcelApp.WorkBooks.Open(ExcelPath);//使用opendialog对话框指定excel档路径
        ExcelApp.Visible := false;
        ExcelRowCount := WorkBook.WorkSheets[1].UsedRange.Rows.Count;
        ProgressBar1.Max := ExcelRowCount - 1;
        Memo1.Lines.Clear ;
        for i := 2 to ExcelRowCount do
        //第一行为字段名信息。不取。
        begin 
          //下面两行是取第一列和第二列数据
          //excelx := excelapp.Cells[i,1].Value;
          //excely := excelapp.Cells[i,2].Value;
          
          //下面是数组方式取24列数据
          for j:=1 to 24 do
            StrValue[j]:= excelapp.Cells[i,j].Value;
         
          if ((excelapp.Cells[i,1].Value = '') and (ExcelApp.Cells[i,2].Value = '')) then
          //指定excel档的第 i 行 ,第 1,2(看情况而定)行如果为空就退出,这样的设定,最好是你的档案里这两行对应数据库中不能为空的数据
            Exit
          else
          begin
            with ADOQuery2 do //插入 t_zbclass[账本类别] 表
            begin
              close;
              sql.clear;
              for j:=1 to 24 do
              begin
                if j=1 then
                  sqlStr:=Quotedstr(StrValue[j])
                else
                  sqlStr:=sqlStr+','+Quotedstr(StrValue[j]);
              end;
              sqlStr:= 'Exec sp_ImportExcel '+ sqlStr;
              sql.add(sqlStr);
              open;
            end;
            //Memo1.Lines.Add('导入第 '+IntToStr(i-1)+' 条记录,结果:'+ADOQuery2.Fields[1].AsString);
          end;
        end;    
      finally
        WorkBook.Close;
        ExcelApp.Quit;
        ExcelApp := Unassigned;
        WorkBook := Unassigned;
      end;
    end;