SAP EXCEL导入到内表


只讲导入内表,既然可以导入内表其他还有什么问题呢,是不??

REPORT  Z_TEST_FB50.


DATA: BEGIN OF ITAB OCCURS 0,
   BLDAT LIKE BKPF-BLDAT,  "凭证日期
   WAERS LIKE BKPF-WAERS,  "公司代码 1000/2000
   BUDAT LIKE BKPF-BUDAT,  "过帐日期
   XBLNR LIKE BKPF-XBLNR,  "参照
   BKTXT LIKE BKPF-BKTXT,  "抬头文本
   BVORG LIKE BKPF-BVORG,  "往来凭证号
   HKONT LIKE BSEG-HKONT,  "科目
   SHKZG LIKE BSEG-SHKZG,  "借/贷 H/S
   WRBTR LIKE BSEG-WRBTR,  "金额
   ZUONR LIKE BSEG-ZUONR,  "项目分配编号
   SGTXT LIKE BSEG-SGTXT,  "项目文本
   KOSTL LIKE BSEG-KOSTL,  "成本中心
   PRCTR LIKE BSEG-PRCTR,  "利润中心
  END OF ITAB.

TYPE-POOLS: slis.
DATA: i_fieldcat TYPE slis_t_fieldcat_alv WITH HEADER LINE,     "slis_t_fieldcat_alv WITH HEADER LINE,slis_t_fieldcat_alv
      i_fieldcat_alv TYPE slis_t_fieldcat_alv WITH HEADER LINE,
      i_events TYPE slis_t_event,              "alv事件
      w_events LIKE LINE OF i_events,
      gd_tab_group TYPE slis_t_sp_group_alv,
*      gd_layout    TYPE lvc_s_layo,     "slis_layout_alv,
      gd_layout    TYPE  slis_layout_alv,
      usercommand TYPE slis_fieldname VALUE 'USERCOMMAND',
      gd_repid     LIKE sy-repid.

CONSTANTS:
  c_begin_row TYPE i VALUE 1,       "Beginning row of excel file
  c_begin_col TYPE i VALUE 1,       "Beginning column of excel file
  c_end_row   TYPE i VALUE 9999,    "Ending row of excel file
  c_end_col   TYPE i VALUE 55.      "Ending column of excel file


SELECTION-SCREEN BEGIN OF BLOCK b2 WITH FRAME TITLE t2.
*PARAMETERS:cc AS CHECKBOX DEFAULT 'X'.
PARAMETERS: pc_file(80).
SELECTION-SCREEN END OF BLOCK b2.

INITIALIZATION.
*  t1 = '查询条件'.
  t2 = '文件上传'.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR pc_file.
  PERFORM sub_find USING pc_file.

*&---------------------------------------------------------------------*
*&      Form  sub_find
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->PC_FILE    text
*----------------------------------------------------------------------*
FORM sub_find USING pc_file.
* Data for open dialog
  DATA: l_filetab   TYPE filetable,
        l_rc        TYPE i.
  CLEAR l_filetab.
  REFRESH l_filetab.
* Open dialog
  CALL METHOD cl_gui_frontend_services=>file_open_dialog
    EXPORTING
*      WINDOW_TITLE            = 'SAP Custom - Open File'
*      DEFAULT_EXTENSION       =
      default_filename        = '*.xls'
*      FILE_FILTER             = '*.xls'
      initial_directory       = 'd:\'
      multiselection          = ''
    CHANGING
      file_table              = l_filetab
      rc                      = l_rc
    EXCEPTIONS
      cntl_error              = 1
      error_no_gui            = 2
      not_supported_by_gui    = 3
      OTHERS                  = 4.
* Get file path
  CHECK l_rc EQ 1.
  READ TABLE l_filetab INDEX 1 INTO pc_file.
ENDFORM.


START-OF-SELECTION.

  PERFORM uploaddata.   "从PC中上传文件到内表中
 

* PERFORM layout_build. "2.用于定义ALV表单的相关格式、属性
*  PERFORM fields_build. "3.用来定义表单中的各个列的相关信息,比如列名等
*  PERFORM display_data. "4.用来显示ALV表单
END-OF-SELECTION.

*&---------------------------------------------------------------------*
*&      2.RFORM uploaddata.   "从PC中上传文件到内表中
*&---------------------------------------------------------------------*
FORM uploaddata.
  DATA: BEGIN OF i_excel OCCURS 0.
          INCLUDE STRUCTURE alsmex_tabline.
  DATA: END OF i_excel.
  DATA: l_answer(1) TYPE c.
  DATA: l_column TYPE i.
  FIELD-SYMBOLS: <fs>.
  DATA: l_pathname LIKE rlgrap-filename.

  MOVE pc_file TO l_pathname.
  CALL FUNCTION 'ALSM_EXCEL_TO_INTERNAL_TABLE'
    EXPORTING
      filename                = l_pathname
      i_begin_col             = c_begin_col
      i_begin_row             = c_begin_row
      i_end_col               = c_end_col
      i_end_row               = c_end_row
    TABLES
      intern                  = i_excel
    EXCEPTIONS
      inconsistent_parameters = 1
      upload_ole              = 2
      OTHERS                  = 3.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
          WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ELSE.
    IF i_excel[] IS INITIAL.
      CALL FUNCTION 'POPUP_TO_CONFIRM_WITH_MESSAGE'
        EXPORTING
*         DEFAULTOPTION        = 'Y'
          diagnosetext1        = 'No data in excel'
*         DIAGNOSETEXT2        = ' '
*         DIAGNOSETEXT3        = ' '
          textline1            = 'No data in excel'
*         TEXTLINE2            = ' '
          titel                = 'Confirm'
*         START_COLUMN         = 25
*         START_ROW            = 6
*         CANCEL_DISPLAY       = 'X'
        IMPORTING
          answer               = l_answer.
    ELSE.
      DELETE i_excel WHERE row = '0001'.
      SORT i_excel BY row col.
      LOOP AT i_excel.
        MOVE i_excel-col TO l_column.
        ASSIGN COMPONENT l_column OF STRUCTURE itab TO <fs>.
        MOVE i_excel-value TO <fs>.
        AT END OF row.
          APPEND itab.
          CLEAR itab.
        ENDAT.
      ENDLOOP.
    ENDIF.
  ENDIF.
ENDFORM.                    "UPLOADDATA




智能推荐

注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告