Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Процедура записи данных из файла excel в БД
- procedure TEol.ReadEOLExcel(post_id:integer);
- var Exl:OleVariant;
- pred_post,prim_post, weldnum: string;
- i,j,ver,eol_post_id,eol_id,eol_child_id: integer;
- begin
- OpenDialog1.FileName:='';
- OpenDialog1.Execute;
- fwait.showwait('Открытие файла MS Excel...');
- Exl:=CreateOleObject('Excel.Application');
- i:=4;
- weldnum:='1';
- if not dm.ADOConnection1.InTransaction then dm.ADOConnection1.BeginTrans;
- try
- Exl:=CreateOleObject('Excel.Application');
- if Vartype(Exl)=VarDispatch then
- begin
- Exl.Workbooks.Open(OpenDialog1.FileName);
- fwait.showwait('Чтение данных из MS Excel...');
- eol_id:=Exl.cells[1,7].text;
- //Получаем id из таблицы eol, последней версии ОЛ, созданного на данное покупное
- dm.ADOQtemp.Close;
- dm.ADOQtemp.sql.Clear;
- dm.ADOQtemp.SQL.Add('select id from eol e where mat_id =(select mat_id from eol where id='+inttostr(eol_id)+') and ver =' + #13 +
- '( select case when isnull(max(e.ver),0)>0 then max(e.ver) else (select max(ver) from eol where mat_id='+inttostr(dm.dop_matQid.AsInteger)+')end '+ #13 +
- 'from eol e left join eol_post ep on ep.eol_id=e.id where mat_id=(select mat_id from eol where id='+inttostr(eol_id)+'))');
- dm.ADOQtemp.Open;
- if dm.ADOQtemp.IsEmpty then
- begin
- showmessage('Информация об ОЛ не найдена');
- exit;
- end;
- //Получим версию поставщика
- dm.ADOQtemp2.Close;
- dm.ADOQtemp2.SQL.Clear;
- dm.ADOQtemp2.SQL.Add('select case when max(isnull(ep.ver,0)) > 0 then max(ep.ver) else 1 end ' + #13 +
- 'from eol e ' + #13 +
- 'left join eol_post ep on e.id=ep.eol_id ' + #13 +
- 'where mat_id=(select mat_id from eol where id='+inttostr(eol_id)+') and e.parent_object_id = 0 and isnull(ep.post_id,0)=' + inttostr(post_id) );
- dm.ADOQtemp2.Open;
- ver:=dm.ADOQtemp2.Fields[0].AsInteger;
- Inc(ver);
- while not dm.ADOQtemp.Eof do
- begin
- //Добавлю информацию о поставщике в таблицу eol_post на основе id из таблицы eol(последней версии данного ОЛ)
- dm.ADOQtemp2.Close;
- dm.ADOQtemp2.SQL.Clear;
- dm.ADOQtemp2.SQL.Add('insert into eol_post(post_id,eol_id,ver) select ' + inttostr(post_id) + #13 +
- ',' + inttostr(dm.ADOQtemp.FieldByName('id').AsInteger) + #13 +
- ',' + inttostr(ver) + ' select @@identity');
- dm.ADOQtemp2.Open;
- eol_post_id:= dm.ADOQtemp2.Fields[0].AsInteger;
- //Добавлю состав eol_post_child для новой версии ОЛ со стороны поставщика оборудования
- dm.ADOQtemp2.Close;
- dm.ADOQtemp2.SQL.Clear;
- dm.ADOQtemp2.SQL.Add('insert into eol_post_child(eol_post_id,eol_child_id,pred_post,notice) ' + #13 +
- 'select ep.id,ec.id,'''','''' from eol e ' + #13 +
- 'left join eol_child ec on e.id=ec.eol_id ' + #13 +
- 'left join eol_post ep on ep.eol_id=e.id and ep.ver=' + inttostr(ver) + #13 +
- ' where e.id=' + inttostr(dm.ADOQtemp.FieldByName('id').AsInteger) + #13 +
- ' and ep.id='+inttostr(eol_post_id));
- dm.ADOQtemp2.ExecSQL;
- dm.ADOQtemp2.Close;
- dm.ADOQtemp.Next;
- end;
- //Пройдемся по всем строка excel файла
- while weldnum<>'' do
- begin
- //Нас интересуют только столбцы 5 и 6 предложение поставщика и примечания и не выделенные жирным(не разделы,заголовки)
- for j:=5 to 6 do
- begin
- pred_post:='';
- prim_post:='';
- if ((j = 5) and not(Exl.cells[i,5].font.bold)) then
- begin
- pred_post:=Exl.cells[i,5].text;
- eol_child_id:=Exl.cells[i,8].text;
- dm.ADOQtemp.Close;
- dm.ADOQtemp.SQL.Clear;
- dm.ADOQtemp.SQL.Add(' update eol_post_child set pred_post=' + quotedstr(pred_post) +
- ' from eol_post ep ' +
- ' left join eol_post_child epc on epc.eol_post_id=ep.id ' +
- ' where eol_child_id = ' + quotedstr(inttostr(eol_child_id)) +
- ' and ver=' + quotedstr(inttostr(ver)) +
- ' and ep.post_id=' + quotedstr(inttostr(post_id)));
- dm.ADOQtemp.ExecSQL;
- end;
- if ((j = 6) and not(Exl.cells[i,6].font.bold)) then
- begin
- prim_post:=Exl.cells[i,6].text;
- eol_child_id:=Exl.cells[i,8].text;
- dm.ADOQtemp.Close;
- dm.ADOQtemp.SQL.Clear;
- dm.ADOQtemp.SQL.Add(' update eol_post_child set notice=' + quotedstr(prim_post) +
- ' from eol_post ep ' +
- ' left join eol_post_child epc on epc.eol_post_id=ep.id ' +
- ' where eol_child_id = ' + quotedstr(inttostr(eol_child_id)) +
- ' and ver=' + quotedstr(inttostr(ver)) +
- ' and ep.post_id=' + quotedstr(inttostr(post_id)));
- dm.ADOQtemp.ExecSQL;
- end;
- end;
- inc(i);
- weldnum:=Exl.cells[i,1].text;
- end;
- end;
- fwait.Close;
- showmessage('Файл загружен в БД');
- if dm.ADOConnection1.InTransaction then dm.ADOConnection1.CommitTrans;
- Exl.quit;
- Exl:=unassigned;
- dm.ADOQtemp.Close;
- except
- on e:exception do
- begin
- if dm.ADOConnection1.InTransaction then dm.ADOConnection1.RollbackTrans;
- showmessage('Не удалось считать данные из Excel: '+e.Message);
- end;
- end;
- end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement