技术开发 频道

ABAP实例:如何读取Excel文件到内表

【IT168 技术文章】

    1 定义内表

    data: gt_data type alsmex_tabline occurs 0 with header line.

    2 定义文件参数

    parameters:
      p_file like rlgrap
-filename.
 

    3定义文件帮助

    at selection-screen on value-request for p_file.
      perform select_file.  
    form select_file .
      call function
'WS_FILENAME_GET'
        exporting
          mask            
= ',Excel Files,*.xls,All Files,*.*.'(101)
          title            
= '选择文件'(100)
        importing
          filename        
= p_file
        exceptions
          inv_winsys      
= 1
          no_batch        
= 2
          selection_cancel
= 3
          selection_error  
= 4
          others          
= 5.
      
if sy-subrc <> 0 and sy-subrc <> 3.
        message e100(zdev) with
'选择文件出错!'(007).
      endif.
    endform.          

    4 读取EXCEL 文件到内表         
 

    " SELECT_FILE  

    call function
'ALSM_EXCEL_TO_INTERNAL_TABLE'
    
*倒入EXCEL数据到内表
        exporting
          filename    
= p_file
          i_begin_col
= '1'
          i_begin_row
= '1'
          i_end_col  
= '4'
          i_end_row  
= '5000'
        tables
          intern      
= gt_data.
      sort gt_data by row col value.

     
    5 数据转换
 

      loop at gt_data.
        
if g_row <> gt_data-row and sy-tabix > 1 .
          append gt_zfi025.
          clear gt_zfi025.
        endif.
        gt_zfi025
-mandt = sy-mandt.
        
case gt_data-col.
          when
1.  gt_zfi025-bukrs = gt_data-value.
          when
2.  gt_zfi025-bzirk = gt_data-value.
          when
3.  gt_zfi025-vkbur = gt_data-value.
          when
4.  gt_zfi025-money = gt_data-value.
        endcase.
        g_row
= gt_data-row.
        clear gt_data.
      endloop.
0
相关文章