ABAP READ EXCEL & SHOW ALV SCREEN


*&---------------------------------------------------------------------*
*& Report ZCEM_READ_EXCEL2
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT zcem_read_excel2.
*** Data Declarations ***
TABLESzcem_table4.
DATAls_out type zcem_table4.
TYPE-POOLSslis,

                        ole2.


* zalsmex_tabline is a structure with 3 fields:

*  ROW         Types        KCD_EX_ROW_N

*  COL          Types        KCD_EX_COL_N

*  VALUE      Types        CHAR50

TYPESty_d_itabvalue TYPE zalsmex_tabline-value,

*  internal table containing the excel data

       ty_t_itab      TYPE STANDARD TABLE OF zalsmex_tabline,

*  line type of sender table

       BEGIN OF ty_s_senderline,

         line(4096TYPE c,

       END OF ty_s_senderline,

*  sender table

       ty_t_sender TYPE STANDARD TABLE OF ty_s_senderline.


CONSTANTS:  gc_esc  VALUE '"'.


TYPESBEGIN OF lty_table,

         col1 TYPE  zcem_table4-col1,
         col2 TYPE  zcem_table4-col2,
         col3 TYPE  zcem_table4-col3,
         col4 TYPE  zcem_table4-col4,
         col5 TYPE  zcem_table4-col5,
         col6 TYPE  zcem_table4-col6,
         col7 TYPE  zcem_table4-col7,
         col8 TYPE  zcem_table4-col8,
         col9 TYPE  zcem_table4-col9,

       END OF lty_table.


DATA:

  lt_fieldcatalog TYPE slis_t_fieldcat_alv WITH HEADER LINE,

  ls_gd_layout    TYPE slis_layout_alv,

  lt_itab_table   TYPE STANDARD TABLE OF lty_table,

  ls_itab_table   TYPE lty_table.


DATA:

  excel_tab    TYPE  ty_t_sender,

  intern       TYPE STANDARD TABLE OF zalsmex_tabline,

  ls_excel_tab TYPE ty_s_senderline,

  ls_string    TYPE string,

  ld_separator TYPE  c,

  application  TYPE  ole2_object,

  workbook     TYPE  ole2_object,

  range        TYPE  ole2_object,

  worksheet    TYPE  ole2_object,

  h_cell       TYPE  ole2_object

  h_cell1      TYPE  ole2_object,

  ld_rc        TYPE i,

  lv_filename  TYPE rlgrap-filename.


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.

        MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno

           WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.

  ENDCASE.

END-OF-DEFINITION.


PARAMETERS:

  p_file   TYPE rlgrap-filename DEFAULT 'C:\Users\cem.akmermer\Desktop\kteam.xlsx' OBLIGATORY,      "Filename

  p_row_st TYPE DEFAULT OBLIGATORY,       "1st Row

  p_row_en TYPE DEFAULT 100 OBLIGATORY"Last Row

  p_col_st TYPE DEFAULT OBLIGATORY,        "1st Column

  p_col_en TYPE DEFAULT OBLIGATORY.      "Last Column


AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.

  CALL FUNCTION 'KD_GET_FILENAME_ON_F4'
    EXPORTING
      program_name  syst-repid
      dynpro_number syst-dynnr
    CHANGING
      file_name     p_file
    EXCEPTIONS
      mask_too_long 1
      OTHERS        2.


  IF sy-subrc <> 0.

    MESSAGE e999(b1WITH 'Issue with getting filename:' sy-subrc.

  ENDIF.


START-OF-SELECTION.

* 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.

  lv_filename p_file.

  CALL METHOD OF workbook 'Open' EXPORTING #1 lv_filename.

  m_message.

  GET PROPERTY OF  application 'ACTIVESHEET' worksheet.

  m_message.


* Get top left cell

  CALL METHOD OF worksheet 'Cells' h_cell

      EXPORTING #1 p_row_st #2 p_col_st.

  m_message.


* Get bottom right cell

  CALL METHOD OF worksheet 'Cells' h_cell1

      EXPORTING #1 p_row_en #2 p_col_en.

  m_message.


* Mark range from top left to bottom right

  CALL METHOD  OF worksheet 'RANGE' range

      EXPORTING #1 h_cell #2 h_cell1.

  m_message.


* Select range

  CALL METHOD OF range 'SELECT'.

  m_message.


* copy range into Clipboard

  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 e999(b1WITH 'Issue importing clipboard:' sy-subrc.

  ENDIF.


  PERFORM convert_excel TABLES excel_tab intern

      USING  ld_separator.


  IF excel_tab[] IS NOT INITIAL.

    CLEARlt_itab_table[].

    LOOP AT excel_tab INTO ls_excel_tab.

      ls_string ls_excel_tab.

      SPLIT ls_string AT ld_separator

          INTO ls_itab_table-col1 ls_itab_table-col2 ls_itab_table-col3  ls_itab_table-col4 ls_itab_table-col5 ls_itab_table-col6 ls_itab_table-col7 ls_itab_table-col8 ls_itab_table-col9.

      "Hard-coded for easy reading - Make dynamic

      APPEND ls_itab_table TO lt_itab_table.

      CLEARls_itab_table.

    ENDLOOP.
*    LOOP AT lt_itab_table ASSIGNING FIELD-SYMBOL(<fs_itab>).
*              <fs_itab>-col1 = ls_out-col1.
*              <fs_itab>-col2 = ls_out-col2.
*              <fs_itab>-col3 = ls_out-col3.
*              <fs_itab>-col4 = ls_out-col4.
*              <fs_itab>-col5 = ls_out-col5.
*              <fs_itab>-col6 = ls_out-col6.
*              <fs_itab>-col7 = ls_out-col7.
*              <fs_itab>-col8 = ls_out-col8.
*              <fs_itab>-col9 = ls_out-col9.
*              APPEND ls_out to lt_itab_table.
*              modify zcem_table4 from ls_out.
*      ENDLOOP.
  ENDIF.


* 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.


END-OF-SELECTION.


*** Main program

  IF lt_itab_table IS NOT INITIAL.

    REFRESH lt_fieldcatalog.

    CLEAR lt_fieldcatalog.

    lt_fieldcatalog-fieldname  'Col1'.

    lt_fieldcatalog-seltext_m  'Malzeme'.

    lt_fieldcatalog-col_pos  1.

    APPEND lt_fieldcatalog.

    CLEAR  lt_fieldcatalog.


    lt_fieldcatalog-fieldname  'Col2'.

    lt_fieldcatalog-seltext_m  'Malzeme kısa metni'.

    lt_fieldcatalog-col_pos  2.

    APPEND lt_fieldcatalog.

    CLEAR  lt_fieldcatalog.


    lt_fieldcatalog-fieldname  'Col3'.

    lt_fieldcatalog-seltext_m  'Hammadde'.

    lt_fieldcatalog-col_pos  3.

    APPEND lt_fieldcatalog.

    CLEAR  lt_fieldcatalog.

    lt_fieldcatalog-fieldname  'Col4'.

    lt_fieldcatalog-seltext_m  'BOM Girdi (kg)'.

    lt_fieldcatalog-col_pos  4.

    APPEND lt_fieldcatalog.

    CLEAR  lt_fieldcatalog.

    lt_fieldcatalog-fieldname  'Col5'.

    lt_fieldcatalog-seltext_m  'Averaj Reel Kütle (kg)'.

    lt_fieldcatalog-col_pos  5.

    APPEND lt_fieldcatalog.

    CLEAR  lt_fieldcatalog.

    lt_fieldcatalog-fieldname  'Col6'.

    lt_fieldcatalog-seltext_m  'Delta Kütle (kg)'.

    lt_fieldcatalog-col_pos  6.

    APPEND lt_fieldcatalog.

    CLEAR  lt_fieldcatalog.

    lt_fieldcatalog-fieldname  'Col7'.

    lt_fieldcatalog-seltext_m  'Birim Maliyet (Euro/kg)'.

    lt_fieldcatalog-col_pos  7.

    APPEND lt_fieldcatalog.

    CLEAR  lt_fieldcatalog.

    lt_fieldcatalog-fieldname  'Col8'.

    lt_fieldcatalog-seltext_m  'Tüketilen Adet'.

    lt_fieldcatalog-col_pos  8.

    APPEND lt_fieldcatalog.

    CLEAR  lt_fieldcatalog.

    lt_fieldcatalog-fieldname  'Col9'.

    lt_fieldcatalog-seltext_m  'Br x Delta x TükAd'.

    lt_fieldcatalog-col_pos  9.

    APPEND lt_fieldcatalog.

    CLEAR  lt_fieldcatalog.


    CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
      EXPORTING
        i_callback_program sy-repid
        is_layout          ls_gd_layout
        it_fieldcat        lt_fieldcatalog[]
      TABLES
        t_outtab           lt_itab_table
      EXCEPTIONS
        program_error      1
        OTHERS             2.

    IF sy-subrc NE  0.

      WRITE:/ sy-subrc.

    ENDIF.

* quit excel and free ABAP Object - unfortunately, this does not kill the excel process

    CALL METHOD OF application 'QUIT'.

    m_message.

* 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.

  ENDIF.


*&---------------------------------------------------------------------*

*&  Form  CONVERT_EXCEL

*&---------------------------------------------------------------------*

FORM convert_excel 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,

    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.                          "#EC CI_NESTED

      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.


*&---------------------------------------------------------------------*

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.


  DATAl_string  TYPE ty_s_senderline,

        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.


  IF i_separator ';' OR  i_separator ',' AND l_string(1gc_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.


*&---------------------------------------------------------------------*

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>.


  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_intTO <l_cell>.

      i_intern_value <l_cell>.

      l_int l_int + 2.

      i_sic_int l_int.

      i_string i_string+l_int.

    ELSEIF i_string CS gc_esc.

      l_int sy-fdpos.

      ASSIGN i_string(l_intTO <l_cell>.

      i_intern_value <l_cell>.

      l_int l_int + 1.

      i_sic_int l_int.

      i_string i_string+l_int.

      l_int strleni_string ).

      IF l_int > .

        MESSAGE x001(zapo.

      ENDIF.

    ELSE.

      MESSAGE x001(zapo.

    ENDIF.

  ENDIF.

ENDFORM.

Yorumlar

Bu blogdaki popüler yayınlar

Transaction Code assignment for Query created in SQVI - SQVI Raporu Tcode Bağlanması