2020.03.01 【ABAP随笔】- Excel批量导出-SAP_CONVERT_TO_XLS_FORMAT

今天写一下SAP_CONVERT_TO_XLS_FORMAT 批量导出excel的函数

部分功能代码如下:

FORM frm_export_excel_1 .

  DATA li_filename TYPE rlgrap-filename.
  DATA ls_filename TYPE string.
  li_filename = p_file.
  ls_filename = p_file.

  CALL METHOD zcl_document_jxzhu=>download_template_to_frontend
    EXPORTING
      iv_fun           = '2'  "选择FUN的功能
      name_of_template = '物料主数据客制表' 
      iv_filepath      = ls_filename "当IV_FUN = 2时,保存路径iv_filepath必填 这样就不会弹框选择,可以引用函数外部的路径
*     iv_relid         = 'MI'
      iv_objid         = 'ZMMT001'
    IMPORTING
      rv_filepath      = ls_filename
    EXCEPTIONS
      download_error   = 1
      OTHERS           = 2.

  WAIT UP TO 2 SECONDS."确保从服务器下载的模板保存下来,之后再打开输入。
  li_filename = ls_filename.

  SELECT * INTO CORRESPONDING FIELDS OF TABLE @lt_zmmt001 FROM zmmt001.

  READ TABLE lt_zmmt001 into data(ls_zmmt001) INDEX 1 .

  "为了防止格式的问题,使用这个函数的时候,输出最好用char型
  TYPES:BEGIN OF lty_excel,
          matnr TYPE char40,
          id    TYPE char10,
          f1    TYPE char10,
          f2    TYPE char10,
          f3    TYPE char10,
        END OF lty_excel.
  DATA lt_zmmt001_excel TYPE TABLE OF lty_excel.

  MOVE-CORRESPONDING lt_zmmt001 TO lt_zmmt001_excel.

  INSERT INITIAL LINE INTO lt_zmmt001_excel ASSIGNING FIELD-SYMBOL(<fs1>) INDEX 1.
  DATA tablestructure TYPE REF TO cl_abap_structdescr.
  tablestructure ?= cl_abap_typedescr=>describe_by_data( ls_zmmt001 ).
  LOOP AT tablestructure->components INTO DATA(ls_comps).
    ASSIGN COMPONENT sy-tabix OF STRUCTURE <fs1> TO FIELD-SYMBOL(<fs2>).
    IF sy-subrc EQ 0 .
      <fs2> = ls_comps-name.
    ENDIF.
  ENDLOOP.

  CALL FUNCTION 'SAP_CONVERT_TO_XLS_FORMAT'
    EXPORTING
*     I_FIELD_SEPERATOR =
      i_line_header     = 'X'
      i_filename        = li_filename
*     I_APPL_KEEP       = ' '
    TABLES
      i_tab_sap_data    = lt_zmmt001_excel
* CHANGING
*     I_TAB_CONVERTED_DATA       =
    EXCEPTIONS
      conversion_failed = 1
      OTHERS            = 2.

ENDFORM.
这里需要提一下,为了解决输出内容格式的问题:
1.     先将一个Excel单位格属性设置为文本格式,然后保存下来,用SMW0将EXCEL上传到服务器上
2.     用zcl_document_jxzhu=>download_template_to_fronten方法来讲模板下载下来

3.   使用SAP_CONVERT_TO_XLS_FORMAT将内表数据导入到Excel

关于

zcl_document_jxzhu=>download_template_to_frontend的方法具体内容:https://www.cnblogs.com/jxzhu/p/12359692.html


-Tab Zhu 不念过去 不畏将来

 

posted @ 2020-03-01 22:01  TAB_Zhu  阅读(2107)  评论(0编辑  收藏  举报