参考:https://blog.csdn.net/sunnyonly1992/article/details/51538522原文导入的excel数据,只有最后一条记录,也就是说key认为是MANDT,否则不会只导入1条记录问题。table001表结构:跟踪打印出来的数据:SPAN {font-family: "Courier New";font-size: 10pt;color: #000000;background: #FFFFFF;}.L0S31 {font-style: italic;color: #808080;}.L0S32 {color: #3399FF;}.L0S33 {color: #4DA619;}.L0S52 {color: #0000FF;}.L0S55 {color: #800080;}.L0S70 {color: #808080;}*&---------------------------------------------------------------------**& Report ZUPLOAD_TEST2*&*&---------------------------------------------------------------------**&*&*&---------------------------------------------------------------------*REPORT ZUPLOAD_TEST2.TABLES: ZTABLE001."假设我们要上传的内容有两个字段:序号,内容。然后定义类型,包含两个组件即序号和内容"TYPES:BEGIN OF TY_ZT, IDX TYPE I, "INDEX" *增加了此列,否则 与W_TABLE输出到不对应 NUM TYPE ZTABLE001-NUM,"序号" TXT TYPE ZTABLE001-TXT,"内容" END OF TY_ZT."定义参照TY_ZT类型的内表和工作区,用于暂存取到的Excel内容"DATA:I_TABLE TYPE TABLE OF TY_ZT, W_TABLE TYPE TY_ZT."定义内表,用于insert或modify透明表内容"DATA:GT_TABLE TYPE TABLE OF ZTABLE001,"直接参照透明表" GW_TABLE TYPE ZTABLE001."选择屏幕"SELECTION-SCREEN BEGIN OF BLOCK B1 WITH FRAME TITLE TEXT-001. SELECTION-SCREEN BEGIN OF LINE. SELECTION-SCREEN COMMENT 1(9) STR1."维护" PARAMETERS P_1 RADIOBUTTON GROUP G1 DEFAULT 'X'."单选" SELECTION-SCREEN END OF LINE. SELECTION-SCREEN BEGIN OF LINE. SELECTION-SCREEN COMMENT 1(9) STR2 ."导入" PARAMETERS P_2 RADIOBUTTON GROUP G1."单选" SELECTION-SCREEN COMMENT 16(2) STR3. PARAMETERS P_FILE(80)."地址栏" SELECTION-SCREEN END OF LINE.SELECTION-SCREEN END OF BLOCK B1.INITIALIZATION.STR1 = '维护'.STR2 = '导入'."导入文件地址栏就是一个parameter ,这里通过一个radiobutton单选按钮,选择 维护或导入两种功能。选择屏幕写好之后,接下来要用到的就只有 P_FILE 这个参数已经调用相应的函数。"AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_FILE. PERFORM OPEN_DIALOG."调用选择文件函数"AT SELECTION-SCREEN."用于屏幕输入检验"IF P_2 = 'X'. IF P_FILE IS INITIAL. MESSAGE '文件地址不可为空!' TYPE 'S' DISPLAY LIKE 'E'. STOP. ENDIF.ENDIF.START-OF-SELECTION.IF P_1 = 'X'. PERFORM FRM_CALL_VIEW.ELSEIF P_2 = 'X'. PERFORM UPLOAD.ENDIF.END-OF-SELECTION.FORM FRM_CALL_VIEW."维护视图"DATA:P_TABNAM LIKE DD02V-TABNAME."Char(30)"P_TABNAM = 'ZTABLE001'.CALL FUNCTION 'VIEW_MAINTENANCE_CALL' "调用维护视图" EXPORTING ACTION = 'S' VIEW_NAME = P_TABNAM "表名" EXCEPTIONS CLIENT_REFERENCE = 1 FOREIGN_LOCK = 2 INVALID_ACTION = 3 NO_CLIENTINDEPENDENT_AUTH = 4 NO_DATABASE_FUNCTION = 5 NO_EDITOR_FUNCTION = 6 NO_SHOW_AUTH = 7 NO_TVDIR_ENTRY = 8 NO_UPD_AUTH = 9 ONLY_SHOW_ALLOWED = 10 SYSTEM_FAILURE = 11 UNKNOWN_FIELD_IN_DBA_SELLIST = 12 VIEW_NOT_FOUND = 13 MAINTENANCE_PROHIBITED = 14 OTHERS = 15 .IF SY-SUBRC 0. MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.ENDIF.ENDFORM.FORM OPEN_DIALOG."选择文件会话。"DATA:LT_FILE_TABLE TYPE FILETABLE.DATA:LW_FILE_TABLE TYPE FILE_TABLE.DATA:L_RC TYPE I. CALL METHOD CL_GUI_FRONTEND_SERVICES=>FILE_OPEN_DIALOG EXPORTING WINDOW_TITLE = '选择文件' DEFAULT_FILENAME = '*.XLSX' "默认excel文件" INITIAL_DIRECTORY = 'D:\ ' "默认打开D盘,也可以默认空" MULTISELECTION = '' "文件单选" CHANGING FILE_TABLE = LT_FILE_TABLE RC = L_RC EXCEPTIONS FILE_OPEN_DIALOG_FAILED = 1 CNTL_ERROR = 2 ERROR_NO_GUI = 3 NOT_SUPPORTED_BY_GUI = 4 others = 5 .CHECK L_RC EQ 1."因为文件单选,所以这里判断一下选择的数量为1"READ TABLE LT_FILE_TABLE INDEX 1 INTO P_FILE."将选择的文件地址写入到地址栏"ENDFORM.FORM UPLOAD."最后也是最关键的一步""定义表格结构内表。"DATA: I_EXCEL TYPE TABLE OF ALSMEX_TABLINE,"表格结构。" W_EXCEL TYPE ALSMEX_TABLINE."ALSMEX_TABLINE是具有Excel数据的表行,有三个组件,row col value 。"DATA:GET_FILE TYPE RLGRAP-FILENAME."这里参照系统中的结构字段。"MOVE P_FILE TO GET_FILE."将地址栏的值赋值给GET_FILE""调用此函数,将Excel中的内容以类似坐标的形式存储到I_excel内表中。" CALL FUNCTION 'ALSM_EXCEL_TO_INTERNAL_TABLE' EXPORTING FILENAME = GET_FILE I_BEGIN_COL = 1 I_BEGIN_ROW = 1 I_END_COL = 8 I_END_ROW = 65535 TABLES INTERN = I_EXCEL EXCEPTIONS INCONSISTENT_PARAMETERS = 1 UPLOAD_OLE = 2 OTHERS = 3 .*DELETE I_EXCEL WHERE ROW = 1."删除第一行抬头。如果excel文件中不存在抬头的话,可不写此句。"IF I_EXCEL IS INITIAL. CALL FUNCTION 'POPUP_TO_CONFIRM_WITH_MESSAGE' EXPORTING* DEFAULTOPTION = 'Y' DIAGNOSETEXT1 = '数据错误'* DIAGNOSETEXT2 = ' '* DIAGNOSETEXT3 = ' ' TEXTLINE1 = 'Excel表格中没有内容!' TITEL = '提示' . RETURN.ENDIF.CLEAR:I_TABLE.FIELD-SYMBOLS:."这里涉及到SAP内部字段的使用,详情请参见微博相关介绍。"SORT I_EXCEL BY ROW COL.DATA:NUM_COL TYPE I.LOOP AT I_EXCEL INTO W_EXCEL. NUM_COL = W_EXCEL-COL. ASSIGN COMPONENT NUM_COL OF STRUCTURE W_TABLE TO . = W_EXCEL-VALUE. write && '; '. AT END OF ROW. APPEND W_TABLE TO I_TABLE. CLEAR:W_TABLE. write / ''. ENDAT. CLEAR:W_EXCEL.ENDLOOP.* 在此更改了MANDT的值,不清楚为什么不生效(table001表中的MANDT类型由CLNT改为INT2则有效)DATA INDEX TYPE I VALUE 100.LOOP AT I_TABLE INTO W_TABLE. INDEX = INDEX + 1. GW_TABLE-MANDT = INDEX."客户端号,创建透明表必须包含的字段。" GW_TABLE-NUM = W_TABLE-NUM. GW_TABLE-TXT = W_TABLE-TXT. write / GW_TABLE-MANDT && '; ' && GW_TABLE-NUM && '; ' && GW_TABLE-TXT.APPEND GW_TABLE TO GT_TABLE.CLEAR:GW_TABLE.CLEAR:W_TABLE.ENDLOOP."将数据库中内容更新"MODIFY ZTABLE001 FROM TABLE GT_TABLE."这里使用modify,兼顾增加和修改,若主键存在则为修改,否则增加"* INSERT ZTABLE001 FROM TABLE GT_TABLE.IF SY-SUBRC = 0. COMMIT WORK AND WAIT."成功提交,或者最后程序结束系统也会自动提交" DATA:NUM_TAB(6) TYPE C. DESCRIBE TABLE GT_TABLE LINES NUM_TAB. CONDENSE NUM_TAB NO-GAPS. DATA:STR_LINE1 TYPE STRING . CONCATENATE '成功导入数据' NUM_TAB '行' INTO STR_LINE1 ."读出excel中的数据条数,用于消息提示" CALL FUNCTION 'POPUP_TO_CONFIRM_WITH_MESSAGE' EXPORTING DIAGNOSETEXT1 = '成功' TEXTLINE1 = STR_LINE1 TITEL = '提示' . ELSE. ROLLBACK WORK."否则失败" ENDIF.ENDFORM.abap导入数据有些地方与个人想的还不一样,也许是不熟悉abap吧。 09-03 22:59