现在可以导进数据了,我用了跳转语句。是不是用个跳转像这样:AdoDataSet1.MoveBy(3);直接跳到想要的那行数据就可以了。下面是完整代码看看对不对啊,设计期运行的时候可以,但协同系统运行时报灾难性故障,看看什么原因,指点一下。:
procedure TMainForm.Button2Click(Sender: TObject);
Var
DBName:string;
I,Seq:Integer;
begin
Seq:=1;
DBName:='';
if OpenDialog1.Execute then
DBName:=OpenDialog1.FileName;
if DBName='' then
exit;
ADOConnection1.Connected:= False;
// AdoConnection1.Close;
AdoConnection1.ConnectionString:='Provider=Microsoft.Jet.OLEDB.4.0;Data Source=' + DBName + ';Extended Properties="Excel 8.0;HDR=YES;IMEX=1"';
AdoConnection1.Open;
AdoDataSet1.Close;
AdoDataSet1.CommandText:='select * from [sheet1$]';
AdoDataSet1.Open;
if (AdoDataSet1.RecordCount<=7) then
begin
jsDialogs.ShowMsg('选择的文件中没有数据!', Caption);
Exit;
end;
try
begin
begin
AdoDataSet1.MoveBy(3);
DataSetBroker1.DataSet.Append;
DataSetBroker1.DataSet.FieldByName('TJRQ').AsDateTime:=sysutils.Date; //日期默认为当前日期
// DBName:=sysutils.FormatDateTime('YYYY-MM-DD',sysutils.Date);
DataSetBroker1.DataSet.FieldByName('SPDH').Asstring:=AdoDataSet1.Fields[5].Asstring; //审批单号
if TBizDataSet(DataSetBroker1.DataSet).GetLookupData('FGKS').DataSet.Locate('FDISPLAYNAME', AdoDataSet1.Fields[1].AsString, []) then //分管科室
DataSetBroker1.DataSet.FieldByName('FGKS').AsString:= TBizDataSet(DataSetBroker1.DataSet).GetLookupData('FGKS').DataSet.FieldByName('FDEPT').AsString;
AdoDataSet1.First;
end ;
AdoDataSet1.MoveBy(6);
try
DataSetBroker2.DataSet.DisableControls;
//while not Tdataset(AdoDataSet1).Eof do
for I:= 0 to AdoDataSet1.FieldCount-1 do
begin
if AdoDataSet1.Fields[1].AsString='小计' then Break; //如果值为小计的话说明数据已经插完到此为止
DataSetBroker2.DataSet.Append;
DataSetBroker2.DataSet.FieldByName('SXH').AsInteger:=Seq; //序号
if TBizDataSet(DataSetBroker2.DataSet).GetLookupData('XMDW').DataSet.Locate('XMDW', AdoDataSet1.Fields[0].AsString, []) then
DataSetBroker2.DataSet.FieldByName('XMDW').AsString:= TBizDataSet(DataSetBroker2.DataSet).GetLookupData('XMDW').DataSet.Fields[0].AsString;// 项目单位
DataSetBroker2.DataSet.FieldByName('CGNR').Asstring:=AdoDataSet1.Fields[2].Asstring; //项目名称
DataSetBroker2.DataSet.FieldByName('GG').AsString:='';//AdoDataSet1.Fields[3].Asstring; //规格 默认为空
DataSetBroker2.DataSet.FieldByName('SL').Asstring:=AdoDataSet1.Fields[3].Asstring; //数量
DataSetBroker2.DataSet.FieldByName('CKDJ').Asstring:=AdoDataSet1.Fields[4].Asstring; //采购单价
DataSetBroker2.DataSet.FieldByName('CGYS').AsString:='0';//AdoDataSet1.Fields[9].Asstring; } //采购预算 默认为0
DataSetBroker2.DataSet.FieldByName('HJ').Asstring:=AdoDataSet1.Fields[5].Asstring;
DataSetBroker2.DataSet.FieldByName('YSNZJ').Asstring:=AdoDataSet1.Fields[6].Asstring;
DataSetBroker2.DataSet.FieldByName('YSWZJ').Asstring:=AdoDataSet1.Fields[7].Asstring;
DataSetBroker2.DataSet.FieldByName('YSWDGZJ').Asstring:=AdoDataSet1.Fields[8].Asstring;
DataSetBroker2.DataSet.FieldByName('QTZJ').AsString:=AdoDataSet1.Fields[9].Asstring;
if TBizDataSet(DataSetBroker2.DataSet).GetLookupData('TEMPZJLY').DataSet.Locate('MC', AdoDataSet1.Fields[10].AsString, []) then //资金来源
DataSetBroker2.DataSet.FieldByName('TEMPZJLY').AsString:= TBizDataSet(DataSetBroker2.DataSet).GetLookupData('ZJLY').DataSet.FieldByName('BH').AsString;
if TBizDataSet(DataSetBroker2.DataSet).GetLookupData('LZKM').DataSet.Locate('DMMC', AdoDataSet1.Fields[10].AsString, []) then //列支科目
DataSetBroker2.DataSet.FieldByName('LZKM').AsString:= TBizDataSet(DataSetBroker2.DataSet).GetLookupData('LZKM').DataSet.FieldByName('BH').AsString;
Seq:=Seq+1;
AdoDataSet1.Next;
end;
finally
DataSetBroker2.DataSet.DisableControls;
end;
end;
Except
Dialogs.ShowMessage('检查EXCEL文件名');
end;
AdoConnection1.Close;
end;
end. |