Search This Blog

Tuesday, August 02, 2011

Break down a file to deal with excel upload function row limitation

report ZZFLOTH_TEMP.

DATA: BEGIN OF file_chunks OCCURS 0,
        file LIKE rlgrap-filename,
        rowb TYPE i,
        rowe TYPE i,
      END OF file_chunks.

SELECTION-SCREEN BEGIN OF BLOCK block1
           WITH FRAME TITLE text-001.
PARAMETERS: p_bdc   LIKE apqi-groupid OBLIGATORY
                    DEFAULT 'ZB01yyyymmdd',
            p_file  LIKE rlgrap-filename OBLIGATORY
                    DEFAULT 'c:\exclusions.xls',
            p_row_bg  TYPE i OBLIGATORY,
            p_recs TYPE i DEFAULT 20000 OBLIGATORY.
SELECTION-SCREEN END OF BLOCK block1.

START-OF-SELECTION.
** Import XLS
  perform read_xls.
  end-of-SELECTION.

*&---------------------------------------------------------------------*
*&      Form  read_xls
*&---------------------------------------------------------------------*
*       read xls file
*----------------------------------------------------------------------*
FORM read_xls .
  DATA:  xls LIKE alsmex_tabline OCCURS 0 WITH HEADER LINE.

* Break input files into chunks of 9000 rows.  The function can
* only handle 9999 rows.
  REFRESH file_chunks.
  PERFORM create_file_chunks USING p_file p_row_bg p_recs.

  LOOP AT file_chunks.
    REFRESH xls.
    IF NOT file_chunks-file IS INITIAL.

      CALL FUNCTION 'ALSM_EXCEL_TO_INTERNAL_TABLE'
        EXPORTING
          filename    = file_chunks-file
          i_begin_col = 1
          i_begin_row = file_chunks-rowb
          i_end_col   = 2
          i_end_row   = file_chunks-rowe
        TABLES
          intern      = xls
        EXCEPTIONS
          OTHERS      = 3.
      IF sy-subrc <> 0.
        MESSAGE e212(zz) WITH p_file.
      ENDIF.
    ENDIF.

    SORT xls BY row col.

    LOOP AT xls.

      CASE xls-col.
        WHEN 1.
          "record-kunnr  = xls-value.
        WHEN 2.
          "record-ktokd  = xls-value.
        WHEN OTHERS.
      ENDCASE.

      AT END OF row.
        "APPEND record.
        "CLEAR record.
      ENDAT.

    ENDLOOP.
  ENDLOOP.

ENDFORM.                    " read_xls

*&---------------------------------------------------------------------*
*&      Form  create_file_chunks
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*       Determine the file chunks that will be read.  No more than
*       9000 records at a time.
*----------------------------------------------------------------------*
FORM create_file_chunks USING file
                              rowb
                              rowe.
  DATA: chunks TYPE i,
        last TYPE i,
        rowbegin TYPE i.

  CLEAR file_chunks.

  CHECK NOT file IS INITIAL.

  file_chunks-file = file.
  rowbegin = rowb.

* Culculate the full chunks of 9000.
  chunks = rowe DIV 9000.
* Calculate the size of the last chunk.
  last = rowe MOD 9000.

* Add the full 9000 chunks
  DO chunks TIMES.
    file_chunks-rowb = rowbegin.
    ADD 9000 TO rowbegin.
* Adjust to account for the  first row.
    file_chunks-rowe = rowbegin - 1.
    APPEND file_chunks.
  ENDDO.

* Add the pertial chunk.
  IF NOT last IS INITIAL.
    file_chunks-rowb = rowbegin.
    ADD last TO rowbegin.
* Adjust to account for the  first row.
    file_chunks-rowe = rowbegin - 1.
    APPEND file_chunks.
  ENDIF.
ENDFORM.                    " create_file_chunks

No comments: