ABAP读取Excel文件可以使用多种方式实现,最常见的就是使用标准函数进行读取,对于期初导入有很大帮助。常用的标准函数有2个,ALSM_EXCEL_TO_INTERNAL_TABL 和 TEXT_CONVERT_XLS_TO_SAP
文章采用的是TEXT_CONVERT_XLS_TO_SAP进行处理,代码如下:
data :begin of gs_itab , matnr type mara-matnr, mtart type mara-mtart, matkl type mara-matkl, MEINS type mara-MEINS, MENGE LIKE STPO-MENGE, VMVER TYPE MBEW-VMVER, end of gs_itab. data : gt_itab like TABLE OF gs_itab . PARAMETERS P_FILE LIKE rlgrap-filename OBLIGATORY. AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_FILE. PERFORM FRM_GET_FILE. START-OF-SELECTION. PERFORM frm_get_excel. FORM FRM_GET_FILE. DATA: lt_filetab TYPE filetable, l_rc TYPE i. CLEAR lt_filetab. REFRESH lt_filetab. CALL METHOD CL_GUI_FRONTEND_SERVICES=>FILE_OPEN_DIALOG EXPORTING window_title = '打开.xls文件' "弹出对话框的抬头 " default_extension = '123.xlsx'"默认的扩展名 " default_filename = '123' "默认的文件名 file_filter = CL_GUI_FRONTEND_SERVICES=>FILETYPE_EXCEL "只显示某中文件类型 * with_encoding = initial_directory = 'd:\text' ""默认打开文件地址 multiselection = abap_FALSE "是否可以选择多个文件 CHANGING file_table = lt_filetab rc = l_rc . IF sy-subrc <> 0. * Implement suitable error handling here ENDIF. CHECK l_rc EQ 1. READ TABLE lt_filetab INDEX 1 INTO P_FILE . ENDFORM. FORM frm_get_excel . DATA it_wa type TRUXS_T_TEXT_DATA. CALL FUNCTION 'TEXT_CONVERT_XLS_TO_SAP' EXPORTING * I_FIELD_SEPERATOR = * I_LINE_HEADER = i_tab_raw_data = it_wa i_filename = P_FILE TABLES i_tab_converted_data = gt_itab. * EXCEPTIONS * CONVERSION_FAILED = 1 * OTHERS = 2 . IF sy-subrc <> 0. * Implement suitable error handling here ENDIF. ENDFORM.
测试结果如下
从测试结果来看,并未产生数量、金额小于1,则导入时被自动放大1000或100倍的问题,所使用的是 ABAP PLATFORM 1809版本。