메뉴 건너뛰기

SAP 한국 커뮤니티

[re] 엑셀 MUTIPLE SHEET UPLOAD SAMPLE

슝슝이 2008.08.22 11:03 조회 수 : 3471 추천:10

CODE BY 슝슝이 
THANKS TO MadMax
----------------------------------------


 


REPORT EXCEL_MUTIPLE_SHEET_UPLOAD_TO_INTERNAL_TABLE NO STANDARD PAGE HEADING.
* this report demonstrates how to send some ABAP data to an
* EXCEL sheet using OLE automation.
TYPE-POOLS: ole2.
**TYPE UPLOAD TABLE
TYPES: BEGIN OF ZLSMEX_TABLINE,
          row TYPE KCD_EX_ROW_N,
          COL TYPE KCD_EX_COL_N,
          VALUE(500),
       END OF ZLSMEX_TABLINE.
DATA gt_upload  type TABLE OF ZLSMEX_TABLINE WITH HEADER LINE.


*      value of excel-cell
TYPES: ty_d_itabvalue             TYPE ZLSMEX_TABLINE-value,
*      internal table containing the excel data
       ty_t_itab                  TYPE ZLSMEX_TABLINE   OCCURS 0,
*      line type of sender table
       BEGIN OF ty_s_senderline,
         line(4096)               TYPE c,
       END OF ty_s_senderline,
*      sender table
       ty_t_sender                TYPE ty_s_senderline  OCCURS 0.
CONSTANTS:  gc_esc              VALUE '"'.
* handles for OLE objects
DATA: H_EXCEL       TYPE OLE2_OBJECT,        " Excel object
      workbooks     TYPE OLE2_OBJECT,         " list of workbooks
      workbook      TYPE OLE2_OBJECT,          " workbook
      worksheet     TYPE ole2_object,
      H_CELL        TYPE OLE2_OBJECT,           " cell
      H_F           TYPE OLE2_OBJECT.            " font


DATA  H TYPE I.
*----------------------------------------------------------------------*
* Type Declaration
*----------------------------------------------------------------------*
TYPE-POOLS: slis, truxs.



PARAMETERS: p_file LIKE rlgrap-filename OBLIGATORY.
DEFINE m_message.
  case sy-subrc.
    when 0.
    when 1.
      message id sy-msgid type sy-msgty number sy-msgno
              with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
    when others. raise upload_ole.
  endcase.
END-OF-DEFINITION.
*&---------------------------------------------------------------------*
*&      Form  SEPARATED_TO_INTERN_CONVERT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM separated_to_intern_convert TABLES i_tab       TYPE ty_t_sender
                                        i_intern    TYPE ty_t_itab
                                 USING  i_separator TYPE c.
  DATA: l_sic_tabix LIKE sy-tabix,
        l_sic_col   TYPE kcd_ex_col.
  DATA: l_fdpos     LIKE sy-fdpos.


*  REFRESH i_intern.


  LOOP AT i_tab.
    l_sic_tabix = sy-tabix.
    l_sic_col = 0.
    WHILE i_tab CA i_separator.
      l_fdpos = sy-fdpos.
      l_sic_col = l_sic_col + 1.
      PERFORM line_to_cell_separat TABLES i_intern
                                   USING  i_tab l_sic_tabix l_sic_col
                                          i_separator l_fdpos.
    ENDWHILE.
    IF i_tab <> space.
      CLEAR i_intern.
      i_intern-row = l_sic_tabix.
      i_intern-col = l_sic_col + 1.
      i_intern-value = i_tab.
      APPEND i_intern.
    ENDIF.
  ENDLOOP.
ENDFORM.                    " SEPARATED_TO_INTERN_CONVERT
*---------------------------------------------------------------------*
FORM line_to_cell_separat TABLES i_intern    TYPE ty_t_itab
                          USING  i_line
                                 i_row       LIKE sy-tabix
                                 ch_cell_col TYPE kcd_ex_col
                                 i_separator TYPE c
                                 i_fdpos     LIKE sy-fdpos.
  DATA: l_string   TYPE ty_s_senderline.
  DATA  l_sic_int  TYPE i.


  CLEAR i_intern.
  l_sic_int = i_fdpos.
  i_intern-row = i_row.
  l_string = i_line.
  i_intern-col = ch_cell_col.
* csv Dateien mit separator in Zelle: --> ;"abc;cd";
  IF ( i_separator = ';' OR  i_separator = ',' ) AND
       l_string(1) = gc_esc.
    PERFORM line_to_cell_esc_sep USING l_string
                                       l_sic_int
                                       i_separator
                                       i_intern-value.
  ELSE.
    IF l_sic_int > 0.
      i_intern-value = i_line(l_sic_int).
    ENDIF.
  ENDIF.
  IF l_sic_int > 0.
    APPEND i_intern.
  ENDIF.
  l_sic_int = l_sic_int + 1.
  i_line = i_line+l_sic_int.
