当前位置:网站首页>【建议收藏】ABAP随笔-EXCEL-4-批量导入-推荐
【建议收藏】ABAP随笔-EXCEL-4-批量导入-推荐
2022-06-27 16:54:00 【Tab Zhu】
01
Excel批量导入3-CL_XLSX_DOCUMENT
我是如何接触到这个类的呢,就要感谢Wang Jerry 的一篇文章。感谢sap研究院大佬让我更透彻的了解了一下XLSX其实就是多个XML文件组合成的。
链接如下:使用ABAP操作Excel的几种方法
下面看我的程序:
DATA error_text TYPE string.
lv_filename = p_file.
CHECK lv_filename ISNOTINITIAL.
CALLMETHOD zcl_document_jxzhu=>import_document_from_frontend "调用方法获取Excel数据
EXPORTING
pi_filename = lv_filename " 文件路径
* pi_sheetname = 'Sheet1' "sheet 页名称
* start_row = '2' "开始获取的行号
check_structure = abap_on "是否匹配结构去获取excel数据(X为 excel排列顺序和内表字段顺序无关,仅用结构来匹配)
IMPORTING
error_text = error_text "错误消息
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 ). ”根据传入的sheet名称获取sheet
*extract data from first sheet
firstsheet = xlsxdocument->get_sheet_by_id( iv_sheet_id = 1 ).
"获取第一个sheet页
"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 the components of structure 得到内表的组成字段属性
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. 将excel结构和内表结构对比,判断是否一致
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 获取excel的内容
*有按照结构来获取数据,和按照列的顺序来获取excel数据
CASE check_structure.
WHEN abap_on.
"按照字段对应获取数据
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.
"按照顺序获取数据
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.结合前2天的随笔,一共给出3种excel导入的方法,掌握应该是第一种最容易掌握,当然第一种缺点也最大。
比较一下三种方法在效率上的区别:导入100条数据测试
1.ALSM_EXCEL_TO_INTERNAL方法:用时7秒
2.客制OLE-突破9999行的限制用时6秒(其实都是ole 和1 差不多也是正常)
3.CL_EHFND_XLSX类用时2秒
由以上可得,同学们可以多花心思研究下XML方式导入excel,以后就可以不用OLE,又快又舒服。
边栏推荐
- TIA博途_基于SCL语言制作模拟量输入输出全局库的具体方法
- Market status and development prospect forecast of global 3-Chloro-1,2-Propanediol industry in 2022
- Win10 LTSC 2021 wsappx CPU usage high
- The data synchronization tool dataX has officially supported reading and writing tdengine
- Current market situation and development prospect forecast of global 3,3 ', 4,4' - biphenyltetracarboxylic dianhydride industry in 2022
- SQL update batch update
- 为什么要从 OpenTSDB 迁移到 TDengine
- How to turn off the server terminal name of vscode
- MySQL读取Binlog日志常见错误和解决方法
- Informatics Olympiad 1333: [example 2-2] blah data set | openjudge noi 3.4 2729:blah data set
猜你喜欢
![[notice of the Association] notice on holding summer special teacher training in the field of artificial intelligence and Internet of things](/img/ef/5b86170e60a7e03c4db512445540b9.jpg)
[notice of the Association] notice on holding summer special teacher training in the field of artificial intelligence and Internet of things

Cloud native database: the outlet of the database, you can also take off

PostgreSQL database Wal - resource manager rmgr

电脑安全证书错误怎么处理比较好

【网络研讨会】MongoDB 携手 Google Cloud 加速企业数字化创新

Hi,你有一份Code Review攻略待查收!

Open source summer 2022 | opengauss project selected and announced

SQL update批量更新

脉脉热帖:为啥大厂都热衷于造轮子?

laravel框架中 定时任务的实现
随机推荐
Market status and development prospect forecast of global off-road recovery rope industry in 2022
Three methods to quickly open CMD in a specified folder or place
Common errors and solutions of MySQL reading binlog logs
PostgreSQL database Wal - resource manager rmgr
Win10 LTSC 2021 wsappx CPU 占用高
技术分享 | kubernetes pod 简介
Wechat applet association search
深度学习和神经网络的介绍
TIA博途_基于SCL语言制作模拟量输入输出全局库的具体方法
Market status and development prospect forecast of global aircraft hose industry in 2022
银河麒麟V10系统激活
喜讯丨英方软件2022获得10项发明专利!
Don't worry. This is the truth about wages in all industries in China
Google Earth Engine(GEE)——ImageCollection (Error)遍历影像集合产生的错误
VS code 运行yarn run dev 报yarn : 无法加载文件XXX的问题
Using WebDAV instead of 445 port file share
Two methods of MySQL database login and logout
利用OpenCV执行相机校准
Hi,你有一份Code Review攻略待查收!
【网络研讨会】MongoDB 携手 Google Cloud 加速企业数字化创新