方法可以將excel的數(shù)據(jù)導入至內表,但要求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. "到新行將數(shù)據(jù)插入gt_tab
APPEND gs_tab to gt_tab .
CLEAR gs_tab .
ENDAT.
endloop.
LOOP一列一列的賦值可以归敬,但不適用字段超多的上載,字段多用如下代碼:
IF lt_excel[] IS INITIAL.
MESSAGE E361(ZFIN01)." '清單中無數(shù)據(jù)'.
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.