DOWNLOAD FROM ITAB TO EXCEL


 先把模板用SMW0存到系统中,名字为 ZLJC001.

*&---------------------------------------------------------------------*
*& Report  ZLJC
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

 REPORT  zljc   .

 INCLUDE <icon>.
 INCLUDE <symbol>.
 INCLUDE z_alv_incl_001.
 INCLUDE z_file_incl_001.



 DATA : im_ifile    TYPE ibipparms-path,
        im_startrow TYPE i,
        ex_tag      TYPE zsubrc,
        ex_msg      TYPE zyxmsg.



 DATA: gc_objid TYPE wwwdata-objid VALUE 'ZLJC001'.


 DATA: ldf_filename TYPE text255,
       ls_fieldcat  TYPE zexcel_s_fieldcat,
       lt_fieldcat  TYPE zexcel_t_fieldcat.
 DATA: ldf_count    TYPE i,
       ldf_row      TYPE i.



 TYPESBEGIN OF ty_data,
           matnr TYPE mara-matnr,
           groes TYPE mara-groes,
        END OF ty_data.
 DATA: lt_data      TYPE TABLE OF ty_data WITH HEADER LINE.


 START-OF-SELECTION.


   PERFORM prepare_file.



   PERFORM frm_download_file.

* 打开文件
   CALL FUNCTION 'ZEXCEL_START'
     EXPORTING
       filename = ldf_filename
     IMPORTING
       subrc    = ex_tag
       msg      = ex_msg.

   CHECK ex_tag = 0.


   PERFORM prepare_data.

   ldf_row = 2.
   CALL FUNCTION 'ZEXCEL_FILL_ITAB'
     EXPORTING
       row        = ldf_row
       column     = 2
       fieldcat   = lt_fieldcat
       autoheader = 0
     IMPORTING
       subrc      = ex_tag
       msg        = ex_msg
     TABLES
       itab       = lt_data.


   CALL FUNCTION 'ZEXCEL_END'
     EXPORTING
       visible = 0
       saves   = 1
       close   = 1
     IMPORTING
       subrc   = ex_tag
       msg     = ex_msg.

IF EX_TAG IS INITIAL.
   MESSAGE I000(26WITH ' DOWNLOAD OK'.
ELSE.
   MESSAGE I000(26WITH ' DOWNLOAD ERROR'.
ENDIF.


*&---------------------------------------------------------------------*
*&      Form  PREPARE_FILE
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
 FORM prepare_file .
   DATA : lfiletable TYPE filetable.
   DATA : ls_file TYPE file_table.
   DATA : l_rc TYPE i.
   CALL METHOD cl_gui_frontend_services=>file_open_dialog
     EXPORTING
       window_title            = 'select open file'
       default_extension       = '*.XLS'
       default_filename        = 'LJC.XLS'
     CHANGING
       file_table              = lfiletable
       rc                      = l_rc
     EXCEPTIONS
       file_open_dialog_failed = 1
       cntl_error              = 2
       error_no_gui            = 3
       not_supported_by_gui    = 4
       OTHERS                  = 5.



   READ TABLE lfiletable INTO ls_file INDEX 1.
   im_ifile = ls_file-filename.


   MOVE im_ifile TO ldf_filename  .
 ENDFORM.                    " PREPARE_FILE


*&---------------------------------------------------------------------*
*&      Form  frm_download_file
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
 FORM frm_download_file .

   CALL FUNCTION 'ZTOOL_FILE_DOWNLOAD_SMW0'
     EXPORTING
       objname           = gc_objid
       pathname          = im_ifile
     IMPORTING
       filename          = im_ifile
     EXCEPTIONS
       objname_not_exist = 1
       inv_winsys        = 2
       no_batch          = 3
       selection_cancel  = 4
       selection_error   = 5
       download_false    = 6
       OTHERS            = 7.
   CASE sy-subrc.
     WHEN 0.

     WHEN 1.

     WHEN 4.
     WHEN 6.

     WHEN OTHERS.

   ENDCASE.

 ENDFORM.                    " FRM_DOWNLOAD_FILE
*&---------------------------------------------------------------------*
*&      Form  PREPARE_DATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
 FORM prepare_data .

   CLEAR: ls_fieldcat, lt_fieldcat, lt_fieldcat[].
   ls_fieldcat-fname   = 'MATNR'.
   ls_fieldcat-coltext = '物料号'.
   APPEND ls_fieldcat TO lt_fieldcat.

   ls_fieldcat-fname   = 'GROES'.
   ls_fieldcat-coltext = '规格'.
   APPEND ls_fieldcat TO lt_fieldcat.



   CLEAR: lt_data,lt_data[].
   SELECT matnr groes INTO TABLE lt_data FROM mara UP TO 10 ROWS.

 ENDFORM.                    " PREPARE_DATA

你可能感兴趣的:(Excel,download)