该程序适合各个模块倒数据,EXCEL模版第一行是抬头文本,第一列必须是客户端的值,代码如下:
*&———————————————————————*
*& Report ZJO_IMPORT
*&
*&———————————————————————*
*&
*&
*&———————————————————————*
REPORT ZJO_IMPORT.
TYPE-POOLS:TRUXS,SLIS.
INCLUDE OLE2INCL.
*———————————————————————-*
* TYPES DIFINITION
* 类型定义
*———————————————————————-*
TYPES: BEGIN OF T_EXCEL,
J_3APGNR TYPE J_3APGNR,
J_3AKORDX TYPE J_3AKORD,
EX_SIZE TYPE ZSIZE_EXTERNAL,
END OF T_EXCEL.
*———————————————————————-*
* INTERNAL TABLE DEFINITION
* 内表定义
*———————————————————————-*
DATA : IT_EXCEL TYPE TABLE OF T_EXCEL,
IT_ALV TYPE TABLE OF T_EXCEL,
IT_BDC LIKE TABLE OF BDCDATA WITH HEADER LINE,
IT_MESS LIKE TABLE OF BDCMSGCOLL WITH HEADER LINE,
IT_FCAT TYPE TABLE OF LVC_S_FCAT.
*———————————————————————-*
* WORK AREA DEFINITION
* 工作区定义
*———————————————————————-*
DATA: WA_EXCEL TYPE T_EXCEL,
WA_ALV TYPE T_EXCEL,
WA_LAYOUT TYPE LVC_S_LAYO,
WA_FCAT LIKE LINE OF IT_FCAT,
G_EXCEL TYPE OLE2_OBJECT,
G_SHEET TYPE OLE2_OBJECT,
G_CELL TYPE OLE2_OBJECT,
G_WKBK TYPE OLE2_OBJECT.
*———————————————————————-*
* DATA DEFINITION
* 数据定义
*———————————————————————-*
DATA: IT_RAW TYPE TRUXS_T_TEXT_DATA.
*———————- ALV显示要用到的几个全局变量 —————-*
DATA:
G_FIELDCAT_ALV TYPE SLIS_T_FIELDCAT_ALV WITH HEADER LINE,
G_LAYOUT TYPE SLIS_LAYOUT_ALV,
G_REPID LIKE SY-REPID VALUE SY-REPID.
DATA: DYN_TABLE TYPE REF TO DATA.
DATA: DYN_WA TYPE REF TO DATA.
FIELD-SYMBOLS:
*———————————————————————-*
* 选择屏幕
*———————————————————————-*
SELECTION-SCREEN BEGIN OF BLOCK BLK1 WITH FRAME TITLE TEXT-T01.
PARAMETERS:P_FILE LIKE RLGRAP-FILENAME OBLIGATORY.
PARAMETERS P_NAME TYPE TABNAME OBLIGATORY.
SELECTION-SCREEN END OF BLOCK BLK1.
*———————————————————————-*
* AT SELECTION-SCREEN ON VALUE-REQUEST
*———————————————————————-*
AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_FILE.
CALL FUNCTION ‘F4_FILENAME’
IMPORTING
FILE_NAME = P_FILE.
*———————————————————————-*
* START-OF-SELECTION
*———————————————————————-*
START-OF-SELECTION.
* 得到文件数据
*创建动态表结构
CREATE DATA DYN_TABLE TYPE TABLE OF (P_NAME).
*创建动态内表
ASSIGN DYN_TABLE->* TO
*创建动态工作区结构
CREATE DATA DYN_WA LIKE LINE OF
*创建动态工作区
ASSIGN DYN_WA->* TO
PERFORM FRM_DATA_GET.
PERFORM FRM_ALV_LAYOUT_BUILD.
PERFORM FRM_DISPLAY_DATA.
** 数据检查
* PERFORM FRM_DATA_CHECK.
** ALV展示
* PERFORM FRM_ALV_LAYOUT_BUILD.
* PERFORM FRM_ALV_SET_COLUMNS.
* PERFORM FRM_ALV_DISPLAY_DATA.
*&———————————————————————*
*& Form FRM_DATA_GET
*&———————————————————————*
* 得到文件数据
*———————————————————————-*
FORM FRM_DATA_GET .
CALL FUNCTION ‘TEXT_CONVERT_XLS_TO_SAP’
EXPORTING
* I_FIELD_SEPERATOR =
I_LINE_HEADER = ‘X’
I_TAB_RAW_DATA = IT_RAW ” WORK TABLE
I_FILENAME = P_FILE
TABLES
I_TAB_CONVERTED_DATA =
EXCEPTIONS
CONVERSION_FAILED = 1
OTHERS = 2.
ENDFORM. ” FRM_DATA_GET
*———————————————
*FORM FRM_GETDAT_EXCEL
*———————————————
FORM FRM_GETDAT_EXCEL .
FIELD-SYMBOLS:
DATA: L_I_XLSTMP TYPE ALSMEX_TABLINE OCCURS 0 WITH HEADER LINE.
DATA: L_COL TYPE I.
CLEAR L_I_XLSTMP.
REFRESH L_I_XLSTMP.
CALL FUNCTION ‘ALSM_EXCEL_TO_INTERNAL_TABLE’
EXPORTING
FILENAME = P_FILE
I_BEGIN_COL = 1
I_BEGIN_ROW = 2
I_END_COL = 11
I_END_ROW = 1000
TABLES
INTERN = L_I_XLSTMP
EXCEPTIONS
INCONSISTENT_PARAMETERS = 1
UPLOAD_OLE = 2
OTHERS = 3.
IF SY-SUBRC <> 0 .
MESSAGE ‘导入文件失败。’ TYPE ‘E’.
ENDIF.
*-转换内表
* REFRESH: IT_TAB.
* CLEAR: IT_TAB.
* SORT L_I_XLSTMP BY ROW COL.
* LOOP AT L_I_XLSTMP.
* L_COL = L_I_XLSTMP-COL.
* ASSIGN COMPONENT L_COL OF STRUCTURE WA_DATA TO
*
* AT END OF ROW.
* APPEND WA_DATA TO IT_TAB.
* CLEAR WA_DATA.
* ENDAT.
* ENDLOOP.
ENDFORM. ” FRM_GETDAT_EXCEL
*&———————————————————————*
*& Form FRM_DATA_CHECK
*&———————————————————————*
* 数据检查
*———————————————————————-*
FORM FRM_DATA_CHECK .
ENDFORM. ”frm_data_check
*&———————————————————————*
*& Form FRM_ALV_LAYOUT_BUILD
*&———————————————————————*
* ALV显示的设置,包括双击,标志是否显示0等
*———————————————————————-*
* –> p1 text
* <– p2 text
*———————————————————————-*
FORM FRM_ALV_LAYOUT_BUILD.
CLEAR G_LAYOUT.
G_LAYOUT-ZEBRA = ‘X’.
G_LAYOUT-DETAIL_POPUP = ‘X’.
* G_LAYOUT-F2CODE = ‘&ETA’.
G_LAYOUT-NO_VLINE = ‘X’.
G_LAYOUT-COLWIDTH_OPTIMIZE = ‘X’.
* G_LAYOUT-BOX_FIELDNAME = ‘FLAG’.
G_LAYOUT-DETAIL_INITIAL_LINES = ‘X’.
G_LAYOUT-DETAIL_TITLEBAR = TEXT-041.
ENDFORM. ” FRM_ALV_LAYOUT_BUILD
*&———————————————————————*
*& Form FRM_ALV_SET_COLUMNS
*&————————-
*——————————————–*
* 设置每一个ALV列的显示
*———————————————————————-*
* –> p1 text
* <– p2 text
*———————————————————————-*
FORM FRM_ALV_SET_COLUMNS .
REFRESH G_FIELDCAT_ALV.
PERFORM FRM_SET_L_FIELDCAT USING 1 ’XHNUM’ TEXT-010.
PERFORM FRM_SET_L_FIELDCAT USING 2 ’YHNUM’ TEXT-011.
ENDFORM. ” FRM_ALV_SET_COLUMNS
*&———————————————————————*
*& Form FRM_SET_L_FIELDCAT
*&———————————————————————*
* 将值赋给每一个列
*———————————————————————-*
* –>P_1 text
* –>P_0101 text
* –>P_TEXT_010 text
*———————————————————————-*
FORM FRM_SET_L_FIELDCAT USING L_POS NAME TEXTL.
DATA:L_FIELDCAT TYPE SLIS_FIELDCAT_ALV.
L_FIELDCAT-COL_POS = L_POS.
L_FIELDCAT-FIELDNAME = NAME.
L_FIELDCAT-SELTEXT_L = TEXTL.
APPEND L_FIELDCAT TO G_FIELDCAT_ALV.
ENDFORM. ” FRM_SET_L_FIELDCAT
*&———————————————————————*
*& Form FRM_STATUS_ALV
*&———————————————————————*
* text
*———————————————————————-*
* –>RT_EXTAB text
*———————————————————————-*
FORM FRM_STATUS_ALV USING RT_EXTAB TYPE SLIS_T_EXTAB.
SET PF-STATUS ‘STA1′.
ENDFORM. ”FRM_STATUS_ALV
” PF_SET_STATUS
*&———————————————————————*
*& Form FRM_USER_COMMAND_ALV
*&———————————————————————*
* text
*———————————————————————-*
* –>PR_UCOMM text
* –>PR_SELFIELD text
*———————————————————————-*
FORM FRM_USER_COMMAND_ALV USING PR_UCOMM TYPE SY-UCOMM PR_SELFIELD TYPE SLIS_SELFIELD.
CASE PR_UCOMM.
WHEN ‘&F03′ OR ‘&F15′ OR ‘&F12′.
LEAVE TO SCREEN 0.
WHEN ‘INSERT’.
PERFORM FRM_INSERT_TABLE.
ENDCASE.
ENDFORM. ” FRM_USER_COMMAND_ALV
*&———————————————————————*
*& Form FRM_INSERT_TABLE
*&———————————————————————*
* text
*———————————————————————-*
* –> p1 text
* <– p2 text
*———————————————————————-*
FORM FRM_INSERT_TABLE .
MODIFY (P_NAME) FROM TABLE
COMMIT WORK.
IF SY-SUBRC EQ 0.
MESSAGE ‘数据导入成功’ TYPE ‘S’.
ENDIF.
ENDFORM. ” FRM_INSERT_TABLE
*&———————————————————————*
*& Form FRM_DISPLAY_DATA
*&———————————————————————*
* text
*———————————————————————-*
* –> p1 text
* <– p2 text
*———————————————————————-*
FORM FRM_DISPLAY_DATA .
* 显示内表中的数据
CALL FUNCTION ‘REUSE_ALV_GRID_DISPLAY’
EXPORTING
I_STRUCTURE_NAME = P_NAME
I_CALLBACK_PROGRAM = G_REPID”传入返回的程序ID,一般是当前程序名
I_CALLBACK_PF_STATUS_SET = ‘FRM_STATUS_ALV’”创建ALV工具栏,对应的程序要创建一样的FORM
I_CALLBACK_USER_COMMAND = ‘FRM_USER_COMMAND_ALV’”响应用户的操作,扑捉功能码对应的程序要创建一样的FORM
IS_LAYOUT = G_LAYOUT “传入ALV显示格式控制数据
* IT_FIELDCAT = G_FIELDCAT_ALV[] “传入显示字段的内表
I_SAVE = ‘A’ ”设置为A表示可以保存ALV显示格式
TABLES
T_OUTTAB =
EXCEPTIONS
PROGRAM_ERROR = 1
OTHERS = 2.
ENDFORM. ” FRM_DISPLAY_DATA