当前位置:网站首页>[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 .
边栏推荐
- 哈希表-数组之和
- Day 7 of "learning to go concurrent programming in 7 days" go language concurrent programming atomic atomic actual operation includes ABA problem
- 爬虫笔记(1)- urllib
- Professor of Tsinghua University: software testing has gone into a misunderstanding - "code is necessary"
- The karsonzhang/fastadmin addons provided by the system reports an error
- Interview question 3 of software test commonly used by large factories (with answers)
- 同花顺炒股软件可靠吗??安全嘛?
- 登录凭证(cookie+session和Token令牌)
- [MySQL] database function clearance Tutorial Part 2 (window function topic)
- Test automatique de Test logiciel - test d'interface de l'introduction à la maîtrise, apprendre un peu chaque jour
猜你喜欢

二维数组中修改代价最小问题【转换题意+最短路径】(Dijkstra+01BFS)

结构化机器学习项目(二)- 机器学习策略(2)

How to participate in openharmony code contribution
![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)

Exclusive interview with millions of annual salary. What should developers do if they don't fix bugs?

01 golang environment construction
![[microservices] (16) -- distributed transaction Seata](/img/1b/aeb534d5a0bd40f5fc14e64bdf5aa9.png)
[microservices] (16) -- distributed transaction Seata

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

Management system itclub (Part 2)

登录凭证(cookie+session和Token令牌)
随机推荐
Dialogue with Qiao Xinyu: the user is the product manager of Wei brand, and zero anxiety defines luxury
爬虫笔记(3)-selenium和requests
Bean paste green protects your eyes
深度学习又有新坑了!悉尼大学提出全新跨模态任务,用文本指导图像进行抠图
Common APIs (Methods) for scope -number and string
二维数组中修改代价最小问题【转换题意+最短路径】(Dijkstra+01BFS)
Day8 - cloud information project introduction and creation
How to open an account for agricultural futures? How much is the handling charge for opening an account for futures? Who can give you a preferential handling charge?
信通院举办“业务与应用安全发展论坛” 天翼云安全能力再获认可
DCC888 :Register Allocation
不外泄的测试用例设计秘籍--模块测试
YOLOv6:又快又准的目标检测框架开源啦
MONTHS_BETWEEN函数使用
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"
Login credentials (cookie+session and token token)
Golang uses regularity to match substring functions
百万年薪独家专访,开发人员不修复bug怎么办?
爬虫笔记(1)- urllib
[MySQL practice] query statement demonstration