Программа выгрузки и загрузки данных из таблиц, посредством текстового файла

Выкладываю исходный код программы которая позволяет загружать и выгружать данные из таблиц в текстовый файл

выгрузка, загрузка данных, загрузка файла,экспорт,export,import

  1. *&---------------------------------------------------------------------*
  2. *& Report ZBC_IMP_EXP_TABLE
  3. *&
  4. *&---------------------------------------------------------------------*
  5. *&
  6. *&
  7. *&---------------------------------------------------------------------*
  8.  
  9. report zbc_imp_exp_table line-size 80
  10. line-count 65
  11. no standard page heading..
  12. type-pools: abap.
  13.  
  14. constants: gc_separator type char3 value '#~#',
  15. gc_name_rec type char2 value 'A0',
  16. gc_header_rec type char2 value 'A1',
  17. gc_record_rec type char2 value 'A2',
  18. gc_0d_replace type char6 value '#~0D~#', " Чем заменить перевод строки
  19. gc_0a_replace type char6 value '#~0A~#'. " Чем заменить перевод строки
  20.  
  21. tables: dd02l.
  22.  
  23. types: begin of lty_tabline,
  24. rectype type char2, " Тип записи файла
  25. line type string, " Строка файла, поля разделены разделителями
  26. end of lty_tabline,
  27. begin of lty_fieldname,
  28. fieldname type dd03l-fieldname,
  29. end of lty_fieldname.
  30.  
  31. types: lty_t_tabline type table of lty_tabline,
  32. lty_t_fieldname type table of lty_fieldname.
  33.  
  34. parameters: p_file type text255 obligatory.
  35. select-options: s_tables for dd02l-tabname.
  36. parameters: p_exp type c radiobutton group 1 default 'X' user-command exp,
  37. p_imp type c radiobutton group 1,
  38. p_del type c as checkbox default ' '.
  39.  
  40. selection-screen begin of block badd with frame title text-add.
  41. parameters: p_cond1 type text50 modif id exp,
  42. p_cond2 type text50 modif id exp,
  43. p_cond3 type text50 modif id exp.
  44. selection-screen end of block badd.
  45.  
  46. *----------------------------------------------------------------------
  47. at selection-screen on value-request for p_file.
  48. perform open_file_name changing p_file.
  49.  
  50. *----------------------------------------------------------------------
  51. at user-command.
  52. case sy-ucomm.
  53. when 'EXP'.
  54. perform output.
  55.  
  56. at selection-screen output.
  57. perform output.
  58.  
  59. *----------------------------------------------------------------------
  60. if p_exp = abap_true.
  61. perform main_export.
  62. perform main_import.
  63.  
  64. *&---------------------------------------------------------------------*
  65. *& Form output
  66. *&---------------------------------------------------------------------*
  67. * text
  68. *----------------------------------------------------------------------*
  69. form output.
  70. loop at screen.
  71. if screen-name = 'P_DEL'.
  72. if p_exp = abap_true.
  73. screen-input = 0.
  74. screen-input = 1.
  75.  
  76. if screen-group1 = 'EXP'.
  77. if p_exp = abap_true.
  78. screen-input = 1.
  79. screen-input = 0.
  80.  
  81. modify screen.
  82. endform. "output
  83.  
  84. *&---------------------------------------------------------------------*
  85. *& Form export_table_to_file
  86. *&---------------------------------------------------------------------*
  87. * text
  88. *----------------------------------------------------------------------*
  89. * --> p1 text
  90. * <-- p2 text
  91. *----------------------------------------------------------------------*
  92. form add_table_to_file using value(u_tabname) type dd02l-tabname
  93. changing ct_contents type lty_t_tabline
  94. value(c_rc) type sysubrc.
  95.  
  96. data: lt_fields type table of lty_fieldname,
  97. lr_data type ref to data,
  98. lv_value type string,
  99. lv_tabix type sy-tabix,
  100. lv_dummy type bapiret2-message.
  101.  
  102. field-symbols: <ls_tabline> type lty_tabline,
  103. <ls_fieldname> type lty_fieldname,
  104. <lt_table> type table,
  105. <ls_line> type any,
  106. <lv_value> type any.
  107.  
  108. * Check if table exists
  109. select single tabname
  110. into u_tabname
  111. from dd02l
  112. where tabname = u_tabname
  113. and tabclass = 'TRANSP'.
  114.  
  115. if sy-subrc <> 0.
  116. message e000(clhp) with 'Не найдена в системе таблица'(001) u_tabname
  117. into lv_dummy.
  118. c_rc = 1.
  119.  
  120. * Заголовок - имя таблицы
  121. append initial line to ct_contents assigning <ls_tabline>.
  122. <ls_tabline>-rectype = gc_name_rec.
  123. <ls_tabline>-line = u_tabname.
  124.  
  125. * Заголовок таблицы (имена полей)
  126. perform get_table_fields using u_tabname changing lt_fields.
  127.  
  128. append initial line to ct_contents assigning <ls_tabline>.
  129. <ls_tabline>-rectype = gc_header_rec.
  130.  
  131. loop at lt_fields assigning <ls_fieldname>.
  132.  
  133. if sy-tabix > 1.
  134. concatenate <ls_tabline>-line gc_separator into <ls_tabline>-line.
  135.  
  136. concatenate <ls_tabline>-line <ls_fieldname>-fieldname into <ls_tabline>-line.
  137.  
  138. * Строки таблицы
  139. create data lr_data type table of (u_tabname).
  140. assign lr_data->* to <lt_table>.
  141.  
  142. " Применяем подходящие условия
  143. do 1 times.
  144.  
  145. if p_cond1 is not initial.
  146. try.
  147.  
  148. from (u_tabname)
  149. into table <lt_table>
  150. where (p_cond1).
  151.  
  152. catch cx_sy_dynamic_osql_semantics.
  153.  
  154. if p_cond2 is not initial.
  155. try.
  156.  
  157. from (u_tabname)
  158. into table <lt_table>
  159. where (p_cond2).
  160.  
  161. catch cx_sy_dynamic_osql_semantics.
  162.  
  163. if p_cond3 is not initial.
  164. try.
  165.  
  166. from (u_tabname)
  167. into table <lt_table>
  168. where (p_cond3).
  169.  
  170. catch cx_sy_dynamic_osql_semantics.
  171.  
  172. from (u_tabname)
  173. into table <lt_table>.
  174.  
  175. loop at <lt_table> assigning <ls_line>.
  176.  
  177. append initial line to ct_contents assigning <ls_tabline>.
  178. <ls_tabline>-rectype = gc_record_rec.
  179.  
  180. loop at lt_fields assigning <ls_fieldname>.
  181.  
  182. lv_tabix = sy-tabix.
  183.  
  184. assign component <ls_fieldname>-fieldname of structure <ls_line> to <lv_value>.
  185. if sy-subrc = 0.
  186. lv_value = <lv_value>.
  187. clear lv_value.
  188.  
  189. replace all occurrences of cl_abap_char_utilities=>cr_lf(1) in lv_value
  190. with gc_0d_replace.
  191.  
  192. replace all occurrences of cl_abap_char_utilities=>cr_lf+1(1) in lv_value
  193. with gc_0a_replace.
  194.  
  195. if lv_tabix > 1.
  196. concatenate <ls_tabline>-line gc_separator into <ls_tabline>-line.
  197.  
  198. concatenate <ls_tabline>-line lv_value into <ls_tabline>-line.
  199. endform. " export_table_to_file
  200. *&---------------------------------------------------------------------*
  201. *& Form get_table_fields
  202. *&---------------------------------------------------------------------*
  203. * Возвращает имена полей таблицы без mandt
  204. *----------------------------------------------------------------------*
  205. * --> p1 text
  206. * <-- p2 text
  207. *----------------------------------------------------------------------*
  208. form get_table_fields using value(u_tabname) type dd02l-tabname
  209. changing ct_fields type lty_t_fieldname.
  210.  
  211. select fieldname
  212. from dd03l
  213. into table ct_fields
  214. where tabname = u_tabname
  215. and precfield = space " Исключаем include
  216. and domname <> 'MANDT'. " Исключаем мандант
  217. endform. " get_table_fields
  218. *&---------------------------------------------------------------------*
  219. *& Form get_all_tabnames_from_file
  220. *&---------------------------------------------------------------------*
  221. * Возвращает список таблиц, которые есть в файле
  222. *----------------------------------------------------------------------*
  223. * --> p1 text
  224. * <-- p2 text
  225. *----------------------------------------------------------------------*
  226. form get_all_tabnames_from_file using ut_content type lty_t_tabline
  227. changing ct_names type string_table.
  228.  
  229. field-symbols: <ls_tabline> type lty_tabline.
  230.  
  231. refresh ct_names[].
  232.  
  233. loop at ut_content assigning <ls_tabline>
  234. where rectype = gc_name_rec.
  235.  
  236. append <ls_tabline>-line to ct_names.
  237. endform. " get_all_tabnames_from_file
  238. *&---------------------------------------------------------------------*
  239. *& Form check_table_structure
  240. *&---------------------------------------------------------------------*
  241. * Выполняет проверку структуры таблицы, возвращает флаг
  242. * успешности-неуспешности и список ошибок если неуспех
  243. *----------------------------------------------------------------------*
  244. * --> p1 text
  245. * <-- p2 text
  246. *----------------------------------------------------------------------*
  247. form check_table_structure using value(u_tabname) type dd02l-tabname
  248. ut_content type lty_t_tabline
  249. changing ct_return type bapiret2_t
  250. value(c_rc) type sysubrc.
  251.  
  252. data: lv_index type sy-tabix,
  253. lv_text type string,
  254. lt_fields_file type table of lty_fieldname,
  255. lt_fields_sap type table of lty_fieldname,
  256. lv_num_fields_sap type i,
  257. lv_num_fields_file type i,
  258. lv_not_found type wdy_boolean.
  259.  
  260. field-symbols: <ls_tabline> type lty_tabline,
  261. <ls_field_file> type lty_fieldname,
  262. <ls_field_sap> type lty_fieldname.
  263.  
  264. * Find the table record
  265. read table ut_content with key rectype = gc_name_rec
  266. line = u_tabname
  267. transporting no fields.
  268.  
  269. if sy-subrc <> 0.
  270.  
  271. concatenate 'Таблица'(002) u_tabname 'не найдена в файле'(003) into lv_text separated by space.
  272. perform msg_to_ret2_string using lv_text 'E' changing ct_return[].
  273. c_rc = 1.
  274.  
  275. * Look for header description line to compare to the database state
  276. lv_index = sy-tabix.
  277. lv_index = lv_index + 1.
  278.  
  279. perform get_first_header_after_line
  280. using
  281. u_tabname
  282. ut_content
  283. lv_index
  284. changing
  285. lt_fields_file[]
  286. c_rc.
  287.  
  288. if c_rc <> 0.
  289. perform msg_to_ret2_string using lv_text 'E' changing ct_return[].
  290.  
  291. * Get fields list from database and perform the comparison
  292. perform get_table_fields using u_tabname changing lt_fields_sap.
  293.  
  294. describe table lt_fields_sap lines lv_num_fields_sap.
  295. describe table lt_fields_file lines lv_num_fields_file.
  296.  
  297. loop at lt_fields_file assigning <ls_field_file>.
  298.  
  299. lv_index = sy-tabix.
  300. lv_not_found = abap_false.
  301.  
  302. unassign <ls_field_sap>.
  303. if lv_index <= lv_num_fields_sap.
  304. read table lt_fields_sap index sy-tabix assigning <ls_field_sap>.
  305.  
  306. if <ls_field_sap> is assigned.
  307. if <ls_field_sap>-fieldname <> <ls_field_file>-fieldname.
  308. lv_not_found = abap_true.
  309. lv_not_found = abap_true.
  310.  
  311. if lv_not_found = abap_true.
  312. concatenate 'В SAP отсутствует (или находится в другой позиции) поле'(004)
  313. <ls_field_file>-fieldname 'таблицы'(005) u_tabname ', имеющееся в файле'(006)
  314. into lv_text separated by space.
  315. perform msg_to_ret2_string using lv_text 'E' changing ct_return[].
  316. c_rc = 1.
  317.  
  318. if lv_num_fields_sap <> lv_num_fields_file.
  319. loop at lt_fields_sap assigning <ls_field_sap>.
  320.  
  321. read table lt_fields_file with key fieldname = <ls_field_sap>-fieldname
  322. transporting no fields.
  323. if sy-subrc <> 0.
  324. concatenate 'В файле отсутствует поле'(007) <ls_field_sap>-fieldname
  325. 'таблицы'(005) u_tabname ', имеющееся в SAP'(008) into lv_text separated by space.
  326. perform msg_to_ret2_string using lv_text 'E' changing ct_return[].
  327. c_rc = 1.
  328. endform. " check_table_structure
  329. *&---------------------------------------------------------------------*
  330. *& Form modify_dbtab_from_file
  331. *&---------------------------------------------------------------------*
  332. * Выполняет модицикацию таблицы из файла
  333. *----------------------------------------------------------------------*
  334. * --> p1 text
  335. * <-- p2 text
  336. *----------------------------------------------------------------------*
  337. form modify_dbtab_from_file using value(u_tabname) type dd02l-tabname
  338. ut_content type lty_t_tabline
  339. value(u_delete) type wdy_boolean
  340. changing value(c_rc) type sysubrc.
  341.  
  342. data: lv_dummy type bapiret2-message,
  343. lv_tabix type sy-tabix,
  344. lr_data type ref to data,
  345. lt_values type table of string,
  346. lv_value type string,
  347. lv_clidep type dd02l-clidep.
  348.  
  349. field-symbols: <ls_tabline> type lty_tabline,
  350. <ls_dbtab_line> type any,
  351. <lv_value> type any,
  352. <ls_fieldname> type lty_fieldname.
  353.  
  354. * Find the table record
  355. read table ut_content with key rectype = gc_name_rec
  356. line = u_tabname
  357. transporting no fields.
  358.  
  359. if sy-subrc <> 0.
  360. message e000(clhp) with 'Таблица'(002) u_tabname 'не найдена в файле'(003)
  361. into lv_dummy.
  362. c_rc = 1.
  363.  
  364. lv_tabix = sy-tabix + 1.
  365.  
  366. select single clidep
  367. into lv_clidep
  368. from dd02l
  369. where tabname = u_tabname
  370. and tabclass = 'TRANSP'.
  371.  
  372. create data lr_data type (u_tabname).
  373. assign lr_data->* to <ls_dbtab_line>.
  374.  
  375. if u_delete = abap_true.
  376. delete from (u_tabname). "#EC CI_NOWHERE
  377.  
  378. loop at ut_content assigning <ls_tabline>
  379. from lv_tabix.
  380. "WHERE rectype = gc_record_rec.
  381.  
  382. " Началась следующая таблица - выходим
  383. if <ls_tabline>-rectype = gc_name_rec.
  384.  
  385. check <ls_tabline>-rectype = gc_record_rec.
  386.  
  387. split <ls_tabline>-line at gc_separator into table lt_values.
  388.  
  389. loop at lt_values into lv_value.
  390.  
  391. lv_tabix = sy-tabix.
  392.  
  393. " Если таблица зависима от манданта, то номер поля будет на 1 больше
  394. if lv_clidep = abap_true.
  395. lv_tabix = lv_tabix + 1.
  396.  
  397. assign component lv_tabix of structure <ls_dbtab_line> to <lv_value>.
  398. if sy-subrc = 0.
  399. <lv_value> = lv_value.
  400.  
  401. modify (u_tabname) from <ls_dbtab_line>.
  402. endform. " modify_dbtab_from_file
  403. *&---------------------------------------------------------------------*
  404. *& Form get_first_header_after_line
  405. *&---------------------------------------------------------------------*
  406. * Технический код: возвращает поля первого заголовка файла
  407. * в загруженном контенте после определенной строки этого файла
  408. *----------------------------------------------------------------------*
  409. * --> p1 text
  410. * <-- p2 text
  411. *----------------------------------------------------------------------*
  412. form get_first_header_after_line using value(u_tabname) type dd02l-tabname
  413. ut_content type lty_t_tabline
  414. value(u_line_from) type i
  415. changing ct_header_fields type lty_t_fieldname
  416. value(c_rc) type sysubrc.
  417.  
  418. data: lv_dummy type bapiret2-message.
  419.  
  420. field-symbols: <ls_tabline> type lty_tabline.
  421.  
  422. loop at ut_content assigning <ls_tabline>
  423. from u_line_from
  424. where rectype = gc_header_rec.
  425.  
  426.  
  427. if sy-subrc <> 0.
  428. message e000(clhp) with 'Для таблицы'(009) u_tabname
  429. 'не найдена строка заголовка в файле'(010) into lv_dummy.
  430. c_rc = 1.
  431.  
  432. * Parse the header string in order to get field list from it
  433. split <ls_tabline>-line at gc_separator into table ct_header_fields.
  434. endform. " get_first_header_after_line
  435. *&---------------------------------------------------------------------*
  436. *& Form load_contents_from_file
  437. *&---------------------------------------------------------------------*
  438. * Загружает файл с диска во внутреннюю таблицу
  439. *----------------------------------------------------------------------*
  440. * --> p1 text
  441. * <-- p2 text
  442. *----------------------------------------------------------------------*
  443. form load_contents_from_file using u_filename
  444. changing ct_content type lty_t_tabline
  445. value(c_rc) type sysubrc.
  446.  
  447. data: begin of lt_data_tab occurs 0,
  448. line(3000) type c,
  449. end of lt_data_tab.
  450.  
  451. data: lv_filename type string,
  452. lv_contents type string,
  453. lt_lines type table of string,
  454. lv_text type string.
  455.  
  456. field-symbols: <ls_tabline> type lty_tabline.
  457.  
  458. lv_filename = u_filename.
  459.  
  460. refresh ct_content[].
  461.  
  462. call method cl_gui_frontend_services=>gui_upload
  463. exporting
  464. filename = lv_filename
  465. read_by_line = abap_false
  466. changing
  467. data_tab = lt_data_tab[]
  468. exceptions
  469. file_open_error = 1
  470. file_read_error = 2
  471. no_batch = 3
  472. gui_refuse_filetransfer = 4
  473. invalid_type = 5
  474. no_authority = 6
  475. unknown_error = 7
  476. bad_data_format = 8
  477. header_not_allowed = 9
  478. separator_not_allowed = 10
  479. header_too_long = 11
  480. unknown_dp_error = 12
  481. access_denied = 13
  482. dp_out_of_memory = 14
  483. disk_full = 15
  484. dp_timeout = 16
  485. not_supported_by_gui = 17
  486. error_no_gui = 18
  487. others = 19.
  488.  
  489. c_rc = sy-subrc.
  490.  
  491. if c_rc <> 0.
  492.  
  493. loop at lt_data_tab.
  494. concatenate lv_contents lt_data_tab into lv_contents.
  495.  
  496. split lv_contents at cl_abap_char_utilities=>cr_lf into table lt_lines.
  497.  
  498. * Каждая строка файла содержит вид записи (2 символа), разделитель, а затем
  499. * уже информацию в зависимости от вида записи
  500. loop at lt_lines into lv_text.
  501.  
  502. check lv_text is not initial.
  503. check strlen( lv_text ) >= 5.
  504.  
  505. append initial line to ct_content assigning <ls_tabline>.
  506. split lv_text at gc_separator into <ls_tabline>-rectype <ls_tabline>-line.
  507.  
  508. replace all occurrences of gc_0d_replace in <ls_tabline>-line
  509. with cl_abap_char_utilities=>cr_lf(1).
  510.  
  511. replace all occurrences of gc_0a_replace in <ls_tabline>-line
  512. with cl_abap_char_utilities=>cr_lf+1(1).
  513. endform. " load_contents_from_file
  514. *&---------------------------------------------------------------------*
  515. *& Form save_contents_to_file
  516. *&---------------------------------------------------------------------*
  517. * Сохраняет содержимое файла из внутренней таблицы на диск
  518. *----------------------------------------------------------------------*
  519. * --> p1 text
  520. * <-- p2 text
  521. *----------------------------------------------------------------------*
  522. form save_contents_to_file using u_filename
  523. ut_content type lty_t_tabline
  524. changing value(c_rc) type sysubrc.
  525.  
  526. data: begin of lt_data_tab occurs 0,
  527. line(3000) type c,
  528. end of lt_data_tab.
  529.  
  530. data: lv_string type string,
  531. lv_lines type i,
  532. lv_filename type string.
  533.  
  534. field-symbols: <ls_tabline> type lty_tabline.
  535.  
  536. lv_filename = u_filename.
  537.  
  538. lv_lines = lines( ut_content ).
  539.  
  540. loop at ut_content assigning <ls_tabline>.
  541.  
  542. if lv_string is not initial.
  543. concatenate lv_string cl_abap_char_utilities=>cr_lf into lv_string.
  544.  
  545. concatenate lv_string <ls_tabline>-rectype gc_separator <ls_tabline>-line
  546. into lv_string.
  547.  
  548. call function 'SCMS_STRING_TO_FTEXT'
  549. exporting
  550. text = lv_string
  551. ftext_tab = lt_data_tab[].
  552.  
  553. call method cl_gui_frontend_services=>gui_download
  554. exporting
  555. filename = lv_filename
  556. write_lf = abap_false
  557. write_lf_after_last_line = abap_false
  558. changing
  559. data_tab = lt_data_tab[] "trunc_trailing_blanks = abap_true
  560. exceptions
  561. file_write_error = 1
  562. no_batch = 2
  563. gui_refuse_filetransfer = 3
  564. invalid_type = 4
  565. no_authority = 5
  566. unknown_error = 6
  567. header_not_allowed = 7
  568. separator_not_allowed = 8
  569. filesize_not_allowed = 9
  570. header_too_long = 10
  571. dp_error_create = 11
  572. dp_error_send = 12
  573. dp_error_write = 13
  574. unknown_dp_error = 14
  575. access_denied = 15
  576. dp_out_of_memory = 16
  577. disk_full = 17
  578. dp_timeout = 18
  579. file_not_found = 19
  580. dataprovider_exception = 20
  581. control_flush_error = 21
  582. not_supported_by_gui = 22
  583. error_no_gui = 23
  584. others = 24.
  585.  
  586. c_rc = sy-subrc.
  587. endform. " save_contents_to_file
  588.  
  589. *&---------------------------------------------------------------------*
  590. *& Form open_file_name
  591. *&---------------------------------------------------------------------*
  592. * text
  593. *----------------------------------------------------------------------*
  594. * -->UC_FILE text
  595. *----------------------------------------------------------------------*
  596. form open_file_name changing uc_file type c.
  597.  
  598. data: lt_filetable type filetable,
  599. ld_str type string,
  600. ld_rc type i.
  601.  
  602. * Отображаем стандартный диалог открытия файла
  603. ld_str = 'Выберите файл для загрузки'(011).
  604. call method cl_gui_frontend_services=>file_open_dialog
  605. exporting
  606. window_title = ld_str
  607. file_filter = 'Text files|*.txt'
  608. multiselection = ' '
  609. changing
  610. file_table = lt_filetable[]
  611. rc = ld_rc. "#EC NOTEXT
  612.  
  613. * Записываем имя файла в параметр
  614. if sy-subrc = 0.
  615. read table lt_filetable index 1 into uc_file.
  616. endform. "open_file_name
  617. *&---------------------------------------------------------------------*
  618. *& Form main_export
  619. *&---------------------------------------------------------------------*
  620. * Главная процедура, выполняющая экспорт
  621. *----------------------------------------------------------------------*
  622. * --> p1 text
  623. * <-- p2 text
  624. *----------------------------------------------------------------------*
  625. form main_export.
  626.  
  627. data: lt_contents type table of lty_tabline,
  628. lv_rc type sysubrc,
  629. lt_log type table of bapiret2,
  630. lv_text type string.
  631.  
  632. field-symbols: <ls_sel_table> like line of s_tables.
  633.  
  634. if s_tables[] is initial.
  635. message e000(clhp) with 'Ни одной таблицы не выбрано'(012).
  636.  
  637. * Формируем файл на основе всех таблиц во внутренней таблице
  638. loop at s_tables assigning <ls_sel_table>.
  639.  
  640. concatenate 'Подготовка таблицы'(013) <ls_sel_table>-low into lv_text separated by space.
  641. perform statusbar_message using 0 lv_text.
  642.  
  643. perform add_table_to_file
  644. using
  645. <ls_sel_table>-low
  646. changing
  647. lt_contents[]
  648. lv_rc.
  649.  
  650. if lv_rc <> 0.
  651. perform log_msg changing lt_log[].
  652. perform show_log using lt_log[].
  653.  
  654. lv_text = 'Сохранение данных в файл'(014).
  655. perform statusbar_message using 0 lv_text.
  656.  
  657. * Сохраняем сформированную таблицу в файл
  658. perform save_contents_to_file
  659. using
  660. p_file
  661. lt_contents[]
  662. changing
  663. lv_rc.
  664.  
  665. if lv_rc <> 0.
  666. perform log_msg changing lt_log[].
  667. perform show_log using lt_log[].
  668.  
  669. message s000(clhp) with 'Данные успешно выгружены в файл'(015) p_file.
  670. endform. " main_export
  671. *&---------------------------------------------------------------------*
  672. *& Form main_import
  673. *&---------------------------------------------------------------------*
  674. * Главная процедура, выполняющая импорт
  675. *----------------------------------------------------------------------*
  676. * --> p1 text
  677. * <-- p2 text
  678. *----------------------------------------------------------------------*
  679. form main_import.
  680.  
  681. data: lt_contents type table of lty_tabline,
  682. lv_rc type sysubrc,
  683. lt_names type table of string,
  684. lv_name type dd02l-tabname,
  685. lt_log type table of bapiret2,
  686. lt_return type table of bapiret2,
  687. lv_global_rc type sysubrc,
  688. lv_text type string,
  689. lv_lines type i,
  690. lv_ans type char1.
  691.  
  692. lv_text = 'Загрузка данных'(016).
  693. perform statusbar_message using 0 lv_text.
  694.  
  695. * Load file to internal table
  696. perform load_contents_from_file
  697. using
  698. p_file
  699. changing
  700. lt_contents
  701. lv_rc.
  702.  
  703. if lv_rc <> 0.
  704. perform log_msg changing lt_log[].
  705. perform show_log using lt_log[].
  706.  
  707. lv_text = 'Проверка данных'(017).
  708. perform statusbar_message using 0 lv_text.
  709.  
  710. * List tables stored in file
  711. perform get_all_tabnames_from_file
  712. using
  713. lt_contents
  714. changing
  715. lt_names.
  716.  
  717. if lt_names[] is initial.
  718. perform msg_to_ret2_string using 'В файле не найдено ни одной таблицы'(018) 'E' changing lt_log[].
  719. perform show_log using lt_log[].
  720.  
  721. * Check each table, table structure must match
  722. loop at lt_names into lv_name.
  723.  
  724. perform check_table_structure
  725. using
  726. lv_name
  727. lt_contents[]
  728. changing
  729. lt_return
  730. lv_rc.
  731.  
  732. if lv_rc <> 0.
  733. append lines of lt_return to lt_log.
  734. lv_global_rc = lv_rc.
  735.  
  736. if lv_global_rc <> 0.
  737. perform msg_to_ret2_string using 'При загрузке файла были ошибки'(019) 'E' changing lt_log[].
  738. perform show_log using lt_log[].
  739.  
  740. * Delete tables not matching the selection
  741. loop at lt_names into lv_name.
  742. if lv_name not in s_tables.
  743. delete lt_names.
  744.  
  745. if lt_names[] is initial.
  746. perform msg_to_ret2_string using 'Ни один из содержащихся в файле таблиц не подходит под критерии выбора, ничего не загружено'(020) 'W'
  747. changing lt_log[].
  748. perform show_log using lt_log[].
  749.  
  750. if lv_global_rc <> 0.
  751. perform show_log using lt_log[].
  752.  
  753. * Several user asks and dialogs
  754. lv_lines = lines( lt_names ).
  755. lv_text = lv_lines.
  756. condense lv_text.
  757. concatenate 'Всего из файла будет загружено'(022) lv_text 'таблиц. Продолжить загрузку?'(023)
  758. into lv_text separated by space.
  759.  
  760. call function 'POPUP_TO_CONFIRM'
  761. exporting
  762. * TITLEBAR = ' '
  763. * DIAGNOSE_OBJECT = ' '
  764. text_question = lv_text
  765. * TEXT_BUTTON_1 = 'Ja'(001)
  766. * ICON_BUTTON_1 = ' '
  767. * TEXT_BUTTON_2 = 'Nein'(002)
  768. * ICON_BUTTON_2 = ' '
  769. * DEFAULT_BUTTON = '1'
  770. * DISPLAY_CANCEL_BUTTON = 'X'
  771. * USERDEFINED_F1_HELP = ' '
  772. * START_COLUMN = 25
  773. * START_ROW = 6
  774. * POPUP_TYPE =
  775. * IV_QUICKINFO_BUTTON_1 = ' '
  776. * IV_QUICKINFO_BUTTON_2 = ' '
  777. importing
  778. answer = lv_ans
  779. * TABLES
  780. * PARAMETER =
  781. exceptions
  782. others = 1.
  783.  
  784. if sy-subrc <> 0.
  785. message id sy-msgid type sy-msgty number sy-msgno
  786. with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  787.  
  788. if lv_ans <> '1'.
  789. message s000(clhp) with 'Загрузка отменена пользователем'(024).
  790.  
  791. call function 'POPUP_TO_CONFIRM'
  792. exporting
  793. * TITLEBAR = ' '
  794. * DIAGNOSE_OBJECT = ' '
  795. text_question = 'Хотите ли вы посмотреть список загружаемых таблиц?'(025)
  796. * TEXT_BUTTON_1 = 'Ja'(001)
  797. * ICON_BUTTON_1 = ' '
  798. * TEXT_BUTTON_2 = 'Nein'(002)
  799. * ICON_BUTTON_2 = ' '
  800. * DEFAULT_BUTTON = '1'
  801. * DISPLAY_CANCEL_BUTTON = 'X'
  802. * USERDEFINED_F1_HELP = ' '
  803. * START_COLUMN = 25
  804. * START_ROW = 6
  805. * POPUP_TYPE =
  806. * IV_QUICKINFO_BUTTON_1 = ' '
  807. * IV_QUICKINFO_BUTTON_2 = ' '
  808. importing
  809. answer = lv_ans
  810. * TABLES
  811. * PARAMETER =
  812. exceptions
  813. others = 1.
  814.  
  815. if sy-subrc <> 0.
  816. message id sy-msgid type sy-msgty number sy-msgno
  817. with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  818.  
  819. if lv_ans = '1'.
  820.  
  821. call function 'POPUP_WITH_TABLE'
  822. exporting
  823. endpos_col = 50
  824. endpos_row = 30
  825. startpos_col = 10
  826. startpos_row = 10
  827. titletext = 'Список загружаемых таблиц'(026)
  828. valuetab = lt_names[]
  829. exceptions
  830. break_off = 1
  831. others = 2.
  832.  
  833. if sy-subrc <> 0 and sy-subrc <> 1.
  834. message id sy-msgid type sy-msgty number sy-msgno
  835. with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  836.  
  837. * Perform load
  838. loop at lt_names into lv_name.
  839.  
  840. concatenate 'Загрузка данных таблицы'(027) lv_name into lv_text separated by space.
  841. perform statusbar_message using 0 lv_text.
  842.  
  843. perform modify_dbtab_from_file
  844. using
  845. lv_name
  846. lt_contents[]
  847. p_del
  848. changing
  849. lv_rc.
  850.  
  851. if lv_rc <> 0.
  852. rollback work. "#EC CI_ROLLBACK
  853. perform log_msg changing lt_log[].
  854. perform show_log using lt_log[].
  855.  
  856. commit work.
  857.  
  858. lv_text = lv_lines.
  859. condense lv_text.
  860. message i000(clhp) with 'Содержимое'(028) lv_text 'таблиц успешно загружено'(029).
  861. endform. " main_import
  862. *&---------------------------------------------------------------------*
  863. *& Form show_log
  864. *&---------------------------------------------------------------------*
  865. * Показывает лог на экран
  866. *----------------------------------------------------------------------*
  867. * --> p1 text
  868. * <-- p2 text
  869. *----------------------------------------------------------------------*
  870. form show_log using ut_log type bapiret2_t.
  871.  
  872. call function 'SUSR_DISPLAY_LOG'
  873. exporting
  874. display_in_popup = abap_true
  875. log_title = 'Журнал обработки программы'(030)
  876. * IT_LOG_SPROT =
  877. it_log_bapiret2 = ut_log[]
  878. exceptions
  879. others = 1.
  880.  
  881. if sy-subrc <> 0.
  882. message id sy-msgid type sy-msgty number sy-msgno
  883. with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  884. endform. " show_log
  885.  
  886. *&---------------------------------------------------------------------*
  887. *& Form statusbar_message
  888. *&---------------------------------------------------------------------*
  889. * text
  890. *----------------------------------------------------------------------*
  891. * -->UD_PERCENT text
  892. * -->UD_TEXT text
  893. *----------------------------------------------------------------------*
  894. form statusbar_message using ud_percent ud_text.
  895. call function 'SAPGUI_PROGRESS_INDICATOR'
  896. exporting
  897. percentage = ud_percent
  898. text = ud_text.
  899. endform. "statusbar_message
  900.  
  901. *&---------------------------------------------------------------------*
  902. *& Form msg_to_ret2_string
  903. *&---------------------------------------------------------------------*
  904. * text
  905. *----------------------------------------------------------------------*
  906. * -->I_MESSAGE_TEXT text
  907. * -->VALUE(I_MSGTY) text
  908. * -->CT_RETURN text
  909. *----------------------------------------------------------------------*
  910. form msg_to_ret2_string using i_message_text
  911. value(i_msgty) type symsgty
  912. changing ct_return type bapiret2_t.
  913.  
  914. data: ls_msg type symsg,
  915. lv_len type i,
  916. lv_dummy type bapiret2-message.
  917.  
  918. if i_msgty <> 'I'
  919. and i_msgty <> 'S'
  920. and i_msgty <> 'W'
  921. and i_msgty <> 'E'
  922. and i_msgty <> 'A'
  923. and i_msgty <> 'X'.
  924.  
  925. i_msgty = 'S'.
  926.  
  927. ls_msg-msgid = 'CLHP'.
  928. ls_msg-msgno = '000'.
  929. ls_msg-msgty = i_msgty.
  930.  
  931. lv_len = strlen( i_message_text ).
  932. if lv_len <= 50.
  933. ls_msg-msgv1 = i_message_text.
  934. if lv_len > 50 and lv_len <= 100.
  935. ls_msg-msgv1 = i_message_text(50).
  936. ls_msg-msgv2 = i_message_text+50.
  937. if lv_len > 100 and lv_len <= 150.
  938. ls_msg-msgv1 = i_message_text(50).
  939. ls_msg-msgv2 = i_message_text+50(50).
  940. ls_msg-msgv3 = i_message_text+100.
  941. if lv_len > 150.
  942. ls_msg-msgv1 = i_message_text(50).
  943. ls_msg-msgv2 = i_message_text+50(50).
  944. ls_msg-msgv3 = i_message_text+100(50).
  945. ls_msg-msgv4 = i_message_text+150.
  946.  
  947. message id ls_msg-msgid type ls_msg-msgty number ls_msg-msgno
  948. with ls_msg-msgv1 ls_msg-msgv2 ls_msg-msgv3 ls_msg-msgv4 into lv_dummy.
  949.  
  950. perform log_msg changing ct_return[].
  951. endform. "msg_to_ret2_string
  952.  
  953. *&---------------------------------------------------------------------*
  954. *& Form log_msg
  955. *&---------------------------------------------------------------------*
  956. * text
  957. *----------------------------------------------------------------------*
  958. * -->CT_RETURN text
  959. *----------------------------------------------------------------------*
  960. form log_msg changing ct_return type bapiret2_t.
  961.  
  962. field-symbols: <ls_return> type bapiret2.
  963.  
  964. append initial line to ct_return assigning <ls_return>.
  965. <ls_return>-id = sy-msgid.
  966. <ls_return>-number = sy-msgno.
  967. <ls_return>-type = sy-msgty.
  968. <ls_return>-message_v1 = sy-msgv1.
  969. <ls_return>-message_v2 = sy-msgv2.
  970. <ls_return>-message_v3 = sy-msgv3.
  971. <ls_return>-message_v4 = sy-msgv4.
  972. endform. "log_msg

Комментарии