Temos várias formas de ler XLSX no ABAP. Segue um exemplo bem completo sobre esse assunto:


*&---------------------------------------------------------------------*
*& Report YXEIT_TNFCI_UPLOAD
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
report yxeit_tnfci_upload.

field-symbols <gt_data>       type standard table .

selection-screen begin of block b1 with frame title g_title.
  
selection-screen comment  /1(78g_c01.
  
selection-screen begin of line.

    
parameters p_file type string obligatory.
  
selection-screen end of line.
selection-screen end of block b1 .

initialization.
  g_title 
'Selection'.
  g_c01 
'File to Upload(xlsx)'.

*--------------------------------------------------------------------*
* at selection screen
*--------------------------------------------------------------------*
at selection-screen on value-request for p_file.

  
datal_rc type i.
  
datat_file_table  type filetable,
        ls_file_table 
type file_table.

  
call method cl_gui_frontend_services=>file_open_dialog
    
exporting
      window_title 
'Select a file'
    
changing
      file_table   
t_file_table
      rc           
l_rc.

  
if sy-subrc 0.
    
read table t_file_table into ls_file_table index 1.
    p_file 
ls_file_table-filename.
    g_c01 
ls_file_table-filename.
  
endif.

start-of-selection .

  
perform read_file .
  
perform process_file.

*---------------------------------------------------------------------*
* Form READ_FILE
*---------------------------------------------------------------------*
form read_file .

  
data l_filename      type string,
         t_records       
type solix_tab,
         l_headerxstring 
type xstring,
         l_filelength    
type i.

  l_filename 
p_file.

  
call function 'GUI_UPLOAD'
    
exporting
      filename                
l_filename
      filetype                
'BIN'
    
importing
      filelength              
l_filelength
      
header                  l_headerxstring
    
tables
      data_tab                
t_records
    
exceptions
      file_open_error         
1
      file_read_error         
2
      no_batch                
3
      gui_refuse_filetransfer 
4
      invalid_type            
5
      no_authority            
6
      unknown_error           
7
      bad_data_format         
8
      header_not_allowed      
9
      separator_not_allowed   
10
      header_too_long         
11
      unknown_dp_error        
12
      access_denied           
13
      dp_out_of_memory        
14
      disk_full               
15
      dp_timeout              
16
      
others                  17.

  
"convert binary data to xstring
  
"if you are using cl_fdt_xl_spreadsheet in odata then skips this step
  
"as excel file will already be in xstring
  
call function 'SCMS_BINARY_TO_XSTRING'
    
exporting
      input_length 
l_filelength
    
importing
      
buffer       l_headerxstring
    
tables
      binary_tab   
t_records
    
exceptions
      failed       
1
      
others       2.

  
if sy-subrc <> 0.
    
"Implement suitable error handling here
  
endif.

  
data lo_excel_ref type ref to cl_fdt_xl_spreadsheet .

  
try .
      lo_excel_ref 
new cl_fdt_xl_spreadsheet(
                              document_name 
l_filename
                              xdocument     
l_headerxstring ) .
    
catch cx_fdt_excel_core.
      
"Implement suitable error handling here
  
endtry .

  
"Get List of Worksheets
  lo_excel_ref
->if_fdt_doc_spreadsheet~get_worksheet_names(
    
importing
      worksheet_names 
data(t_worksheets) ).

  
if not t_worksheets is initial.
    
read table t_worksheets into data(l_woksheetnameindex 1.

    
data(lo_data_ref) = lo_excel_ref->if_fdt_doc_spreadsheet~get_itab_from_worksheet(
                                             l_woksheetname 
).
    
"now you have excel work sheet data in dyanmic internal table
    
assign lo_data_ref->to <gt_data>.
  
endif.

endform.

*---------------------------------------------------------------------*
* Form PROCESS_FILE
*---------------------------------------------------------------------*
form process_file .

  
data l_numberofcolumns   type i,
         l_date_string       
type string,
         l_target_date_field 
type datum.
 

  
field-symbols <ls_data> type any,
                  <l_field> 
type any.

  
"you could find out number of columns dynamically from table <gt_data>
  l_numberofcolumns 
.

  
loop at <gt_data> assigning <ls_data> from .

    
"processing columns
    
do l_numberofcolumns times.
      
assign component sy-index of structure <ls_data> to <l_field> .
      
if sy-subrc .
        
case sy-index .
*          when 1 .
*          when 2 .
          
when 10 .
            l_date_string 
<l_field> .
            
perform date_convert using l_date_string changing l_target_date_field .
            
write l_target_date_field .
          
when others.
            
write <l_field> .
        
endcase .
      
endif.
    
enddo .
    
new-line .
  
endloop .


endform.

*---------------------------------------------------------------------*
* Form DATE_CONVERT
*---------------------------------------------------------------------*
form date_convert using iv_date_string type string changing cv_date type datum .

  
datal_convert_date(10type c.

  l_convert_date 
iv_date_string .

  
"date format YYYY/MM/DD
  
find regex '^\d{4}[/|-]\d{1,2}[/|-]\d{1,2}$' in l_convert_date.
  
if sy-subrc 0.
    
call function '/SAPDMC/LSM_DATE_CONVERT'
      
exporting
        date_in             
l_convert_date
        date_format_in      
'DYMD'
        to_output_format    
' '
        to_internal_format  
'X'
      
importing
        date_out            
l_convert_date
      
exceptions
        illegal_date        
1
        illegal_date_format 
2
        no_user_date_format 
3
        
others              4.
  
else.

    
" date format DD/MM/YYYY
    
find regex '^\d{1,2}[/|-]\d{1,2}[/|-]\d{4}$' in l_convert_date.
    
if sy-subrc 0.
      
call function '/SAPDMC/LSM_DATE_CONVERT'
        
exporting
          date_in             
l_convert_date
          date_format_in      
'DDMY'
          to_output_format    
' '
          to_internal_format  
'X'
        
importing
          date_out            
l_convert_date
        
exceptions
          illegal_date        
1
          illegal_date_format 
2
          no_user_date_format 
3
          
others              4.
    
endif.

  
endif.

  
if sy-subrc 0.
    cv_date 
l_convert_date .
  
endif.

endform .


Copyright © 2013 ABAP SAP - AbapFox! Aprenda ABAP Definitivamente