很多项目都有这种需求,虽然别人用的各有不同,不过闲来无事,还是自己搞了一个出来。基于EXCEL的导入。
*&---------------------------------------------------------------------*
*& Report ZLY_UPLOAD_TABLE
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------* REPORT ZUPLOAD_TABLE.
TABLES:RSRD1.
TYPE-POOLS:ABAP. FIELD-SYMBOLS: <DYN_TABLE> TYPE STANDARD TABLE,
<DYN_WA>,
<DYN_FIELD>.
DATA: DY_TABLE TYPE REF TO DATA,
DY_LINE TYPE REF TO DATA,
XFC TYPE LVC_S_FCAT,
IFC TYPE LVC_T_FCAT. DATA : GT_EXCEL_T TYPE ALSMEX_TABLINE OCCURS WITH HEADER LINE,
GS_EXCEL_T LIKE LINE OF GT_EXCEL_T.
DATA: GC_FIELDVALUE TYPE FIELDVALUE, "字符值
GC_FLDNAME TYPE FIELDNAME. "字段名 SELECTION-SCREEN:BEGIN OF BLOCK BLK01 WITH FRAME TITLE TEXT-.
PARAMETERS:P_TBMA TYPE RSRD1-TBMA_VAL,
P_FILE LIKE RLGRAP-FILENAME.
SELECTION-SCREEN END OF BLOCK BLK01. AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_FILE.
PERFORM FRM_GET_FILEPATH. START-OF-SELECTION.
CHECK P_TBMA IS NOT INITIAL.
CHECK P_FILE IS NOT INITIAL.
PERFORM PRM_GET_TAB_STRC.
PERFORM PRM_GET_EXCEL.
PERFORM PRM_UPDATA_TAB.
*&---------------------------------------------------------------------*
*& Form FRM_GET_FILEPATH
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM FRM_GET_FILEPATH .
CALL FUNCTION 'WS_FILENAME_GET'
EXPORTING
MASK = ',Excel(*.xls),*.XLS,*.XLSX,'
TITLE = '选择文件'()
IMPORTING
FILENAME = P_FILE
EXCEPTIONS
INV_WINSYS =
NO_BATCH =
SELECTION_CANCEL =
SELECTION_ERROR =
OTHERS = .
IF SY-SUBRC <> .
" MESSAGE e100(zdev) WITH '选择文件出错!'(007).
ENDIF.
ENDFORM. " FRM_GET_FILEPATH
*&---------------------------------------------------------------------*
*& Form PRM_GET_TAB_STRC
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM PRM_GET_TAB_STRC .
DATA:GT_TABLE TYPE DDFIELDS,
GS_TABLE TYPE DFIES. CALL FUNCTION 'CATSXT_GET_DDIC_FIELDINFO'
EXPORTING
IM_STRUCTURE_NAME = P_TBMA
IMPORTING
EX_DDIC_INFO = GT_TABLE
EXCEPTIONS
FAILED =
OTHERS = .
IF SY-SUBRC <> .
ENDIF. LOOP AT GT_TABLE INTO GS_TABLE.
CLEAR XFC.
XFC-FIELDNAME = GS_TABLE-FIELDNAME .
XFC-INTTYPE = GS_TABLE-INTTYPE.
XFC-INTLEN = GS_TABLE-LENG.
XFC-DECIMALS = GS_TABLE-DECIMALS.
APPEND XFC TO IFC.
ENDLOOP. CALL METHOD CL_ALV_TABLE_CREATE=>CREATE_DYNAMIC_TABLE
EXPORTING
IT_FIELDCATALOG = IFC
IMPORTING
EP_TABLE = DY_TABLE.
ASSIGN DY_TABLE->* TO <DYN_TABLE>. CREATE DATA DY_LINE LIKE LINE OF <DYN_TABLE>.
ASSIGN DY_LINE->* TO <DYN_WA>.
ENDFORM. " PRM_GET_TAB_STRC
*&---------------------------------------------------------------------*
*& Form PRM_GET_EXCEL
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM PRM_GET_EXCEL .
IF P_FILE IS NOT INITIAL.
*将excel数据传入内表
CALL FUNCTION 'ALSM_EXCEL_TO_INTERNAL_TABLE'
EXPORTING
FILENAME = P_FILE
I_BEGIN_COL = ''
I_BEGIN_ROW = ''
I_END_COL = ''
I_END_ROW = ''
TABLES
INTERN = GT_EXCEL_T.
ENDIF. IF GT_EXCEL_T[] IS NOT INITIAL.
LOOP AT GT_EXCEL_T INTO GS_EXCEL_T.
AT NEW ROW.
CLEAR <DYN_WA>.
ENDAT.
IF <DYN_WA> IS INITIAL.
ASSIGN COMPONENT GS_EXCEL_T-COL OF STRUCTURE <DYN_WA> TO <DYN_FIELD>.
IF SY-SUBRC = .
<DYN_FIELD> = SY-MANDT.
ENDIF.
ENDIF.
GS_EXCEL_T-COL = GS_EXCEL_T-COL + .
GC_FIELDVALUE = GS_EXCEL_T-VALUE.
CONDENSE GC_FIELDVALUE.
ASSIGN COMPONENT GS_EXCEL_T-COL OF STRUCTURE <DYN_WA> TO <DYN_FIELD>.
IF SY-SUBRC = .
<DYN_FIELD> = GC_FIELDVALUE.
ENDIF.
AT END OF ROW.
APPEND <DYN_WA> TO <DYN_TABLE>.
ENDAT.
ENDLOOP.
ENDIF.
ENDFORM. " PRM_GET_EXCEL
*&---------------------------------------------------------------------*
*& Form PRM_UPDATA_TAB
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM PRM_UPDATA_TAB .
MODIFY (P_TBMA) FROM TABLE <DYN_TABLE>.
IF SY-SUBRC = .
MESSAGE '导入成功' TYPE 'S'.
ELSE.
MESSAGE '导入失败' TYPE 'S' DISPLAY LIKE 'E'.
EXIT.
ENDIF.
ENDFORM. " PRM_UPDATA_TAB
最简单的一种方式,输入参数:表名,EXXCEL(EXCEL里数据按表的数据元素顺序,可不加客户端)