ENDFORM.                    "line_to_cell_separat


*---------------------------------------------------------------------*
FORM line_to_cell_esc_sep USING i_string
                                i_sic_int      TYPE i
                                i_separator    TYPE c
                                i_intern_value TYPE ty_d_itabvalue.
  DATA: l_int TYPE i,
        l_cell_end(2).
  FIELD-SYMBOLS: .
  l_cell_end = gc_esc.
  l_cell_end+1 = i_separator .


  IF i_string CS gc_esc.
    i_string = i_string+1.
    IF i_string CS l_cell_end.
      l_int = sy-fdpos.
      ASSIGN i_string(l_int) TO .
      i_intern_value = .
      l_int = l_int + 2.
      i_sic_int = l_int.
      i_string = i_string+l_int.
    ELSEIF i_string CS gc_esc.
*     letzte Celle
      l_int = sy-fdpos.
      ASSIGN i_string(l_int) TO .
      i_intern_value = .
      l_int = l_int + 1.
      i_sic_int = l_int.
      i_string = i_string+l_int.
      l_int = strlen( i_string ).
      IF l_int > 0 . MESSAGE x001(kx) . ENDIF.
    ELSE.
      MESSAGE x001(kx) . "was ist mit csv-Format
    ENDIF.
  ENDIF.
ENDFORM.                    "line_to_cell_esc_sep
*&---------------------------------------------------------------------*
*&      Form  read_excel
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM zalsm_excel_to_internal_table
    TABLES
      INTERN  LIKE  gt_upload[]
    USING
       VALUE(FILENAME) LIKE  RLGRAP-FILENAME
       VALUE(I_BEGIN_COL) TYPE  I
       VALUE(I_BEGIN_ROW) TYPE  I
       VALUE(I_END_COL) TYPE  I
       VALUE(I_END_ROW) TYPE  I.


  DATA: excel_tab     TYPE  ty_t_sender.
  DATA: ld_separator  TYPE  c.
  DATA: application   TYPE  ole2_object,
        workbook      TYPE  ole2_object,
        range         TYPE  ole2_object,
        worksheet     TYPE  ole2_object.
  DATA: h_cell        TYPE  ole2_object,
        h_cell1       TYPE  ole2_object.
  DATA:
    ld_rc             TYPE i,
    sheet_per         type i,
    cnt               type i.


*   Ruckgabewert der Methode "clipboard_export     "


* Makro fur Fehlerbehandlung der Methods
  sheet_per = 2.
  cnt = 1.
  REFRESH excel_tab.
  do sheet_per times.


* check parameters
    IF i_begin_row > i_end_row. RAISE inconsistent_parameters. ENDIF.
    IF i_begin_col > i_end_col. RAISE inconsistent_parameters. ENDIF.


* Get TAB-sign for separation of fields
    CLASS cl_abap_char_utilities DEFINITION LOAD.
    ld_separator = cl_abap_char_utilities=>horizontal_tab.


* open file in Excel
    IF application-header = space OR application-handle = -1.
      CREATE OBJECT application 'Excel.Application'.
      m_message.
    ENDIF.
    CALL METHOD OF application 'Workbooks' = workbook.
    m_message.
    CALL METHOD OF workbook 'Open' EXPORTING #1 = filename.
    m_message.
  set property of application 'Visible' = 1.
  m_message.


    CALL METHOD OF application 'Worksheets' = worksheet EXPORTING #1 = cnt.
    m_message.


    CALL METHOD OF worksheet 'Activate'.
    m_message.


    GET PROPERTY OF  application 'ACTIVESHEET' = worksheet.
    m_message.


* mark whole spread sheet
    CALL METHOD OF worksheet 'Cells' = h_cell
      EXPORTING #1 = i_begin_row #2 = i_begin_col.
    m_message.
    CALL METHOD OF worksheet 'Cells' = h_cell1
      EXPORTING #1 = i_end_row #2 = i_end_col.
    m_message.


    CALL METHOD OF worksheet 'RANGE' = range
      EXPORTING #1 = h_cell #2 = h_cell1.
    m_message.
    CALL METHOD OF range 'SELECT'.
    m_message.


* copy marked area (whole spread sheet) into Clippboard
    CALL METHOD OF range 'COPY'.
    m_message.


* read clipboard into ABAP
    CALL METHOD cl_gui_frontend_services=>clipboard_import
      IMPORTING
        data                 = excel_tab
      EXCEPTIONS
        cntl_error           = 1
*      ERROR_NO_GUI         = 2
*      NOT_SUPPORTED_BY_GUI = 3
        OTHERS               = 4
            .
    IF sy-subrc <> 0.
      MESSAGE a037(alsmex).
    ENDIF.


    PERFORM separated_to_intern_convert TABLES excel_tab intern
                                        USING  ld_separator.
