我之前写的一篇日志:ABAP 将EXECL数据导入SAP内表的几个步骤
http://blog.csdn.net/szlaptop/article/details/8663451
用的函数“ALSM_EXCEL_TO_INTERNAL_TABLE”,当时觉得它非常好用,但是今天要推翻这个结论了,因为发现了一个更好用的函数:
“TEXT_CONVERT_XLS_TO_SAP”。这个函数直接可以把execl的内容原原本本的写入到内表,不用格式转化那么麻烦。
如果该内表ITAB的数据最后要写入你的自建表里,那么还得迂回一下,因为透明表里有个MANDT客户端字段。所以得再建一个内表来迂回。
LOOP AT itab.
CLEAR itab1.
MOVE-CORRESPONDING itab to itab1.
APPEND itab1.
ENDLOOP.
MODIFY zchannel FROM TABLE itab1.
CALL FUNCTION 'TEXT_CONVERT_XLS_TO_SAP'
EXPORTING
* I_FIELD_SEPERATOR =
I_LINE_HEADER = 'X'
i_tab_raw_data = IT_RAW
i_filename = fname1
tables
i_tab_converted_data = itab
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.
通过下载execl模板给用户,用户按照模板填写再将数据导入SAP。
INCLUDE ZOLE2INCL.
TYPE-POOLS: TRUXS.
DATA:BEGIN OF itab OCCURS 0,
area LIKE zchannel-area,
city LIKE zchannel-city,
zsystem LIKE zchannel-zsystem,
storename LIKE zchannel-storename,
competent LIKE zchannel-competent,
promoters LIKE zchannel-promoters,
matnr LIKE zchannel-matnr,
salesqty LIKE zchannel-salesqty,
zunit LIKE zchannel-zunit,
salesamount LIKE zchannel-salesamount,
zdate LIKE zchannel-zdate,
END OF itab.
data:itab1 LIKE zchannel OCCURS 0 WITH HEADER LINE.
DATA: IT_RAW TYPE TRUXS_T_TEXT_DATA.
DATA: H_EXCEL TYPE OLE2_OBJECT, " Excel object
H_MAPL TYPE OLE2_OBJECT, " list of workbooks
H_MAP TYPE OLE2_OBJECT, " workbook
H_ZL TYPE OLE2_OBJECT, " cell
H_F TYPE OLE2_OBJECT. " font
SELECTION-SCREEN BEGIN OF BLOCK blk1 WITH FRAME TITLE text-001.
PARAMETERS: fname1 TYPE rlgrap-filename OBLIGATORY.
SELECTION-SCREEN END OF BLOCK blk1.
SELECTION-SCREEN BEGIN OF BLOCK BLK2 WITH FRAME TITLE TEXT-002.
SELECTION-SCREEN: BEGIN OF LINE,
PUSHBUTTON 2(10) PUSH USER-COMMAND DOWN.
SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN END OF BLOCK BLK2.
INITIALIZATION.
PUSH = '下载上传模板'.
AT SELECTION-SCREEN.
IF SY-UCOMM = 'DOWN'.
PERFORM DISPLAYEXCEL.
ENDIF.
FORM DISPLAYEXCEL .
* tell user what is going on
CALL FUNCTION 'SAPGUI_PROGRESS_INDICATOR'
EXPORTING
* PERCENTAGE = 0
TEXT = TEXT-007
EXCEPTIONS
OTHERS = 1.
* start Excel
CREATE OBJECT H_EXCEL 'EXCEL.APPLICATION'.
* PERFORM ERR_HDL.
SET PROPERTY OF H_EXCEL 'Visible' = 1.
* CALL METHOD OF H_EXCEL 'FILESAVEAS' EXPORTING #1 = 'c:\kis_excel.xls'
.
* PERFORM ERR_HDL.
* tell user what is going on
CALL FUNCTION 'SAPGUI_PROGRESS_INDICATOR'
EXPORTING
* PERCENTAGE = 0
TEXT = TEXT-008
EXCEPTIONS
OTHERS = 1.
* get list of workbooks, initially empty
CALL METHOD OF H_EXCEL 'Workbooks' = H_MAPL.
PERFORM ERR_HDL.
* add a new workbook
CALL METHOD OF H_MAPL 'Add' = H_MAP.
PERFORM ERR_HDL.
* tell user what is going on
CALL FUNCTION 'SAPGUI_PROGRESS_INDICATOR'
EXPORTING
* PERCENTAGE = 0
TEXT = TEXT-009
EXCEPTIONS
OTHERS = 1.
* output column headings to active Excel sheet
PERFORM FILL_CELL USING 1 1 1 '区域名称'(001).
PERFORM FILL_CELL USING 1 2 1 '城市'(002).
PERFORM FILL_CELL USING 1 3 1 '所在系统'(004).
PERFORM FILL_CELL USING 1 4 1 '门店名称'(005).
PERFORM FILL_CELL USING 1 5 1 销售代表'(006).
PERFORM FILL_CELL USING 1 6 1 '促销员'(007).
PERFORM FILL_CELL USING 1 7 1 '物料号'(008).
PERFORM FILL_CELL USING 1 8 1 '数量'(009).
PERFORM FILL_CELL USING 1 9 1 '单位'(009).
PERFORM FILL_CELL USING 1 10 1 '金额'(010).
PERFORM FILL_CELL USING 1 11 1 '日期'(011).
PERFORM FILL_CELL USING 2 1 0 '北京'(001).
PERFORM FILL_CELL USING 2 2 0 '北京'(002).
PERFORM FILL_CELL USING 2 3 0 '天系统'(003).
PERFORM FILL_CELL USING 2 4 0 '奥体天'(004).
PERFORM FILL_CELL USING 2 5 0 '白小夏'(005).
PERFORM FILL_CELL USING 2 6 0 '夏小白'(006).
PERFORM FILL_CELL USING 2 7 0 'A00-000001'(007).
PERFORM FILL_CELL USING 2 8 0 '3'(008).
PERFORM FILL_CELL USING 2 9 0 '包'(009).
PERFORM FILL_CELL USING 2 10 0 '38.8'(010).
PERFORM FILL_CELL USING 2 11 0 '20130420'(011).
ENDFORM. "displayexcel
FORM ERR_HDL.
IF SY-SUBRC <> 0.
WRITE: / 'Fehler bei OLE-Automation:'(010), SY-SUBRC.
STOP.
ENDIF.
ENDFORM.
FORM FILL_CELL USING I J BOLD VAL.
CALL METHOD OF H_EXCEL 'Cells' = H_ZL EXPORTING #1 = I #2 = J.
PERFORM ERR_HDL.
SET PROPERTY OF H_ZL 'Value' = VAL .
PERFORM ERR_HDL.
GET PROPERTY OF H_ZL 'Font' = H_F.
PERFORM ERR_HDL.
SET PROPERTY OF H_F 'Bold' = BOLD .
PERFORM ERR_HDL.
ENDFORM. "FILL_CELL