Ej Abre Excel

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 3

* abre un fichero de excel con una primera l�nea de cabecera con los

* nombres de los campos seguida de n registros


* Genera un archivo temporal .DAT con los registros y lo carga
* finalmente a la tabla t_record

data: application type ole2_object,


workbooks type ole2_object,
fworkbook type ole2_object,
fsheets type ole2_object,
fsheet type ole2_object,
dsheet type ole2_object,
frange type ole2_object,
frow type ole2_object.

data: return,
filename type ibipparms-path,
l_filename type string,
t_filename like rlgrap-filename.

* Filename conversion
move pa_path to filename.
move filename to t_filename.
translate t_filename to upper case.
replace '.XLS' with '.DAT' into t_filename.

if sy-subrc ne 0.
* error - filename extension is not XLS
exit.
endif.

* check file existence


call function 'WS_QUERY'
EXPORTING
filename = filename
query = 'FE'
IMPORTING
return = return
EXCEPTIONS
inv_query = 1
no_batch = 2
frontend_error = 3
others = 4.
if ( sy-subrc ne 0 ) or ( return eq 0 ).
* error - file does not exist
exit.
endif.

***** Working through OLE (MS Excel) *****


* OLE initialization
create object dsheet 'Excel.Sheet'.
if sy-subrc ne 0.
* error - OLE initialization of MS Excel
exit.
endif.
* supress messages from MS Excel
call method of dsheet 'Application' = application.
set property of application 'DisplayAlerts' = 0.
* opening XLS-file
call method of application 'Workbooks' = workbooks.
call method of workbooks 'Open'
EXPORTING
#1 = filename
#2 = 0
#3 = 1.
* remove header line from XLS data
get property of application 'ActiveWorkbook' = fworkbook.
call method of fworkbook 'Sheets' = fsheets.
call method of fsheets 'Item' = fsheet
EXPORTING
#1 = 1.
get property of fsheet 'Rows' = frange.
call method of frange 'Item' = frow
EXPORTING
#1 = 1.
call method of frow 'Delete'.
* save as DAT file
call method of fworkbook 'SaveAs'
EXPORTING
#1 = t_filename
#2 = 20.
* close the book without saving
call method of fworkbook 'Close'
EXPORTING
#1 = 0.
* ??????? ?????? ?? OLE-??????
free object dsheet.

***** Filling internal table with data *****


* refresh input_table.
refresh t_record.
l_filename = t_filename.
call function 'GUI_UPLOAD'
EXPORTING
filename = l_filename
has_field_separator = 'X'
TABLES
data_tab = t_record
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.
if sy-subrc ne 0.
* error - loading DAT file
exit.
else.
* the loading was successfull
endif.
****** Delete temporary DAT-file *****
call function 'GUI_DELETE_FILE'
EXPORTING
file_name = t_filename
EXCEPTIONS
others = 1.
if sy-subrc <> 0.
call function 'WS_QUERY'
EXPORTING
filename = t_filename
query = 'FE'
IMPORTING
return = return
EXCEPTIONS
inv_query = 1
no_batch = 2
frontend_error = 3
others = 4.
if ( sy-subrc ne 0 ) or ( return eq 0 ).
exit.
else.
* error - deleting temporary file "TODO
endif.
endif.

You might also like