Advertisement
Guest User

Untitled

a guest
Sep 26th, 2019
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. //Процедура записи данных из файла excel в БД
  3. procedure TEol.ReadEOLExcel(post_id:integer);
  4. var Exl:OleVariant;
  5.     pred_post,prim_post, weldnum: string;
  6.     i,j,ver,eol_post_id,eol_id,eol_child_id: integer;
  7. begin
  8.     OpenDialog1.FileName:='';
  9.     OpenDialog1.Execute;
  10.     fwait.showwait('Открытие файла MS Excel...');
  11.     Exl:=CreateOleObject('Excel.Application');
  12.  
  13.     i:=4;
  14.     weldnum:='1';
  15.  
  16.     if not dm.ADOConnection1.InTransaction then dm.ADOConnection1.BeginTrans;
  17.  
  18.     try
  19.       Exl:=CreateOleObject('Excel.Application');
  20.       if Vartype(Exl)=VarDispatch then
  21.       begin
  22.         Exl.Workbooks.Open(OpenDialog1.FileName);
  23.         fwait.showwait('Чтение данных из MS Excel...');
  24.         eol_id:=Exl.cells[1,7].text;
  25.  
  26.         //Получаем id из таблицы eol, последней версии ОЛ, созданного на данное покупное
  27.         dm.ADOQtemp.Close;
  28.         dm.ADOQtemp.sql.Clear;
  29.         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 +
  30.                             '( 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 +
  31.                             '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)+'))');
  32.         dm.ADOQtemp.Open;
  33.         if dm.ADOQtemp.IsEmpty then
  34.         begin
  35.           showmessage('Информация об ОЛ не найдена');
  36.           exit;
  37.         end;
  38.  
  39.         //Получим версию поставщика
  40.         dm.ADOQtemp2.Close;
  41.         dm.ADOQtemp2.SQL.Clear;
  42.         dm.ADOQtemp2.SQL.Add('select case when max(isnull(ep.ver,0)) > 0 then max(ep.ver) else 1 end ' + #13 +
  43.                              'from eol e '                                                             + #13 +
  44.                              'left join eol_post ep on e.id=ep.eol_id '                                + #13 +
  45.                              '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) );
  46.         dm.ADOQtemp2.Open;
  47.  
  48.         ver:=dm.ADOQtemp2.Fields[0].AsInteger;
  49.         Inc(ver);
  50.  
  51.  
  52.         while not dm.ADOQtemp.Eof do
  53.         begin
  54.           //Добавлю информацию о поставщике в таблицу eol_post на основе id из таблицы eol(последней версии данного ОЛ)
  55.           dm.ADOQtemp2.Close;
  56.           dm.ADOQtemp2.SQL.Clear;
  57.           dm.ADOQtemp2.SQL.Add('insert into eol_post(post_id,eol_id,ver) select ' + inttostr(post_id)                      + #13 +
  58.                                ',' + inttostr(dm.ADOQtemp.FieldByName('id').AsInteger)                             + #13 +
  59.                                ',' + inttostr(ver) + ' select @@identity');
  60.           dm.ADOQtemp2.Open;
  61.           eol_post_id:= dm.ADOQtemp2.Fields[0].AsInteger;
  62.  
  63.           //Добавлю состав eol_post_child для новой версии ОЛ со стороны поставщика оборудования
  64.           dm.ADOQtemp2.Close;
  65.           dm.ADOQtemp2.SQL.Clear;
  66.           dm.ADOQtemp2.SQL.Add('insert into eol_post_child(eol_post_id,eol_child_id,pred_post,notice) '                         + #13 +
  67.                                'select ep.id,ec.id,'''','''' from eol e '                                                       + #13 +
  68.                                'left join eol_child ec on e.id=ec.eol_id '                                                      + #13 +
  69.                                'left join eol_post ep on ep.eol_id=e.id and ep.ver=' + inttostr(ver)                            + #13 +
  70.                                ' where e.id=' + inttostr(dm.ADOQtemp.FieldByName('id').AsInteger)                               + #13 +
  71.                                ' and ep.id='+inttostr(eol_post_id));
  72.           dm.ADOQtemp2.ExecSQL;
  73.           dm.ADOQtemp2.Close;
  74.  
  75.           dm.ADOQtemp.Next;
  76.         end;
  77.  
  78.  
  79.         //Пройдемся по всем строка excel файла
  80.         while weldnum<>'' do
  81.         begin
  82.         //Нас интересуют только столбцы 5 и 6 предложение поставщика и примечания и не выделенные жирным(не разделы,заголовки)
  83.           for j:=5 to 6 do
  84.           begin
  85.             pred_post:='';
  86.             prim_post:='';
  87.             if ((j = 5) and not(Exl.cells[i,5].font.bold)) then
  88.             begin
  89.               pred_post:=Exl.cells[i,5].text;
  90.               eol_child_id:=Exl.cells[i,8].text;
  91.  
  92.               dm.ADOQtemp.Close;
  93.               dm.ADOQtemp.SQL.Clear;
  94.               dm.ADOQtemp.SQL.Add(' update eol_post_child set pred_post=' + quotedstr(pred_post) +
  95.                                   ' from eol_post ep ' +
  96.                                   ' left join eol_post_child epc on epc.eol_post_id=ep.id ' +
  97.                                   ' where eol_child_id = ' + quotedstr(inttostr(eol_child_id)) +
  98.                                   ' and ver=' + quotedstr(inttostr(ver)) +
  99.                                   ' and ep.post_id=' + quotedstr(inttostr(post_id)));
  100.               dm.ADOQtemp.ExecSQL;
  101.             end;
  102.             if ((j = 6) and not(Exl.cells[i,6].font.bold)) then
  103.             begin
  104.               prim_post:=Exl.cells[i,6].text;
  105.               eol_child_id:=Exl.cells[i,8].text;
  106.  
  107.               dm.ADOQtemp.Close;
  108.               dm.ADOQtemp.SQL.Clear;
  109.               dm.ADOQtemp.SQL.Add(' update eol_post_child set notice=' + quotedstr(prim_post) +
  110.                                   ' from eol_post ep ' +
  111.                                   ' left join eol_post_child epc on epc.eol_post_id=ep.id ' +
  112.                                   ' where eol_child_id = ' + quotedstr(inttostr(eol_child_id)) +
  113.                                   ' and ver=' + quotedstr(inttostr(ver)) +
  114.                                   ' and ep.post_id=' + quotedstr(inttostr(post_id)));
  115.               dm.ADOQtemp.ExecSQL;
  116.             end;
  117.           end;
  118.  
  119.           inc(i);
  120.           weldnum:=Exl.cells[i,1].text;
  121.         end;
  122.  
  123.       end;
  124.  
  125.     fwait.Close;
  126.     showmessage('Файл загружен в БД');
  127.     if dm.ADOConnection1.InTransaction then dm.ADOConnection1.CommitTrans;
  128.  
  129.     Exl.quit;
  130.     Exl:=unassigned;
  131.     dm.ADOQtemp.Close;
  132.  
  133.     except
  134.       on e:exception do
  135.       begin
  136.         if dm.ADOConnection1.InTransaction then dm.ADOConnection1.RollbackTrans;
  137.         showmessage('Не удалось считать данные из Excel: '+e.Message);
  138.       end;
  139.     end;
  140. end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement