读取excel循环语句

sanyuan35 2017-09-30 08:20:41
现要把excel数据导入表中,现有代码,可是到了读取时,循环语句没有读全。
excel数据为:

以下代码读取时,没有update到2017-09-10数据
var
iRE:Integer;
iRow,iCol :Integer;
MsExcel,MsExcelWorkBook,MsExcelWorkSheet:Variant;
i:Integer;
eicol:Integer;
eirow:Integer;
begin
Try
OpenDialog1.FileName:='*.xls';
if not OpenDialog1.Execute then
begin
Exit;
end;
if ExtractFileExt(OpenDialog1.FileName) <> '.xls' then
begin
MessageBox(0, '请选择正确的excel文件',PChar('提示'),MB_OK or MB_ICONWARNING);
Exit;
end;
MsExcel:=CreateOleObject('Excel.Application');
MsExcel.visible:=true;
MsExcelWorkBook:=MsExcel.WorkBooks.Open(OpenDialog1.FileName);

//更新排班主数据
Application.ProcessMessages;//防止进程阻塞
iRow:=2;
iCol:=1;
i:=1;


while trim(msExcel.WorkSheets['sheet1'].Cells[iCol,eicol].value) <> '' do begin

with query.Close;
query.SQL.Text := 'update wh_dict set wh_amount=:wh_ amount where wh_id= :wh_id and wh_name:=wh_name and in_date:=in_date ;

Parameters.ParamByName('wh_id').Value := Trim(msExcel.WorkSheets[1].Cells[iRow,iCol].value);
Parameters.ParamByName('wh_name').Value := Trim(msExcel.WorkSheets[1].Cells[iRow,iCol+1].value);
Parameters.ParamByName(' wh_amount').Value := Trim(msExcel.WorkSheets[1].Cells[iRow,iCol+2].value);
Parameters.ParamByName('duty_date').Value := Trim(msExcel.WorkSheets[1].Cells[i,iCol+2].value);
ExecSQL;
end;
iRow:=iRow+1;

end;
MsExcel.Quit;
Except
MessageBox(self.Handle,'数据导入失败!','系统提示',0);
MsExcel.Quit;
Exit;
end ;
MessageBox(self.Handle,'数据导入成功!','系统提示',0);
end;

...全文
628 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
whb00120012 2017-10-25
  • 打赏
  • 举报
回复
看一下 “'duty_date'”的数据 类型,如是日期型,改为字符串型。
kenlewis 2017-10-12
  • 打赏
  • 举报
回复

   //更新排班主数据
  iRow:=1;      //初始位置,第1行
  iCol:=1;      //初始位置,第1列
  iC:=0;        //按列循环标记
  while trim(msExcel.WorkSheets['sheet1'].Cells[iRow,iCol+1+iC].value) <> '' do  //这句是判断日期那一列是否有内容
  begin
    iR:=0;        //按行循环标记
    while trim(msExcel.WorkSheets['sheet1'].Cells[iRow+iR,iCol].value) <> '' do   //判断这行有没有数据
    begin
      Application.ProcessMessages;//防止进程阻塞
      with query.Close;
        query.SQL.Text := 'update wh_dict set wh_amount=:wh_ amount  where wh_id= :wh_id  and wh_name=:wh_name and in_date=:duty_date  ;
        Parameters.ParamByName('wh_id').Value := Trim(msExcel.WorkSheets[1].Cells[iRow+iR,iCol+iC].value);
        Parameters.ParamByName('wh_name').Value := Trim(msExcel.WorkSheets[1].Cells[iRow+iR,iCol+1+iC].value);
        Parameters.ParamByName('wh_amount').Value := Trim(msExcel.WorkSheets[1].Cells[iRow+iR,iCol+2+iC].value);
        Parameters.ParamByName('duty_date').Value := Trim(msExcel.WorkSheets[1].Cells[iRow,iCol+1+iC].value);
        ExecSQL;
      end;
        iR := iR + 1;
    end;
    iC := iC + 1;
  end;
sanyuan35 2017-10-09
  • 打赏
  • 举报
回复
引用 1 楼 lyhoo163 的回复:
你仅处理了一个日期的值。 你要加一个处理另一个日期的值啊:

  while trim(msExcel.WorkSheets['sheet1'].Cells[iCol,eicol].value) <> '' do
  begin
    with query.Close;
    query.SQL.Text := 'update wh_dict set wh_amount=:wh_ amount  where wh_id= :wh_id  and wh_name:=wh_name and  in_date:=in_date  ;
    Parameters.ParamByName('wh_id').Value := Trim(msExcel.WorkSheets[1].Cells[iRow,iCol].value);
    Parameters.ParamByName('wh_name').Value := Trim(msExcel.WorkSheets[1].Cells[iRow,iCol+1].value);
    Parameters.ParamByName(' wh_amount').Value := Trim(msExcel.WorkSheets[1].Cells[iRow,iCol+2].value); 
    Parameters.ParamByName('duty_date').Value := Trim(msExcel.WorkSheets[1].Cells[i,iCol+2].value);
    ExecSQL;

    with query.Close;
    query.SQL.Text := 'update wh_dict set wh_amount=:wh_ amount  where wh_id= :wh_id  and wh_name:=wh_name and  in_date:=in_date  ;
    Parameters.ParamByName('wh_id').Value := Trim(msExcel.WorkSheets[1].Cells[iRow,iCol].value);
    Parameters.ParamByName('wh_name').Value := Trim(msExcel.WorkSheets[1].Cells[iRow,iCol+1].value);
    Parameters.ParamByName(' wh_amount').Value := Trim(msExcel.WorkSheets[1].Cells[iRow,iCol+3].value); 
    Parameters.ParamByName('duty_date').Value := Trim(msExcel.WorkSheets[1].Cells[i,iCol+3].value);
    ExecSQL;
  end;
    iRow:=iRow+1;
  end;
我后面还有其它的日期,2017-09-11 2017-09-12 ……这些日期是循环的,就是不知道怎么处理这日期循环问题
lyhoo163 2017-10-09
  • 打赏
  • 举报
回复
通过日期的循环(注意位置的循环),再依次读入数据。
lyhoo163 2017-10-03
  • 打赏
  • 举报
回复
你仅处理了一个日期的值。 你要加一个处理另一个日期的值啊:

  while trim(msExcel.WorkSheets['sheet1'].Cells[iCol,eicol].value) <> '' do
  begin
    with query.Close;
    query.SQL.Text := 'update wh_dict set wh_amount=:wh_ amount  where wh_id= :wh_id  and wh_name:=wh_name and  in_date:=in_date  ;
    Parameters.ParamByName('wh_id').Value := Trim(msExcel.WorkSheets[1].Cells[iRow,iCol].value);
    Parameters.ParamByName('wh_name').Value := Trim(msExcel.WorkSheets[1].Cells[iRow,iCol+1].value);
    Parameters.ParamByName(' wh_amount').Value := Trim(msExcel.WorkSheets[1].Cells[iRow,iCol+2].value); 
    Parameters.ParamByName('duty_date').Value := Trim(msExcel.WorkSheets[1].Cells[i,iCol+2].value);
    ExecSQL;

    with query.Close;
    query.SQL.Text := 'update wh_dict set wh_amount=:wh_ amount  where wh_id= :wh_id  and wh_name:=wh_name and  in_date:=in_date  ;
    Parameters.ParamByName('wh_id').Value := Trim(msExcel.WorkSheets[1].Cells[iRow,iCol].value);
    Parameters.ParamByName('wh_name').Value := Trim(msExcel.WorkSheets[1].Cells[iRow,iCol+1].value);
    Parameters.ParamByName(' wh_amount').Value := Trim(msExcel.WorkSheets[1].Cells[iRow,iCol+3].value); 
    Parameters.ParamByName('duty_date').Value := Trim(msExcel.WorkSheets[1].Cells[i,iCol+3].value);
    ExecSQL;
  end;
    iRow:=iRow+1;
  end;

2,497

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 数据库相关
社区管理员
  • 数据库相关社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