通用的excel导入函数,解决WPS无法导入SAP的问题

FORM upload_file.
  DATA: l_file       TYPE string,
        rawtab(4096) TYPE c OCCURS 0,
        p_pfilename  TYPE rlgrap-filename,
        ws_ext(3)    TYPE c,
        ws_ext1(4)   TYPE c,
        length1      TYPE i,
        length2      TYPE i,
        length       TYPE i.

  DATA: lt_data TYPE alsmex_tabline OCCURS 0 WITH HEADER LINE.
  DATA: ls_data TYPE alsmex_tabline.

  CONDENSE p_pfile.
  length = strlen( p_pfile ).
  IF length < 3.
    MESSAGE '请选择正确的文件!' TYPE 'I'.
    LEAVE LIST-PROCESSING.

  ENDIF.




  length1 = length - 3.
  ws_ext = p_pfile+length1(3).
  TRANSLATE ws_ext TO UPPER CASE.
  length2 = length - 4.
  ws_ext1 = p_pfile+length2(4).
  TRANSLATE ws_ext1 TO UPPER CASE.



  IF ws_ext = 'XLS' OR ws_ext1 = 'XLSX'.
    p_pfilename = p_pfile.

    CALL FUNCTION 'ALSM_EXCEL_TO_INTERNAL_TABLE'    "读取excel文件中的内容
      EXPORTING
        filename    = p_pfilename
        i_begin_col = '1'
        i_begin_row = '1'
        i_end_col   = '50'
        i_end_row   = '5000'
      TABLES
        intern      = lt_data[].
    DELETE lt_data WHERE row EQ 1.
    FIELD-SYMBOLS: .

    LOOP AT lt_data INTO ls_data.

      ASSIGN COMPONENT ls_data-col OF STRUCTURE gs_template TO .

      "动态方法将值传到相应的内表
       = ls_data-value.

      AT END OF row.
        APPEND gs_template  TO gt_template.
        CLEAR: gs_template.
      ENDAT.
    ENDLOOP.

  ENDIF.
*  ELSEIF WS_EXT = 'TXT'.
*    P_PFILENAME = P_PFILE.
*    CALL FUNCTION 'WS_UPLOAD'
*      EXPORTING
*        FILENAME                = P_PFILENAME
*        FILETYPE                = 'DAT'
*      TABLES
*        DATA_TAB                = GT_TEMPLATE[]
*      EXCEPTIONS
*        CONVERSION_ERROR        = 1
*        FILE_OPEN_ERROR         = 2
*        FILE_READ_ERROR         = 3
*        INVALID_TYPE            = 4
*        NO_BATCH                = 5
*        UNKNOWN_ERROR           = 6
*        INVALID_TABLE_WIDTH     = 7
*        GUI_REFUSE_FILETRANSFER = 8
*        CUSTOMER_ERROR          = 9.
*  ELSE.
*
*    MESSAGE E000(Z99) WITH
*    '文件必需是  XLS OR TXT!' '' '' ''.
*  ENDIF.


  IF gt_template[] IS INITIAL.
    MESSAGE e000(z99) WITH    '文件是空的!' '' '' ''.
  ENDIF.
  PERFORM frm_check_data."数据检查
  "MOVE-CORRESPONDING GT_HEADLIST1 TO GT_ALV.
ENDFORM.

下面这个,WPS的excel导入不了:

AT SELECTION-SCREEN ON VALUE-REQUEST FOR hdr_file.
  CALL FUNCTION 'F4_FILENAME'
    EXPORTING
      program_name  = syst-repid
      dynpro_number = syst-dynnr
      field_name    = 'HDR_FILE'
    IMPORTING
      file_name     = hdr_file.
  IF sy-subrc NE 0.
    WRITE: / 'Error'.
  ENDIF.

START-OF-SELECTION.

  DATA: lv_raw_data(4096) TYPE c OCCURS 0.
  DATA: lv_time TYPE i.

  CALL FUNCTION 'TEXT_CONVERT_XLS_TO_SAP'
    EXPORTING
      i_line_header        = 'X'
      i_tab_raw_data       = lv_raw_data
      i_filename           = hdr_file
    TABLES
      i_tab_converted_data = lt_data
    EXCEPTIONS
      conversion_failed    = 1
      OTHERS               = 2.
  IF sy-subrc <> 0.
    MESSAGE '上传数据失败' TYPE 'S' DISPLAY LIKE 'E'.
    STOP.
  ENDIF.

  IF lt_data[] IS INITIAL.
    MESSAGE '上传的文件没有数据,请检查' TYPE 'S' DISPLAY LIKE 'E'.
    STOP.
  ENDIF.

  CHECK NOT lt_data IS INITIAL.

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