SAP ABAP 多个sheet 上传下载改造函数ALSM_EXCEL_TO_INTERNAL_TABLE

 复制一份更改如下调用

FUNCTION ALSM_EXCEL_TO_INTERNAL_TABLE1 .
*"----------------------------------------------------------------------
*"*"本地接口:
*"  IMPORTING
*"     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
*"     REFERENCE(SHEET_NAME) LIKE  ALSMEX_TABLINE-VALUE
*"  TABLES
*"      INTERN STRUCTURE  ALSMEX_TABLINE
*"  EXCEPTIONS
*"      INCONSISTENT_PARAMETERS
*"      UPLOAD_OLE
*"----------------------------------------------------------------------
*{   INSERT         S4DK900341                                        1

  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.
*   Rückgabewert der Methode "clipboard_export     "

* Makro für Fehlerbehandlung der Methods
  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.


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

*--ADD HOUZHIYU
**  set property of application 'Visible' = 1.
**  m_message.
  if SHEET_NAME = SPACE."用默认模式
    get property of  APPLICATION 'ACTIVESHEET' = WORKSHEET.
    M_MESSAGE.
  else.
*-->可以实现读取多个sheet
    call method of APPLICATION 'WORKSHEETS' = WORKSHEET
      exporting
        #1 = SHEET_NAME.

    call method of WORKSHEET 'Activate'.
    M_MESSAGE.
  endif.
*---END OF------------------------------------------------


* 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
*}   INSERT
ENDFUNCTION.

你可能感兴趣的:(ABAP,excel)