* clear clipboard
*    REFRESH excel_tab.
    CALL METHOD cl_gui_frontend_services=>clipboard_export
       IMPORTING
          data                 = excel_tab
       CHANGING
          rc                   = ld_rc
       EXCEPTIONS
          cntl_error           = 1
*       ERROR_NO_GUI         = 2
*       NOT_SUPPORTED_BY_GUI = 3
          OTHERS               = 4
            .


* quit Excel and free ABAP Object - unfortunately, this does not kill
* the Excel process
    CALL METHOD OF application 'QUIT'.
    m_message.


* >>>>> Begin of change note 575877
* to kill the Excel process it's necessary to free all used objects
    FREE OBJECT h_cell.       m_message.
    FREE OBJECT h_cell1.      m_message.
    FREE OBJECT range.        m_message.
    FREE OBJECT worksheet.    m_message.
    FREE OBJECT workbook.     m_message.
    FREE OBJECT application.  m_message.
* <<<<< End of change note 575877
    cnt = cnt + 1.
  enddo.
ENDForm.                    "zalsm_excel_to_internal_table


*&---------------------------------------------------------------------*
*&   Event START-OF-SELECTION
*&---------------------------------------------------------------------*
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
  CALL FUNCTION 'KD_GET_FILENAME_ON_F4'
    EXPORTING
      mask      = ',Microsoft Excel Files,*.xls'
      static    = 'X'
    CHANGING
      file_name = p_file.
*&---------------------------------------------------------------------*
*&   Event START-OF-SELECTION
*&---------------------------------------------------------------------*
START-OF-SELECTION.
  PERFORM ZALSM_EXCEL_TO_INTERNAL_TABLE
              TABLES
                 gt_upload
              USING
                  p_file
                  1
                  1
                  9
                  5000.
  LOOP AT GT_UPLOAD.
    WRITE GT_UPLOAD-VALUE.
  ENDLOOP.


번호 제목 글쓴이 날짜 조회 수
4349 <b>[완료]</b>3개 이상 테이블 조인 관련해서 추가 질문입니다^^ [2] 아밥초보입니다 2008.08.21 2242
4348 [요청]printer 출력 시 한글 깨짐 현상,,, [4] darkangel 2008.08.21 3677
4347 <b>[완료]</b>ALV 더블클릭값을 이용하여 data 처리후 화면에 결과를 찍을려는데.. [3] 보경아빠 2008.08.21 1955
4346 <b>[완료]</b>숫자 컨버전하려구하는데요...123,456 -> 123,000 [6] CoolGuy 2008.08.21 1336
4345 [요청]SELECT-OPTIONS의 날짜입력화면에서 Auto check !? [4] file seph 2008.08.22 2094
4344 [요청]function만들때 import값을 select-options로 할려고하는데.. 문의요.. [3] 금은돌 2008.08.22 1912
4343 <b>[완료]</b>현재 PC 의 해상도를 읽어올수 있는 펑션이나 메소드가 있을까요? [2] 으..2MB~떠나야하나 2008.08.22 1231
4342 [요청]alv 체크박스 관련 질문입니다.. [2] 무한반복 2008.08.22 1263
» [re] 엑셀 MUTIPLE SHEET UPLOAD SAMPLE 슝슝이 2008.08.22 3471
4340 [요청]간단한 FIELD-SYMBOLS 사용하려는데요.... [7] CoolGuy 2008.08.22 1319
4339 [요청]ALV 선택라인에 대한 상세조회 팝업창 띄우려고 합니다.. [2] 다급 2008.08.22 1830
4338 <b>[완료]</b>함수 이용한 ALV 에서 행추가해서 데이터를 입력 저장하는 로직을 알려주세요 [2] Norton 2008.08.22 2193
4337 <b>[완료]</b>[re] gui_upload로 엑셀 upload 질문입니다. [3] 띵호아빠 2008.08.24 4408
4336 <b>[완료]</b>HR 에서 PP02를 이용해 Cost변경을 BDC로 할려고 하는데, 직접함 되는데 BDC로는안될때(BDC에서 팝업을 띄울수 있는 방법은?) [3] sagar 2008.08.25 1010
4335 <b>[완료]</b>ps 관련 테이블에 관한 질문입니다 [6] 아밥어렵네요 2008.08.25 1564
4334 [요청]at first, at last, at new, at end 정확한 개념좀 갈켜주세여.. 휴... [5] 아밥입문자 2008.08.25 9032
4333 <b>[완료]</b>method 에서 -> => 의미 좀 알려주세요 [3] 삶은달걀과사이다 2008.08.25 1998
4332 [요청]ALV 에서... 처음과 다른 인터널테이블를 이용한 화면 출력... [3] seph 2008.08.25 1078
4331 <b>[완료]</b>엑셀양식으로 다운 받을 때, 문서형식을 설정하는 법이 궁금합니다. [3] 아밥초보입니다 2008.08.26 1933
4330 [요청]Table control 에서의 질문입니다 [2] 아밥어렵네요 2008.08.26 1228