引用共同学习;
经常遇到将外部数据大批量的导出sap中。今天的需求是:批量导入BOM分配,然后批量删除。
将execl导入sap内表的函数:ALSM_EXCEL_TO_INTERNAL_TABLE,很好用,不过听说这个函数只能导入1万行以内的数据,具体没有验证。
Step1:声明变量:
- TYPES: BEGIN OF ITAB1,
- MATNR LIKE MARA-MATNR,
- WERKS LIKE MARC-WERKS,
- END OF ITAB1.
- DATA:ITAB TYPE STANDARD TABLE OF ITAB1 WITH HEADER LINE.
- DATA:WA_UPLOAD TYPE ITAB1.
- DATA:IT_DATA1 TYPE STANDARD TABLE OF ALSMEX_TABLINE WITH HEADER LINE.
- DATA:FLG_WARNING LIKE CAPIFLAG-FLWARNING.
- FIELD-SYMBOLS: <WA_ITAB> TYPE ALSMEX_TABLINE.
Step2:画屏:
- SELECTION-SCREEN BEGIN OF BLOCK BLK1 WITH FRAME TITLE TEXT-001.
- PARAMETERS: FNAME1 TYPE RLGRAP-FILENAME OBLIGATORY.
- SELECTION-SCREEN END OF BLOCK BLK1.
Step3:控制级语句:
- INITIALIZATION.
- AT SELECTION-SCREEN ON VALUE-REQUEST FOR FNAME1." SEACH HELP
- * CALL FUNCTION 'F4_FILENAME'
- * EXPORTING
- * FIELD_NAME = 'FNAME1'
- * IMPORTING
- * FILE_NAME = FNAME1.
- CALL FUNCTION 'WS_FILENAME_GET'
- EXPORTING
- MASK = ',EXCEL FILE,*.XLS;*.XLSX;'
- MODE = 'O' "S为保存,O为打开
- IMPORTING
- FILENAME = FNAME1
- EXCEPTIONS
- INV_WINSYS = 1
- NO_BATCH = 2
- SELECTION_CANCEL = 3
- SELECTION_ERROR = 4
- OTHERS = 5.
- START-OF-SELECTION.
- PERFORM INTERNAL_TABLE.
- END-OF-SELECTION.
Step 4:获取数据,转化到内表
- FORM INTERNAL_TABLE.
- CALL FUNCTION 'ALSM_EXCEL_TO_INTERNAL_TABLE'
- EXPORTING
- FILENAME = FNAME1
- I_BEGIN_COL = '1'
- I_BEGIN_ROW = '2'
- I_END_COL = '2'
- I_END_ROW = '65535'
- TABLES
- INTERN = IT_DATA1
- EXCEPTIONS
- INCONSISTENT_PARAMETERS = 1
- UPLOAD_OLE = 2
- OTHERS = 3
- .
- IF IT_DATA1[] IS INITIAL.
- MESSAGE '清单中无数据' TYPE 'E'.
- ENDIF.
- SORT IT_DATA1 BY ROW.
- *格式转化
- LOOP AT IT_DATA1 ASSIGNING <WA_ITAB>.
- CASE <WA_ITAB>-COL.
- WHEN '0001'.
- WA_UPLOAD-WERKS = <WA_ITAB>-VALUE.
- WHEN '0002'.
- WA_UPLOAD-MATNR = <WA_ITAB>-VALUE.
- ENDCASE.
- AT END OF ROW.
- APPEND WA_UPLOAD TO ITAB.
- CLEAR WA_UPLOAD.
- ENDAT.
- ENDLOOP.
- ENDFORM.
Step5. 批量删除BOM分配
- FORM BOM_DELETE.
- LOOP AT ITAB.
- CALL FUNCTION 'CSAP_MAT_BOM_ALLOC_DELETE'
- EXPORTING
- MATERIAL = ITAB-MATNR
- PLANT = ITAB-WERKS
- BOM_USAGE = '1'
- * ALTERNATIVE =
- * FL_NO_CHANGE_DOC = ' '
- * FL_COMMIT_AND_WAIT = ' '
- IMPORTING
- FL_WARNING = FLG_WARNING
- BOM_NO = BOM_NO
- * TABLES
- * T_PLANT =
- EXCEPTIONS
- ERROR = 1
- OTHERS = 2
- .
- IF FLG_WARNING = 'X'.
- WRITE :/ ITAB-WERKS,ITAB-MATNR , '删除成功'.
- ELSE.
- WRITE :/ ITAB-WERKS,ITAB-MATNR , '删除失败'.
- ENDIF.
- ENDLOOP.
- ENDFORM.
我之前写的一篇日志:ABAP 将EXECL数据导入SAP内表的几个步骤
用的函数“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
之前尝试很多次使用gui_upload去上传excel数据,每次都出现乱码,不管中文英文都乱码。 至今不知道gui_upload是否支持excel文件上传,它的每个参数都尝试了,修改参数貌似并不能解决它出现乱码的事实。
于是想到用ALSM_EXCEL_TO_INTERNAL_TABLE,这个函数用起来有点蛋疼,不能使用自定义的格式,必须要定义一个和它规定的格式的内表来接收传来的值,完了要loop这个内表,再一条一条sign的自定义格式的内表中去。
后来选择用:TEXT_CONVERT_XLS_TO_SAP,这个函数用起来挺方便,没有乱码现象,还可以去掉首行,最重要的是能返回自己需要的格式的内表。
以下是实例:
TYPE-POOLS truxs. DATA:it_raw TYPE truxs_t_text_data.
*定义一个内表来存储数据,内表的列数和要传得数据的列数要相同,其按照列来匹配传值 DATA: BEGIN OF gt_data OCCURS 0, col1 TYPE char10, col2 TYPE char10, END OF gt_data.
*生成一个parameters元素,给其加上on value-request事件(在parameters上点击F4时触发)
PARAMETERS:p_file TYPE rlgrap-filename.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
CLEAR p_file.
CALL FUNCTION 'F4_FILENAME' IMPORTING file_name = p_file."返回文件名
*Excel传值 START-OF-SELECTION. PERFORM exceltotab. PERFORM display_data.
*&---------------------------------------------------------------------* *& Form exceltotab *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* FORM exceltotab .
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 = gt_data[] "ACTUAL DATA 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.
ENDFORM. "exceltotab
R/3 4.6C系统,CALL FUNCTION ‘TEXT_CONVERT_XLS_TO_SAP’,把EXCEL数据读取到内表时,遇到以下问题:
1.EXCEL中第一行是标题,调用FM时,参数I_LINE_HEADER=’X’
读第一条数据时,会把标题放入“值是空的字段”
2.模板中前两行是标题,调用FM时,参数I_LINE_HEADER=”,然后再把前两行数据删除
100*N+1行,有把标题放入“值是空的字段”的情况
以上问题在SAP ECC 6.0中没有发现
解决方法:
改用FUNCTION ‘ALSM_EXCEL_TO_INTERNAL_TABLE’