ABAP实验笔记:ALV标准excel导出功能

"标准alv excel导出功能
"程序类关系
"类:CL_SALV_BS_DATA_TABLE
"构造方法:ID String
"方法:IF_SALV_BS_DATA_SOURCE~SET_TABLE
"类:CL_SALV_BS_MODEL
"构造方法:CL_SALV_BS_DATA

"类:CL_SALV_BS_MODEL_TABLE
"构造方法:CL_SALV_BS_DATA_TABLE
"类:CL_SALV_BS_SERVICE_MNGR
"构造方法:CL_SALV_BS_MODEL

"获取这个类对象?
"类:CL_SALV_BS_RESULT_DATA_TABLE
"构造方法:CL_SALV_BS_MODEL_TABLE类对象
"          CL_SALV_BS_SERVICE_MNGR类对象

"类:CL_SALV_BS_EX_OFFICE2007
"构造方法:传入CL_SALV_BS_RESULT_DATA_TABLE类对象
"方法:TRANSFORM,返回xstring
"Function:'SCMS_XSTRING_TO_BINARY'转换成raw table
"cl_bcs_convert=>xstring_to_solix( iv_xstring  = xdata ).转换成raw table
"cl_gui_frontend_services=>gui_download( ).

"设置数据表
"lr_data TYPE REF TO cl_salv_bs_data_table_actions.
"create object lr_data.
"ASSIGN r_data->* TO . r_data
*lr_data->set_data(
*  EXPORTING
*    as_reference = abap_false
*  CHANGING
*    value        =  ).
"这个类怎么来?
" r_result_data type CL_SALV_EX_RESULT_DATA_TABLE
* CREATE OBJECT lr_excel2007 TYPE CL_SALV_BS_EX_OFFICE2007
*   EXPORTING
*     r_result_data = r_result_data.
"调用transform获取xstring
*call method lr_excel2007->transform
*  receiving
*    excel_xml = xml.



START-OF-SELECTION.
  PERFORM f_start_program.


FORM f_start_program.
  PERFORM f_test_excel.
ENDFORM.


FORM f_test_excel.
  DATA:lt_fieldcat TYPE lvc_t_fcat.
  DATA:lt_spfli TYPE TABLE OF spfli.
  DATA:ls_spfli LIKE LINE OF lt_spfli.
  DATA:lo_table TYPE REF TO DATA.
  DATA:lv_xstr TYPE xstring.
  DATA:lt_rawdata TYPE solix_tab.
  DATA:lo_result_data_table TYPE REF TO cl_salv_ex_result_data_table.
  DATA:lo_bs_ex_office2007 TYPE REF TO cl_salv_bs_ex_office2007.
  DATA:lv_version TYPE string.
  DATA:lv_flavour TYPE string.

  "获取数据
  SELECT * INTO TABLE lt_spfli FROM spfli.
  GET REFERENCE OF lt_spfli INTO lo_table.
  "构造列字段
  PERFORM f_create_fieldcat CHANGING lt_fieldcat.
  "获取cl_salv_ex_result_data_table类对象
  lo_result_data_table = cl_salv_ex_util=>factory_result_data_table(
    r_data = lo_table
    t_fieldcatalog =  lt_fieldcat ).
  "获取xml版本信息
  lv_version = cl_salv_bs_a_xml_base=>get_version( ).
  IF lv_version = if_salv_bs_xml=>version_25.
    lv_version = if_salv_bs_xml=>version_25.
  ELSEIF lv_version = if_salv_bs_xml=>version_26.
    lv_version = if_salv_bs_xml=>version_26.
  ENDIF.
  lv_flavour = if_salv_bs_c_tt=>c_tt_xml_flavour_export.
*  "方式1:转换xstring
*  CREATE OBJECT lo_bs_ex_office2007 EXPORTING r_result_data = lo_result_data_table.
*  lv_xstr = lo_bs_ex_office2007->transform( ).
*  lt_rawdata = cl_bcs_convert=>xstring_to_solix( iv_xstring  = lv_xstr ).

  "方式2:转换xstring
  cl_salv_bs_tt_util=>if_salv_bs_tt_util~transform(
    EXPORTING
      xml_version   = lv_version
      r_result_data = lo_result_data_table
      xml_type      = if_salv_bs_xml=>c_type_xlsx  " XML Type as SALV Constant
      xml_flavour   = lv_flavour
      gui_type      = if_salv_bs_xml=>c_gui_type_gui " Constant
    IMPORTING
      xml           = lv_xstr
   ).

  PERFORM f_dialog_xml USING lv_xstr.
ENDFORM.

FORM f_create_fieldcat
  CHANGING it_fieldcat TYPE lvc_t_fcat.
  DATA:ls_fieldcat TYPE lvc_s_fcat.
  CLEAR ls_fieldcat.
  ls_fieldcat-col_pos = 1.        "column position
  ls_fieldcat-fieldname = 'CARRID'.
  ls_fieldcat-reptext = '航班ID'.
  APPEND ls_fieldcat TO it_fieldcat.
  CLEAR ls_fieldcat.
  ls_fieldcat-col_pos = 2.        "column position
  ls_fieldcat-fieldname = 'CONNID'.
  ls_fieldcat-reptext = '编码'.
  APPEND ls_fieldcat TO it_fieldcat.
ENDFORM.

"弹窗下载xml
FORM f_dialog_xml
  USING iv_xml TYPE xstring.
  DATA:lv_extension TYPE string VALUE 'XLSX'.
  DATA:lv_directory TYPE string VALUE 'D:/'.
  DATA:lv_filename TYPE string VALUE 'test'.
  DATA:lv_mask TYPE string VALUE 'Excel (*.XLSX)|*.XLSX'.
  CALL FUNCTION 'XML_EXPORT_DIALOG'
    EXPORTING
      I_XML                            = iv_xml
      I_DEFAULT_EXTENSION              = lv_extension
      I_INITIAL_DIRECTORY              = lv_directory
      I_DEFAULT_FILE_NAME              = lv_filename
      I_MASK                           = lv_mask
*     I_APPLICATION                    =
*   EXCEPTIONS
*     APPLICATION_NOT_EXECUTABLE       = 1
*     OTHERS                           = 2
            .
  IF SY-SUBRC <> 0.
* Implement suitable error handling here
  ENDIF.

ENDFORM.

你可能感兴趣的:(ABAP,abap,经验分享)