REPORT zdemo001 NO STANDARD PAGE HEADING.
TYPE-POOLS: slis.
INCLUDE
TABLES: sscrfields, zttpm_debt01.
DATA: gt_zttpm_debt01 TYPE TABLE OF zttpm_debt01,
gr_grid TYPE REF TO cl_gui_alv_grid,
g_error TYPE char1.
TYPES:BEGIN OF ty_partner,
partner TYPE bu_partner,
END OF ty_partner.
DATA: lt_zttpm_debt01 TYPE TABLE OF zttpm_debt01.
DATA: ls_partner TYPE ty_partner.
DATA: lt_partner TYPE TABLE OF ty_partner.
DATA: lv_qianqi_debt TYPE char20,
lv_qianqi_int TYPE char20.
FIELD-SYMBOLS:
SELECTION-SCREEN FUNCTION KEY 1.
SELECTION-SCREEN BEGIN OF BLOCK blk1 WITH FRAME TITLE text-001.
PARAMETERS: p_search RADIOBUTTON GROUP rg1 DEFAULT 'X' USER-COMMAND setype,
p_import RADIOBUTTON GROUP rg1.
SELECTION-SCREEN END OF BLOCK blk1.
SELECTION-SCREEN BEGIN OF BLOCK blk2 WITH FRAME TITLE text-002.
SELECT-OPTIONS: s_bp FOR zttpm_debt01-customer_bp MODIF ID bk1.
SELECTION-SCREEN END OF BLOCK blk2.
SELECTION-SCREEN BEGIN OF BLOCK blk3 WITH FRAME TITLE text-003.
PARAMETERS: p_file TYPE string LOWER CASE MEMORY ID file MODIF ID bk2.
SELECTION-SCREEN END OF BLOCK blk3.
INITIALIZATION.
DATA: functxt TYPE smp_dyntxt.
functxt-icon_id = icon_export.
functxt-quickinfo = '下载模板'.
functxt-icon_text = '下载模板'.
sscrfields-functxt_01 = functxt.
AT SELECTION-SCREEN OUTPUT.
CASE 'X'.
WHEN p_search.
LOOP AT SCREEN.
IF screen-group1 = 'BK1'.
screen-active = '1'.
ENDIF.
IF screen-group1 = 'BK2'.
screen-active = '0'.
ENDIF.
MODIFY SCREEN.
ENDLOOP.
WHEN p_import.
LOOP AT SCREEN.
IF screen-group1 = 'BK1'.
screen-active = '0'.
ENDIF.
IF screen-group1 = 'BK2'.
screen-active = '1'.
ENDIF.
MODIFY SCREEN.
ENDLOOP.
ENDCASE.
AT SELECTION-SCREEN.
CASE sscrfields-ucomm.
WHEN 'FC01'.
CALL METHOD zmcl_service_common=>download_file_template
EXPORTING
iv_objid = 'ZMR057'
iv_default_extension = '.XLSX'
iv_default_file_name = '信息导入.XLSX'.
ENDCASE.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
DATA: lv_desktop TYPE string,
lv_return TYPE i,
lt_filename TYPE filetable,
lv_filename TYPE file_table.
cl_gui_frontend_services=>get_desktop_directory( CHANGING desktop_directory = lv_desktop ).
CALL METHOD cl_gui_frontend_services=>file_open_dialog
EXPORTING
default_extension = 'XLSX'
initial_directory = lv_desktop
CHANGING
file_table = lt_filename
rc = lv_return
EXCEPTIONS
OTHERS = 99.
READ TABLE lt_filename INTO lv_filename INDEX 1.
p_file = lv_filename.
START-OF-SELECTION.
CASE abap_true.
**********************************************************************
* 查询数据
**********************************************************************
WHEN p_search.
SELECT * INTO TABLE gt_zttpm_debt01
FROM zttpm_debt01
WHERE customer_bp IN s_bp.
**********************************************************************
* 数据导入
**********************************************************************
WHEN p_import.
DATA: lr_reader TYPE REF TO zif_excel_reader,
lr_excel TYPE REF TO zcl_excel,
lr_sheet TYPE REF TO zcl_excel_worksheet,
lv_cell TYPE i,
lv_index TYPE string.
FIELD-SYMBOLS:
TRY.
CREATE OBJECT lr_reader TYPE zcl_excel_reader_2007.
lr_excel = lr_reader->load_file( p_file ).
CATCH zcx_excel.
MESSAGE '导入出错' TYPE 'S' DISPLAY LIKE 'E'.
EXIT.
ENDTRY.
lr_sheet = lr_excel->get_active_worksheet( ).
* 删除表头
DELETE lr_sheet->sheet_content WHERE cell_row <= 1 OR cell_column >= 8.
FIELD-SYMBOLS:
LOOP AT lr_sheet->sheet_content ASSIGNING
AT NEW cell_row.
APPEND INITIAL LINE TO gt_zttpm_debt01 ASSIGNING
ENDAT.
CONDENSE
lv_cell =
"IF
ASSIGN COMPONENT lv_cell OF STRUCTURE
IF sy-subrc = 0.
TRY.
CATCH cx_root.
DATA: lv_float TYPE f.
lv_float =
ENDTRY.
ENDIF.
"ENDIF.
* IF
*
* ENDIF.
ENDLOOP.
ENDCASE.
**********************************************************************
* 显示数据
**********************************************************************
DATA: ls_layout TYPE lvc_s_layo,
lt_fieldcat TYPE lvc_t_fcat.
FIELD-SYMBOLS:
ls_layout-col_opt = abap_true.
ls_layout-cwidth_opt = abap_true.
ls_layout-zebra = abap_true.
ls_layout-sel_mode = 'A'.
CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
EXPORTING
i_structure_name = 'ZTTPM_DEBT01'
i_buffer_active = abap_true
i_bypassing_buffer = abap_true
CHANGING
ct_fieldcat = lt_fieldcat.
LOOP AT lt_fieldcat ASSIGNING
CASE
WHEN 'CUSTOMER_BP'.
WHEN 'CUSTOMER_NAME'.
WHEN 'QIANQI_DEBT'.
WHEN 'QIANQI_INT'.
WHEN 'UPDATE_DATE'.
WHEN 'UPDATE_BY'.
WHEN 'MESSAGE'.
WHEN OTHERS.
DELETE lt_fieldcat.
CONTINUE.
ENDCASE.
ENDLOOP.
IF p_search EQ 'X'.
DELETE lt_fieldcat INDEX 7.
ENDIF.
SORT gt_zttpm_debt01 BY customer_bp.
SELECT partner FROM but000 INTO TABLE lt_partner
FOR ALL ENTRIES IN gt_zttpm_debt01
WHERE partner = gt_zttpm_debt01-customer_bp.
SORT lt_partner BY partner.
LOOP AT gt_zttpm_debt01 ASSIGNING
READ TABLE lt_partner INTO ls_partner WITH KEY partner =
IF ls_partner-partner IS INITIAL.
g_error = 'X'.
ENDIF.
IF
lv_qianqi_debt =
CONDENSE lv_qianqi_debt NO-GAPS.
IF cl_abap_matcher=>matches(
pattern = '^(-?[1-9]\d*(\.\d*[1-9])?)|(-?0\.\d*[1-9])$'
text = lv_qianqi_debt ) = abap_true.
ELSE.
g_error = 'X'.
ENDIF.
ENDIF.
IF
lv_qianqi_int =
CONDENSE lv_qianqi_int NO-GAPS.
IF cl_abap_matcher=>matches(
pattern = '^(-?[1-9]\d*(\.\d*[1-9])?)|(-?0\.\d*[1-9])$'
text = lv_qianqi_int ) = abap_true.
ELSE.
g_error = 'X'.
ENDIF.
ENDIF.
CLEAR:ls_partner.
ENDLOOP.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
EXPORTING
i_callback_program = sy-repid
is_layout_lvc = ls_layout
i_callback_pf_status_set = 'FRM_SET_STATUS'
i_callback_user_command = 'FRM_USER_COMMAND'
it_fieldcat_lvc = lt_fieldcat
TABLES
t_outtab = gt_zttpm_debt01.
**********************************************************************
*&---------------------------------------------------------------------*
*& Form frm_save
*&---------------------------------------------------------------------*
* 保存数据
*----------------------------------------------------------------------*
FORM frm_save.
IF g_error IS NOT INITIAL.
MESSAGE '导入数据有错误不能保存!' TYPE 'W'.
EXIT.
ENDIF.
MODIFY zttpm_debt01 FROM TABLE gt_zttpm_debt01.
IF sy-subrc = 0.
MESSAGE '保存成功' TYPE 'S'.
ENDIF.
COMMIT WORK AND WAIT.
ENDFORM. "frm_save
*&---------------------------------------------------------------------*
*& Form frm_user_command
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_UCOMM text
* -->P_RS_SELFIELD text
*----------------------------------------------------------------------*
FORM frm_user_command USING p_ucomm TYPE sy-ucomm
p_selfield TYPE slis_selfield .
DATA: lv_answer TYPE c.
CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
IMPORTING
e_grid = gr_grid.
CASE p_ucomm.
WHEN 'SAVE' OR '&DATA_SAVE'.
PERFORM frm_save.
p_selfield-refresh = abap_true.
WHEN 'DELETE'.
PERFORM frm_delete.
p_selfield-refresh = abap_true.
ENDCASE.
ENDFORM. "frm_user_command
*&---------------------------------------------------------------------*
*& Form frm_set_status
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_EXTAB text
*----------------------------------------------------------------------*
FORM frm_set_status USING p_extab TYPE slis_t_extab.
DATA lt_buttons TYPE TABLE OF sy-ucomm.
IF p_import EQ ''.
APPEND 'SAVE' TO lt_buttons.
DELETE lt_buttons WHERE table_line EQ 'DELETE'.
ELSE.
APPEND 'DELETE' TO lt_buttons.
DELETE lt_buttons WHERE table_line EQ 'SAVE'.
ENDIF.
SET PF-STATUS 'GUIALV01' EXCLUDING lt_buttons.
ENDFORM. "frm_set_status
*&---------------------------------------------------------------------*
*& Form frm_delete
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM frm_delete.
DATA: lt_zttpm_debt01 TYPE TABLE OF zttpm_debt01,
ls_zttpm_debt01 TYPE zttpm_debt01,
lt_rows TYPE lvc_t_row,
ls_row TYPE lvc_s_row.
CALL METHOD gr_grid->get_selected_rows
IMPORTING
et_index_rows = lt_rows.
LOOP AT lt_rows INTO ls_row.
READ TABLE gt_zttpm_debt01 INTO ls_zttpm_debt01 INDEX ls_row-index.
IF sy-subrc = 0.
APPEND ls_zttpm_debt01 TO lt_zttpm_debt01.
ENDIF.
ENDLOOP.
IF lt_zttpm_debt01[] IS NOT INITIAL.
DELETE zttpm_debt01 FROM TABLE lt_zttpm_debt01.
IF sy-subrc = 0.
MESSAGE '删除成功' TYPE 'S'.
ENDIF.
COMMIT WORK AND WAIT.
ENDIF.
LOOP AT lt_zttpm_debt01 INTO ls_zttpm_debt01.
DELETE TABLE gt_zttpm_debt01 FROM
ENDLOOP.
CALL METHOD gr_grid->refresh_table_display
EXCEPTIONS
finished = 1
OTHERS = 2.
ENDFORM. " frm_delete