方法可以将excel的数据导入至内表,但要求excel字段和内表字段一致。
- 方法一
DATA: lt_filename TYPE rlgrap-filename,
lt_data TYPE truxs_t_text_data.```
DATA:GT_DATA TYPE STANDARD TABLE OF ZSEXCEL WITH HEADER LINE .
FIELD-SYMBOLS: <FS>.
``` lt_filename = p_path. "路径
CALL FUNCTION 'TEXT_CONVERT_XLS_TO_SAP'
EXPORTING```
```I_LINE_HEADER = 'X'
I_TAB_RAW_DATA = lt_data
I_FILENAME = lt_filename
TABLES
I_TAB_CONVERTED_DATA = GT_VBRK
EXCEPTIONS
CONVERSION_FAILED = 1
OTHERS = 2
.```
``` IF SY-SUBRC <> 0.
** MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
** WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.```
* 方法二
``` DATA: lt_filename TYPE rlgrap-filename , "路径
lt_excel TYPE TABLE OF alsmex_tabline ,
ls_excel TYPE alsmex_tabline.```
```lt_filename = p_path. "路径```
```call function 'ALSM_EXCEL_TO_INTERNAL_TABLE'
exporting
filename = lt_filename
i_begin_col = 1
i_begin_row = 2
i_end_col = 128
i_end_row = 9999
tables
intern = lt_excel
exceptions
inconsistent_parameters = 1
upload_ole = 2
others = 3.```
```sort lt_excel by row col.```
loop at lt_excel into ls_excel.
case ls_excel-col.
when 1. "读取第一列
gs_tab-vbeln = ls_excel-value.```
* translate gs_vbak-vbeln to upper case.
when 2. "读取第二列
gs_tab-POSNR = ls_excel-value.
endcase .
at end of row. "到新行将数据插入gt_tab
APPEND gs_tab to gt_tab .
CLEAR gs_tab .
ENDAT.
endloop.
LOOP一列一列的赋值可以,但不适用字段超多的上载,字段多用如下代码:
IF lt_excel[] IS INITIAL.
MESSAGE E361(ZFIN01)." '清单中无数据'.
ENDIF.
LOOP AT lt_excel . "添加至内表
AT NEW ROW.
CLEAR GS_TAB.
ENDAT .
ASSIGN COMPONENT lt_excel-COL OF STRUCTURE GS_TAB TO <FS>."分配
<FS> = lt_excel-VALUE."赋值
AT END OF ROW.
APPEND GS_TAB TO GT_TAB.
ENDAT.
ENDLOOP.