ABAP ABAP2XLSX 开源项目 excel 上传数据处理 demo

这是一个SAPABAP程序,主要功能是从用户上传的Excel文件中读取数据,然后进行处理,并使用ALV(ABAPListViewer)显示数据。程序首先定义了数据结构,接着处理文件选择事件,通过动态参数读取功能获取上传的文件名。然后使用特定的类来读取Excel文件的内容,并存储到内部表中。最后,程序通过CL_SALV_TABLE类创建并显示ALV表格。
摘要由CSDN通过智能技术生成
*&---------------------------------------------------------------------*
*& Report ZTEST_22
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT ztest_22.

TYPES: BEGIN OF ts_excel,
         zz01 TYPE string,
         zz02 TYPE string,
         zz03 TYPE string,
         zz04 TYPE string,
         zz05 TYPE string,
         zz06 TYPE string,
       END OF ts_excel.

DATA gt_data  TYPE TABLE OF ts_excel.
DATA go_salv  TYPE  REF TO cl_salv_table.

PARAMETERS: p_upfile TYPE string LOWER CASE.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_upfile.

  PERFORM f4_p_upfile CHANGING p_upfile.

START-OF-SELECTION.

*Get upload data
  PERFORM get_upd_data.

*deal data

*ALv display
  PERFORM display_alv_data .




*&---------------------------------------------------------------------*
*& Form f4_p_upfile
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*&      <-- P_UPFILE
*&---------------------------------------------------------------------*
FORM f4_p_upfile  CHANGING p_upfile TYPE string.
  DATA: lv_repid       TYPE syrepid,
        lt_fields      TYPE dynpread_tabtype,
        ls_field       LIKE LINE OF lt_fields,
        lt_files       TYPE filetable,
        lv_rc          TYPE i,
        lv_file_filter TYPE string.

  lv_repid = sy-repid.

  CALL FUNCTION 'DYNP_VALUES_READ'
    EXPORTING
      dyname               = lv_repid
      dynumb               = '1000'
      request              = 'A'
    TABLES
      dynpfields           = lt_fields
    EXCEPTIONS
      invalid_abapworkarea = 01
      invalid_dynprofield  = 02
      invalid_dynproname   = 03
      invalid_dynpronummer = 04
      invalid_request      = 05
      no_fielddescription  = 06
      undefind_error       = 07.
  READ TABLE lt_fields INTO ls_field WITH KEY fieldname = 'P_UPFILE'.
  p_upfile = ls_field-fieldvalue.

  lv_file_filter = 'Excel Files (*.XLSX;*.XLSM)|*.XLSX;*.XLSM'.
  cl_gui_frontend_services=>file_open_dialog( EXPORTING
                                                default_filename        = p_upfile
                                                file_filter             = lv_file_filter
                                              CHANGING
                                                file_table              = lt_files
                                                rc                      = lv_rc
                                              EXCEPTIONS
                                                OTHERS                  = 1 ).
  READ TABLE lt_files INDEX 1 INTO p_upfile.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form get_upd_data
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM get_upd_data .

  DATA: lo_excel     TYPE REF TO zcl_excel,
        lo_reader    TYPE REF TO zif_excel_reader,
        lo_worksheet TYPE REF TO zcl_excel_worksheet.
  DATA  lo_error     TYPE REF TO zcx_excel.
  TRY.
      CREATE OBJECT lo_reader TYPE zcl_excel_reader_2007.

      lo_excel = lo_reader->load_file( i_filename = p_upfile i_from_applserver = '' ).

      lo_worksheet = lo_excel->get_worksheet_by_index( iv_index = 1 ).

      lo_worksheet->get_table( IMPORTING et_table = gt_data ).

      BREAK-POINT.

    CATCH zcx_excel INTO lo_error.
      DATA(lv_text)  = lo_error->if_message~get_longtext( ).

      MESSAGE lv_text TYPE 'I' DISPLAY LIKE 'E'.

  ENDTRY.

ENDFORM.
*&---------------------------------------------------------------------*
*& Form display_alv_data
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM display_alv_data .

  TRY.
      cl_salv_table=>factory(
      IMPORTING
        r_salv_table = go_salv
      CHANGING
          t_table        = gt_data ).
    CATCH cx_salv_msg.                                  "#EC NO_HANDLER

  ENDTRY.

  go_salv->display( ).

ENDFORM.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值