直接上代码
REPORT ztest_cj08.DATA: filename TYPE string VALUE 'c:bathinput.csv'.TYPES: BEGIN OF itab, F(20), END OF itab.DATA: data_tab TYPE STANDARD TABLE OF itab.DATA: wa_tab TYPE itab.DATA: file_filter TYPE string, rc TYPE i.DATA: file_table TYPE filetable.DATA: wa_file_table TYPE LINE OF filetable.DATA lv_fname TYPE localfile.DATA: lt_upload TYPE TABLE OF zalsmex_tabline. file_filter = 'All Files (*.*)|*'.*弹框取文件路径PERFORM frm_getfilename CHANGING lv_fname.*取EXCEL表数据CALL FUNCTION 'ZALSM_EXCEL_TO_INTERNAL_TABLE' EXPORTING filename = lv_fname i_begin_col = 1 i_begin_row = 6 i_end_col = 35 i_end_row = 65535* I_SHEET_NAME = LV_FNAME TABLES intern = lt_upload EXCEPTIONS inconsistent_parameters = 1 upload_ole = 2 OTHERS = 3.IF sy-subrc <> 0. MESSAGE s018(zsd01) DISPLAY LIKE 'E'. STOP.
ENDIF.
其中用到的FM:ZALSM_EXCEL_TO_INTERNAL_TABLE,代码如下
function ZALSM_EXCEL_TO_INTERNAL_TABLE .*"----------------------------------------------------------------------*"*"本地接口:*" IMPORTING*" VALUE(FILENAME) LIKE RLGRAP-FILENAME*" VALUE(I_BEGIN_COL) TYPE I*" VALUE(I_BEGIN_ROW) TYPE I*" VALUE(I_END_COL) TYPE I*" VALUE(I_END_ROW) TYPE I*" TABLES*" INTERN STRUCTURE ZALSMEX_TABLINE*" EXCEPTIONS*" INCONSISTENT_PARAMETERS*" UPLOAD_OLE*"---------------------------------------------------------------------- data: EXCEL_TAB type TY_T_SENDER. data: LD_SEPARATOR type C. data: APPLICATION type OLE2_OBJECT, WORKBOOK type OLE2_OBJECT, RANGE type OLE2_OBJECT, WORKSHEET type OLE2_OBJECT. data: H_CELL type OLE2_OBJECT, H_CELL1 type OLE2_OBJECT. data: LD_RC type I.* Rückgabewert der Methode "clipboard_export "* Makro für Fehlerbehandlung der Methods define M_MESSAGE. case sy-subrc. when 0. when 1. message id sy-msgid type sy-msgty number sy-msgno with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4. when others. raise upload_ole. endcase. end-of-definition.* check parameters if I_BEGIN_ROW > I_END_ROW. raise INCONSISTENT_PARAMETERS. endif. if I_BEGIN_COL > I_END_COL. raise INCONSISTENT_PARAMETERS. endif.* Get TAB-sign for separation of fields class CL_ABAP_CHAR_UTILITIES definition load. LD_SEPARATOR = CL_ABAP_CHAR_UTILITIES=>HORIZONTAL_TAB.* open file in Excel if APPLICATION-HEADER = SPACE or APPLICATION-HANDLE = -1. create object APPLICATION 'Excel.Application'. M_MESSAGE. endif. call method of APPLICATION 'Workbooks' = WORKBOOK. M_MESSAGE. call method of WORKBOOK 'Open' exporting #1 = FILENAME. M_MESSAGE.* set property of application 'Visible' = 1.* m_message. get property of APPLICATION 'ACTIVESHEET' = WORKSHEET. M_MESSAGE.* mark whole spread sheet call method of WORKSHEET 'Cells' = H_CELL exporting #1 = I_BEGIN_ROW #2 = I_BEGIN_COL. M_MESSAGE. call method of WORKSHEET 'Cells' = H_CELL1 exporting #1 = I_END_ROW #2 = I_END_COL. M_MESSAGE. call method of WORKSHEET 'RANGE' = RANGE exporting #1 = H_CELL #2 = H_CELL1. M_MESSAGE. call method of RANGE 'SELECT'. M_MESSAGE.* copy marked area (whole spread sheet) into Clippboard call method of RANGE 'COPY'. M_MESSAGE.* read clipboard into ABAP call method CL_GUI_FRONTEND_SERVICES=>CLIPBOARD_IMPORT importing DATA = EXCEL_TAB exceptions CNTL_ERROR = 1* ERROR_NO_GUI = 2* NOT_SUPPORTED_BY_GUI = 3 others = 4. if SY-SUBRC <> 0. message A037(ALSMEX). endif. if EXCEL_TAB[] is initial. exit. endif. perform SEPARATED_TO_INTERN_CONVERT tables EXCEL_TAB INTERN using LD_SEPARATOR.* clear clipboard refresh EXCEL_TAB. call method CL_GUI_FRONTEND_SERVICES=>CLIPBOARD_EXPORT importing DATA = EXCEL_TAB changing RC = LD_RC exceptions CNTL_ERROR = 1* ERROR_NO_GUI = 2* NOT_SUPPORTED_BY_GUI = 3 others = 4.* quit Excel and free ABAP Object - unfortunately, this does not kill* the Excel process