当前位置:网站首页>[suggested collection] ABAP essays-excel-4-batch import recommended
[suggested collection] ABAP essays-excel-4-batch import recommended
2022-06-27 22:38:00 【Tab Zhu】
01
Excel Bulk import 3-CL_XLSX_DOCUMENT
How did I come into contact with this class , Thank you Wang Jerry An article from . thank sap The leader of the research institute gave me a more thorough understanding XLSX In fact, there are many XML Composed of files .
Links are as follows : Use ABAP operation Excel Several ways to
Let's look at my program :
DATA error_text TYPE string.
lv_filename = p_file.
CHECK lv_filename ISNOTINITIAL.
CALLMETHOD zcl_document_jxzhu=>import_document_from_frontend " Call method to get Excel data
EXPORTING
pi_filename = lv_filename " File path
* pi_sheetname = 'Sheet1' "sheet Page name
* start_row = '2' " Start to get the line number
check_structure = abap_on " Whether to match the structure to get excel data (X by excel The arrangement order is independent of the field order of the inner table , Match with structure only )
IMPORTING
error_text = error_text " Error message
CHANGING
pt_tab = lt_zmmt001
EXCEPTIONS
file_open_error = 1
OTHERS = 2.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.
cl_demo_output=>display( lt_zmmt001 ).
*create a xlsx handler
DATA(xlsxhandler) = cl_ehfnd_xlsx=>get_instance( ).
*open xlxs into xstring
DATA(xstring_excel) = cl_openxml_helper=>load_local_file( pi_filename ).
*load the xlsx
DATA(xlsxdocument) = xlsxhandler->load_doc( iv_file_data = xstring_excel ).
*extract data from sheet ( pi_sheetname)
DATA(firstsheet) = xlsxdocument->get_sheet_by_name( iv_sheet_name = pi_sheetname ). ” Based on the incoming sheet Name acquisition sheet
*extract data from first sheet
firstsheet = xlsxdocument->get_sheet_by_id( iv_sheet_id = 1 ).
" Get the first one sheet page
"check file structure, first line of excel file
DATA(columncount) = firstsheet->get_last_column_number_in_row( 1 ).
DATA column TYPE i VALUE 1.
" Get how many columns are in the first row , Prepare for the following loop assignment
*get the components of structure Get the component field attributes of the inner table
DATA lw_tab_ref TYPE REF TO data.
CREATE DATA lw_tab_ref LIKE LINE OF pt_tab.
DATA tablestructure TYPE REF TO cl_abap_structdescr.
tablestructure ?= cl_abap_typedescr=>describe_by_data_ref( lw_tab_ref ).
DATA(tablecomponents) = tablestructure->get_components( ).
"get the content of excel. take excel Structure and internal table structure comparison , Judge whether it is consistent
TYPES: BEGIN OF columninfo,
column TYPE i,
columnname TYPE string,
END OF columninfo.
TYPES columnsinfo TYPE STANDARD TABLE OF columninfo
WITH EMPTY KEY.
DATA columnfromfile TYPE columnsinfo.
IF check_structure = abap_on.
* get the title row compare with tab structure if need
DO columncount TIMES.
DATA(cellvalue) = firstsheet->get_cell_content(
EXPORTING
iv_row = 1
iv_column = column ).
APPEND INITIAL LINE TO columnfromfile
ASSIGNING FIELD-SYMBOL(<columnfromfile>).
<columnfromfile>-column = column.
<columnfromfile>-columnname = cellvalue.
IF line_exists( tablecomponents[ name = cellvalue ] ).
DELETE tablecomponents WHERE name = cellvalue.
ELSE.
error_text = error_text && |,{ cellvalue }|.
ENDIF.
column = column + 1.
ENDDO.
IF error_text IS NOT INITIAL.
MESSAGE e001(00) RAISING file_open_error WITH error_text.
ENDIF.
ENDIF.
* get the title row compare with tab structure if need
*get data of excel obtain excel The content of
* Data is obtained according to the structure , And get... In the order of columns excel data
CASE check_structure.
WHEN abap_on.
" Get data according to the corresponding fields
WHILE currentrow <= rowcount.
APPEND INITIAL LINE TO pt_tab ASSIGNING
FIELD-SYMBOL(<currentrow>).
LOOP AT columnfromfile
REFERENCE INTO DATA(currentcolumn).
cellvalue = firstsheet->get_cell_content(
EXPORTING
iv_row = currentrow
iv_column = currentcolumn->*-column ).
ASSIGN COMPONENT currentcolumn->*-columnname
OF STRUCTURE <currentrow> TO FIELD-SYMBOL(<cellvalue>).
<cellvalue> = cellvalue.
ENDLOOP.
currentrow = currentrow + 1.
ENDWHILE.
WHEN OTHERS.
" Get the data in order
CLEAR column.
WHILE currentrow <= rowcount.
APPEND INITIAL LINE TO pt_tab ASSIGNING <currentrow>.
DO columncount TIMES.
column = column + 1.
cellvalue = firstsheet->get_cell_content(
EXPORTING iv_row = currentrow
iv_column = column ).
ASSIGN COMPONENT column
OF STRUCTURE <currentrow> TO <cellvalue>.
<cellvalue> = cellvalue.
ENDDO.
CLEAR column.
currentrow = currentrow + 1.
ENDWHILE.
ENDCASE.Before combination 2 Days' essays , A total of 3 Kind of excel How to import , Mastery should be the first and easiest to master , Of course, the first shortcoming is also the greatest .
Compare the efficiency differences of the three methods : Import 100 Data tests
1.ALSM_EXCEL_TO_INTERNAL Method : when 7 second
2. Guest system OLE- breakthrough 9999 The time limit of the row 6 second ( All of them are ole and 1 It's almost normal )
3.CL_EHFND_XLSX Class time 2 second
From the above , Students can study more carefully XML Mode import excel, You don't have to OLE, Fast and comfortable .
边栏推荐
- Test birds with an annual salary of 50w+ are using this: JMeter script development -- extension function
- Professor of Tsinghua University: software testing has gone into a misunderstanding - "code is necessary"
- 使用sqlite3语句后出现省略号 ... 的解决方法
- 记一次List对象遍历及float类型判断大小
- About the SQL injection of davwa, errors are reported: analysis and verification of the causes of legal mix of settlements for operation 'Union'
- 真香,自从用了Charles,Fiddler已经被我彻底卸载了
- \w和[A-Za-z0-9_],\d和[0-9]等价吗?
- Day 7 of "learning to go concurrent programming in 7 days" go language concurrent programming atomic atomic actual operation includes ABA problem
- 【mysql实战】查询语句实战演示
- How to do function test well? Are you sure you don't want to know?
猜你喜欢

渗透学习-靶场篇-dvwa靶场详细攻略(持续更新中-目前只更新sql注入部分)

百万年薪独家专访,开发人员不修复bug怎么办?

管理系统-ITclub(上)

About the SQL injection of davwa, errors are reported: analysis and verification of the causes of legal mix of settlements for operation 'Union'

Test birds with an annual salary of 50w+ are using this: JMeter script development -- extension function

管理系統-ITclub(下)

Go from introduction to practice - error mechanism (note)

《7天学会Go并发编程》第7天 go语言并发编程Atomic原子实战操作含ABA问题
![The problem of minimum modification cost in two-dimensional array [conversion question + shortest path] (dijkstra+01bfs)](/img/e6/4eb2ddf4d9bac5e40bf2e96656d294.png)
The problem of minimum modification cost in two-dimensional array [conversion question + shortest path] (dijkstra+01bfs)

使用sqlite3语句后出现省略号 ... 的解决方法
随机推荐
Software defect management - a must for testers
Gao fushuai in the unit testing industry, pytest framework, hands-on teaching, will do this in the future test reports~
Typescript learning
Software test automation test -- interface test from entry to proficiency, learn a little every day
This set of steps for performance testing using JMeter includes two salary increases and one promotion
Educational Codeforces Round 108 (Rated for Div. 2)
DCC888 :Register Allocation
Summary of Web testing and app testing by bat testing experts
average-population-of-each-continent
Acwing weekly contest 57- digital operation - (thinking + decomposition of prime factor)
Memoirs of actual combat: breaking the border from webshell
Codeforces Round #721 (Div. 2)
\W and [a-za-z0-9_], \Are D and [0-9] equivalent?
Macro task and micro task understanding
Crawler notes (2) - parse
《7天學會Go並發編程》第7天 go語言並發編程Atomic原子實戰操作含ABA問題
Go from introduction to actual combat - execute only once (note)
【微服务】(十六)—— 分布式事务Seata
正则表达式
Is flush stock trading software reliable?? Is it safe?