1. 程式人生 > >SAP EXCEL匯入到內表

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