步骤1:
事务代码 SMW0
选择二进制数据选项点击上方按钮。
点击新建按钮输入名称和描述,上传模版文件。
案例传入 EXCEL 如下:
创建好资源库对象结果如下。
步骤2:报表效果展示
点击按钮选择上传的文件。
解析 Excel 文件结果到 alv 内表展示。
选择需要更新的数据,点击保存,修改成功后结果回写到alv表。
步骤3:源码
报表源码如下:
*&---------------------------------------------------------------------*
*& Report Z_EXCEL_TEST
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT Z_EXCEL_TEST.
INCLUDE zbcn_excel.
*----------------------------------------------------------------------*
* Type-pools/定义类型池
*----------------------------------------------------------------------*
TYPE-POOLS: slis.
*----------------------------------------------------------------------*
* Initialization/初始事件
*----------------------------------------------------------------------*
*----------------------------------------------------------------------*
* Variable/定义变量
*----------------------------------------------------------------------*
DATA:gv_filter TYPE string,
gs_functxt TYPE smp_dyntxt.
*----------------------------------------------------------------------*
* 声明ALV对象
*----------------------------------------------------------------------*
DATA:gs_layout_lvc TYPE lvc_s_layo,
gt_fcat_lvc TYPE lvc_t_fcat,
gs_fcat_lvc TYPE lvc_s_fcat.
types:
BEGIN OF ts_data,
sel type char1, "是否被选择
index TYPE sy-index,
msgtyp TYPE icon_d,
message TYPE bapi_msg.
INCLUDE type zmmtest.
TYPES:zdate TYPE DATS,
END OF ts_data.
TYPES:tt_data TYPE STANDARD TABLE OF ts_data WITH DEFAULT KEY INITIAL SIZE 0.
DATA: gt_data TYPE tt_data,
gs_data TYPE ts_data.
*----------------------------------------------------------------------*
* Selection screen/定义屏幕
*----------------------------------------------------------------------*
SELECTION-SCREEN FUNCTION KEY: 1 .
SELECTION-SCREEN BEGIN OF BLOCK block1 WITH FRAME TITLE TEXT-t01.
PARAMETERS:p_path LIKE rlgrap-filename MODIF ID m1.
SELECTION-SCREEN END OF BLOCK block1.
INITIALIZATION.
gs_functxt-icon_id = icon_export.
gs_functxt-quickinfo = '模板下载'.
gs_functxt-icon_text = '模板下载'.
sscrfields-functxt_01 = gs_functxt.
*----------------------------------------------------------------------*
* At selection-screen/屏幕事件
*----------------------------------------------------------------------*
AT SELECTION-SCREEN.
IF sscrfields-ucomm = 'FC01' .
CLEAR: gv_filter.
gv_filter = '|EXCEL-Files (*.XLS)|*.XLS|All Files(*.*)|*.*|'.
PERFORM frm_download_tmp USING gv_filter 'ZTEST'.
ENDIF.
*----------------------------------------------------------------------*
AT SELECTION-SCREEN OUTPUT.
*----------------------------------------------------------------------*
*AT SELECTION-SCREEN ON VALUE-REQUEST.
*----------------------------------------------------------------------*
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_path.
gv_filter = 'EXCEL-Files (*.XLS)|*.XLS|EXCEL-Files (*.XLSX)|*.XLSX|All Files(*.*)|*.*|'.
PERFORM frm_select_file USING gv_filter p_path.
*----------------------------------------------------------------------*
* Start-of-selection/开始选择事件
*----------------------------------------------------------------------*
START-OF-SELECTION.
*取数据
PERFORM frm_get_data.
*----------------------------------------------------------------------*
* End-of-selection/结束选择事件
*----------------------------------------------------------------------*
END-OF-SELECTION.
* 确定ALV输出列的属性
PERFORM frm_fill_fieldcat.
* 确定ALV输出格式的属性
PERFORM frm_fill_layout .
* ALV报表输出
PERFORM frm_alv_display.
FORM frm_get_data .
DATA:lv_row TYPE n LENGTH 5,
lv_message TYPE bapi_msg.
IF p_path IS INITIAL.
MESSAGE '请输入上传文件的路径!' TYPE 'S' DISPLAY LIKE 'E'.
LEAVE LIST-PROCESSING.
ENDIF.
DATA: lt_load TYPE TABLE OF zalsmex_tabline,
ls_load TYPE zalsmex_tabline.
CALL FUNCTION 'ZALSM_EXCEL_TO_INTERNAL_TABLE'
EXPORTING
filename = p_path
i_begin_col = '1'"开始列
i_begin_row = '2'"开始行
i_end_col = '5'"结束列
i_end_row = '10000'"结束行
TABLES
intern = lt_load
EXCEPTIONS
inconsistent_parameters = 1
upload_ole = 2
OTHERS = 3.
SORT lt_load BY row col.
LOOP AT lt_load INTO ls_load.
lv_row = ls_load-row + 4.
CASE ls_load-col.
WHEN 1.
gs_data-id = ls_load-value.
WHEN 2.
gs_data-name = ls_load-value.
WHEN 3.
CATCH SYSTEM-EXCEPTIONS convt_no_number = 1.
gs_data-age = ls_load-value.
ENDCATCH.
IF sy-subrc <> 0.
lv_message = '第' && lv_row && '行“年龄”列存在非数字的输入'.
MESSAGE lv_message TYPE 'S' DISPLAY LIKE 'E'.
LEAVE LIST-PROCESSING.
ENDIF.
WHEN 4.
gs_data-hobby = ls_load-value.
WHEN 5.
PERFORM frm_convert_date USING ls_load-value
CHANGING gs_data-zdate.
ENDCASE.
AT END OF row.
APPEND gs_data TO gt_data.
CLEAR:gs_data.
ENDAT.
ENDLOOP.
ENDFORM.
FORM frm_fill_fieldcat .
REFRESH: gt_fcat_lvc .
PERFORM frm_add_field USING 'X' 'MSGTYP' '状态' '' '' '' '' ''.
PERFORM frm_add_field USING 'X' 'MESSAGE' '消息' '' '' '' '' ''.
PERFORM frm_add_field USING '' 'ID' '学号' '' '' '' '' ''.
PERFORM frm_add_field USING '' 'NAME' '姓名' '' '' '' '' ''.
PERFORM frm_add_field USING '' 'AGE' '年龄' '' '' '' '' ''.
PERFORM frm_add_field USING '' 'HOBBY' '兴趣' '' '' '' '' ''.
PERFORM frm_add_field USING '' 'ZDATE' '日期' '' '' '' '' ''.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_FILL_LAYOUT
*&---------------------------------------------------------------------*
*----------------------------------------------------------------------*
FORM frm_fill_layout .
CLEAR:gs_layout_lvc.
gs_layout_lvc-zebra = 'X'.
gs_layout_lvc-cwidth_opt = 'X'.
gs_layout_lvc-box_fname = 'SEL'. "选择框
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_ALV_DISPLAY
*&---------------------------------------------------------------------*
FORM frm_alv_display .
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
EXPORTING
i_callback_program = sy-repid
i_callback_pf_status_set = 'FRM_PF_STATUS'
i_callback_user_command = 'FRM_USER_COMMAND'
is_layout_lvc = gs_layout_lvc
it_fieldcat_lvc = gt_fcat_lvc
i_save = 'A'
TABLES
t_outtab = gt_data
EXCEPTIONS
program_error = 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.
*&---------------------------------------------------------------------*
*& Form FRM_PF_STATUS
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM frm_pf_status USING pt_extab TYPE slis_t_extab .
SET PF-STATUS 'STATUS01' .
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_USER_COMMAND
*&---------------------------------------------------------------------*
* 自定义的用户命令处理
*----------------------------------------------------------------------*
* -->PV_UCOMM 用户命令类型
* -->PS_SELFIELD 用户命令信息
*----------------------------------------------------------------------*
FORM frm_user_command USING pv_ucomm TYPE sy-ucomm
ps_selfield TYPE slis_selfield.
DATA: lr_grid TYPE REF TO cl_gui_alv_grid.
ps_selfield-refresh = 'X'.
ps_selfield-col_stable = 'X'.
ps_selfield-row_stable = 'X'.
CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
IMPORTING
e_grid = lr_grid.
CALL METHOD lr_grid->check_changed_data.
CASE pv_ucomm.
WHEN '&SAVE'.
PERFORM frm_save.
WHEN OTHERS.
ENDCASE.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_ADD_FIELD
*&---------------------------------------------------------------------*
* 设置屏幕显示的字段
*----------------------------------------------------------------------*
* -->P_U_FIELDNAME text
* -->P_U_SELTEXT text
*----------------------------------------------------------------------*
FORM frm_add_field USING pv_key
pv_fieldname
pv_seltext
pv_nozero
pv_sum
pv_ref_field
pv_ref_table
pv_edit.
CLEAR:gs_fcat_lvc.
gs_fcat_lvc-key = pv_key.
gs_fcat_lvc-fieldname = pv_fieldname.
gs_fcat_lvc-coltext = pv_seltext.
gs_fcat_lvc-no_zero = pv_nozero.
gs_fcat_lvc-do_sum = pv_sum.
gs_fcat_lvc-ref_field = pv_ref_field.
gs_fcat_lvc-ref_table = pv_ref_table.
gs_fcat_lvc-edit = pv_edit.
IF pv_fieldname = 'MSGTYP'.
gs_fcat_lvc-icon = 'X'.
ENDIF.
APPEND gs_fcat_lvc TO gt_fcat_lvc.
ENDFORM. " FRM_ADD_FIELD
*&---------------------------------------------------------------------*
*& Form FRM_SAVE
*&---------------------------------------------------------------------*
FORM frm_save.
DATA:gs_test TYPE zmmtest.
LOOP AT gt_data into gs_data WHERE sel = 'X'.
CLEAR:gs_test.
gs_test = CORRESPONDING #( gs_data ).
MODIFY zmmtest from gs_test.
gs_data-msgtyp = icon_led_green.
gs_data-message = '新建成功'.
modify gt_data from gs_data.
ENDLOOP.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_CONVERT_DATE
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*
*&---------------------------------------------------------------------*
FORM frm_convert_date USING pv_value
CHANGING pv_date TYPE d.
TRY.
CALL METHOD cl_abap_datfm=>conv_date_ext_to_int
EXPORTING
im_datext = pv_value
* IM_DATFMDES =
IMPORTING
ex_datint = pv_date
* EX_DATFMUSED =
.
CATCH cx_abap_datfm_no_date .
* MESSAGE 'No Date' TYPE 'S' DISPLAY LIKE 'E'.
MESSAGE '日期格式错误' TYPE 'S' DISPLAY LIKE 'E'.
LEAVE LIST-PROCESSING.
CATCH cx_abap_datfm_invalid_date .
* MESSAGE 'Invlid Date' TYPE 'S' DISPLAY LIKE 'E'.
MESSAGE '日期格式错误' TYPE 'S' DISPLAY LIKE 'E'.
LEAVE LIST-PROCESSING.
CATCH cx_abap_datfm_format_unknown .
* MESSAGE 'Unknown Date format' TYPE 'S' DISPLAY LIKE 'E'.
MESSAGE '日期格式错误' TYPE 'S' DISPLAY LIKE 'E'.
LEAVE LIST-PROCESSING.
CATCH cx_abap_datfm_ambiguous .
* MESSAGE 'Ambiguous Date' TYPE 'S' DISPLAY LIKE 'E'.
MESSAGE '日期格式错误' TYPE 'S' DISPLAY LIKE 'E'.
LEAVE LIST-PROCESSING.
ENDTRY.
ENDFORM.
2.包含文件源码如下:
*&---------------------------------------------------------------------*
*& Include ZBCN_EXCEL
*&---------------------------------------------------------------------*
TABLES: sscrfields.
DATA: gs_key LIKE wwwdatatab,
gv_stripped_name TYPE rlgrap-filename,
gv_file_path TYPE rlgrap-filename.
DATA:gt_iexcel TYPE STANDARD TABLE OF alsmex_tabline,
gs_iexcel TYPE alsmex_tabline.
* Batchinputdata of single transaction
DATA: bdcdata LIKE bdcdata OCCURS 0 WITH HEADER LINE.
* messages of call transaction
DATA: messtab LIKE bdcmsgcoll OCCURS 0 WITH HEADER LINE.
DATA: ctumode LIKE ctu_params-dismode VALUE 'N'.
"A: show all dynpros
"E: show dynpro on error only
"N: do not display dynpro
DATA: cupdate LIKE ctu_params-updmode VALUE 'L'.
"S: synchronously
"A: asynchronously
"L: local
*&---------------------------------------------------------------------*
*& Form FRM_SELECT_FILE
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM frm_select_file USING pv_title pv_filename.
CALL FUNCTION 'WS_FILENAME_GET'
EXPORTING
mask = ',Excel Files xls,*.xls,Excel Files xlsx,*.xlsx,TXT Files,*.txt,ALL Files,*.*'
mode = '0'
title = pv_title
IMPORTING
filename = pv_filename
EXCEPTIONS
inv_winsys = 1
no_batch = 2
selection_cancel = 3
selection_error = 4
OTHERS = 5.
ENDFORM. " FRM_SELECT_FILE
*&---------------------------------------------------------------------*
*& Form FRM_GET_EXCEL_DATA
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM frm_get_excel_data USING pv_file
pv_beg_col
pv_beg_row
pv_end_col
pv_end_row
pv_del_row.
REFRESH: gt_iexcel.
CALL FUNCTION 'ALSM_EXCEL_TO_INTERNAL_TABLE'
EXPORTING
filename = pv_file
i_begin_col = pv_beg_col
i_begin_row = pv_beg_row
i_end_col = pv_end_col
i_end_row = pv_end_row
TABLES
intern = gt_iexcel
EXCEPTIONS
inconsistent_parameters = 1
upload_ole = 2
OTHERS = 3.
IF sy-subrc <> 0.
if sy-langu = '1' .
MESSAGE '导入数据文件失败!' TYPE 'S'.
else.
MESSAGE 'Failed to import data file!' TYPE 'S'.
endif.
LEAVE LIST-PROCESSING.
ENDIF.
ENDFORM. " FRM_GET_EXCEL_DATA
*&---------------------------------------------------------------------*
*& Form FRM_DOWNLOAD_TMP
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->pv_filter text
* -->pv_objid text
*----------------------------------------------------------------------*
FORM frm_download_tmp USING pv_filter
pv_objid.
DATA: pv_fd LIKE rlgrap-filename.
if sy-langu = '1' .
CALL FUNCTION 'WS_FILENAME_GET'
EXPORTING
mask = pv_filter
mode = '0'
title = '模板下载'
IMPORTING
filename = pv_fd
EXCEPTIONS
inv_winsys = 1
no_batch = 2
selection_cancel = 3
selection_error = 4
OTHERS = 5.
else.
CALL FUNCTION 'WS_FILENAME_GET'
EXPORTING
mask = pv_filter
mode = '0'
title = 'Template download'
IMPORTING
filename = pv_fd
EXCEPTIONS
inv_winsys = 1
no_batch = 2
selection_cancel = 3
selection_error = 4
OTHERS = 5.
endif.
IF pv_fd IS NOT INITIAL.
CLEAR: gs_key.
gs_key-relid = 'MI'.
gs_key-objid = pv_objid.
CALL FUNCTION 'DOWNLOAD_WEB_OBJECT'
EXPORTING
key = gs_key
destination = pv_fd.
CALL FUNCTION 'SO_SPLIT_FILE_AND_PATH'
EXPORTING
full_name = pv_fd
IMPORTING
stripped_name = gv_stripped_name
file_path = gv_file_path.
ENDIF.
ENDFORM. " FRM_DOWNLOAD_TMP
*&---------------------------------------------------------------------*
*& Form BDC_DYNPRO
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->PROGRAM text
* -->DYNPRO text
*----------------------------------------------------------------------*
FORM bdc_dynpro USING program dynpro.
CLEAR bdcdata.
bdcdata-program = program.
bdcdata-dynpro = dynpro.
bdcdata-dynbegin = 'X'.
APPEND bdcdata.
ENDFORM. "BDC_DYNPRO
*----------------------------------------------------------------------*
* Insert field *
*----------------------------------------------------------------------*
FORM bdc_field USING fnam fval.
DATA: lv_fval TYPE string.
lv_fval = fval.
CONDENSE lv_fval.
CLEAR bdcdata.
bdcdata-fnam = fnam.
bdcdata-fval = lv_fval.
APPEND bdcdata.
ENDFORM. "BDC_FIELD
需要的结构类型定义:
3.ZALSM_EXCEL_TO_INTERNAL_TABLE 接口代码
FUNCTION zalsm_excel_to_internal_table .
*"----------------------------------------------------------------------
*"*"本地接口:
*" IMPORTING
*" VALUE(FILENAME) LIKE RLGRAP-FILENAME
*" VALUE(SHEET_NAME) TYPE CHAR50 OPTIONAL
*" 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.
*add by hubert begin---------
sy-subrc = 0. "强制
* set property of application 'Visible' = 1.
* m_message.
IF sheet_name = space."用默认模式
GET PROPERTY OF application 'ACTIVESHEET' = worksheet.
m_message.
ELSE.
* -->读取指定SHEET
CALL METHOD OF application 'WORKSHEETS' = worksheet
EXPORTING
#1 = sheet_name.
* #2 = 0 "UpdateLinks
* #3 = 0 "ReadOnly
* #4 = 1. "Format
CALL METHOD OF worksheet 'Activate'.
m_message.
ENDIF.
*add by hubert end--------
*
* 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.
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
CALL METHOD OF application 'QUIT'.
m_message.
* >>>>> Begin of change note 575877
* to kill the Excel process it's necessary to free all used objects
FREE OBJECT h_cell. m_message.
FREE OBJECT h_cell1. m_message.
FREE OBJECT range. m_message.
FREE OBJECT worksheet. m_message.
FREE OBJECT workbook. m_message.
FREE OBJECT application. m_message.
* <<<<< End of change note 575877
ENDFUNCTION.