当前位置:网站首页>[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 .
边栏推荐
- Golang uses regularity to match substring functions
- Yolov6: the fast and accurate target detection framework is open source
- average-population-of-each-continent
- Memoirs of actual combat: breaking the border from webshell
- Character interception triplets of data warehouse: substrb, substr, substring
- Record a list object traversal and determine the size of the float type
- Vue+mysql login registration case
- DCC888 :Register Allocation
- Day 7 of "learning to go concurrent programming in 7 days" go language concurrent programming atomic atomic actual operation includes ABA problem
- 清华大学教授:软件测试已经走入一个误区——“非代码不可”
猜你喜欢

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

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

爬虫笔记(2)- 解析

Yarn中RMApp、RMAppAttempt、RMContainer和RMNode状态机及其状态转移

Windwos 8.1系统安装vmware tool插件报错的解决方法

《7天学会Go并发编程》第六天 go语言Sync.cond的应用和实现 go实现多线程联合执行

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

Deep learning has a new pit! The University of Sydney proposed a new cross modal task, using text to guide image matting

Read write separation master-slave replication of MySQL

6G显卡显存不足出现CUDA Error:out of memory解决办法
随机推荐
"I make the world cooler" 2022 Huaqing vision R & D product launch was a complete success
Yarn performance tuning of CDH cluster
Crawler notes (2) - parse
go语言切片Slice和数组Array对比panic: runtime error: index out of range问题解决
regular expression
渗透学习-靶场篇-dvwa靶场详细攻略(持续更新中-目前只更新sql注入部分)
[MySQL practice] query statement demonstration
哈希表-数组之和
《7天學會Go並發編程》第7天 go語言並發編程Atomic原子實戰操作含ABA問題
从学生到工程师的蜕变之路
爬虫笔记(2)- 解析
How to do function test well? Are you sure you don't want to know?
如何做好功能测试?你确定不想知道吗?
01 golang environment construction
C # QR code generation and recognition, removing white edges and any color
MONTHS_BETWEEN函数使用
关于davwa的SQL注入时报错:Illegal mix of collations for operation ‘UNION‘原因剖析与验证
Summary of Web testing and app testing by bat testing experts
宏任务、微任务理解
Kill the general and seize the "pointer" (Part 2)