当前位置:网站首页>[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 .
边栏推荐
- Is it safe to open a stock account through the account opening link given by the CICC securities manager? I want to open an account
- Introduce you to ldbc SNB, a powerful tool for database performance and scenario testing
- Basic data type and complex data type
- 【微服务】(十六)—— 分布式事务Seata
- [MySQL] database function clearance Tutorial Part 2 (window function topic)
- Penetration learning - shooting range chapter - detailed introduction to Pikachu shooting range (under continuous update - currently only the SQL injection part is updated)
- Dynamic refresh mapper
- Infiltration learning - problems encountered during SQL injection - explanation of sort=left (version(), 1) - understanding of order by followed by string
- Conversion between flat array and JSON tree
- Day8 - cloud information project introduction and creation
猜你喜欢
结构化机器学习项目(二)- 机器学习策略(2)
Crawler notes (2) - parse
渗透学习-靶场篇-dvwa靶场详细攻略(持续更新中-目前只更新sql注入部分)
百万年薪独家专访,开发人员不修复bug怎么办?
信通院举办“业务与应用安全发展论坛” 天翼云安全能力再获认可
Vue+mysql login registration case
"I make the world cooler" 2022 Huaqing vision R & D product launch was a complete success
Conversation Qiao Xinyu: l'utilisateur est le gestionnaire de produits Wei Brand, zéro anxiété définit le luxe
渗透学习-靶场篇-pikachu靶场详细攻略(持续更新中-目前只更新sql注入部分)
二维数组中修改代价最小问题【转换题意+最短路径】(Dijkstra+01BFS)
随机推荐
管理系统-ITclub(下)
Selenium上传文件有多少种方式?不信你有我全!
CDH集群之YARN性能调优
AQS SOS AQS with me
Process judgment - ternary operation - for loop
爬虫笔记(2)- 解析
Acwing week 57 longest continuous subsequence - (binary or tree array)
[microservices] (16) -- distributed transaction Seata
average-population-of-each-continent
【mysql实战】查询语句实战演示
爬虫笔记(3)-selenium和requests
Common problems encountered by burp Suite
Professor of Tsinghua University: software testing has gone into a misunderstanding - "code is necessary"
01 golang environment construction
Learn to go concurrent programming in 7 days go language sync Application and implementation of cond
Day 7 of "learning to go concurrent programming in 7 days" go language concurrent programming atomic atomic actual operation includes ABA problem
美团20k软件测试工程师的经验分享
go语言切片Slice和数组Array对比panic: runtime error: index out of range问题解决
Is it safe to open a stock account through the account opening link given by the CICC securities manager? I want to open an account
Test birds with an annual salary of 50w+ are using this: JMeter script development -- extension function