Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

function module to get data from files

Former Member
0 Likes
5,538

hi, can anyone tell me the function module to get data from any kind of file, be it a word document or excel sheet, or a plain text file ? i need to get the contents of the file and display them. And also do i need to convert it to a particular format while reading and displaying ? Thank you

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
2,315

Hi Hitesh Nangrani,

You can use the following function module to read a file and upload it to some internal table, inorder to use the data further.

I'll give you an example wherein theres a selection screen to select any file (word or excel) from ur PC and then upload it to an internal table.

Example :

Define a variable of type string to hold ur file name

Data : lv_file_name type string.

      • SELECTION SCREEN ***

Selection Screen to take the file, where p_f_name is the parameter of type rlgrap-filename and it is made mandatory & text-100 is a text field where u can give any description like "please input file path"

selection-screen begin of block b1 with frame title text-100.

parameters : p_f_name like rlgrap-filename obligatory.

selection-screen end of block b1.

      • AT SELECTION-SCREEN ***

at selection-screen on value-request for p_f_name.

      • Function Module To Open The Input File Containing The Input Values ***

following function module will open the file, in IMPORTING give the parameter p_f_name, which has the file path, it will open the file at that path i.e. the file u want

call function 'WS_FILENAME_GET'

EXPORTING

def_filename = 'C:\test.xls'

def_path = 'C:\'

mask = ',.,..'

mode = 'O' "O --> Open S --> Save

title = 'OPEN'

IMPORTING

filename = p_f_name

EXCEPTIONS

inv_winsys = 1

no_batch = 2

selection_cancel = 3

selection_error = 4

others = 5.

Assign the file path to the variable u had declared to hold the file name

      • START-OF-SELECTION ***

start-of-selection.

lv_file_name = p_f_name.

      • Function Module To Upload Input File To The Internal Table ***

following function module will read the file and transfer it data to the internal table

In FILENAME assign the filename variabe and in TABLES assign the internal table name in which u want the file data to come

call function 'GUI_UPLOAD'

EXPORTING

filename = lv_file_name

filetype = 'ASC'

has_field_separator = 'X'

TABLES

data_tab = lt_material_details.

NOTE : See to it that ur internal table has all the fields that are there in ur excel sheet / word, so that the contents of file properly go into respective fields of internal table. otherwise there would be mis-matching of fields and data.

Hope this reply would help you...do let me knw if it worked...All The Best

Regards,

Radhika

7 REPLIES 7
Read only

Former Member
0 Likes
2,315

Basic question i feel.Use gui_upload

Read only

0 Likes
2,315

did that, but the contents i get are i think in another format, coz they are not in english, so if i have to convert them into another format for displaying then can you tell me a function module for converting it to another format ?

Read only

Former Member
0 Likes
2,315

Hi Hitesh,

There are number of FM's for resolving your issue.

uplaod documents mayu be of .doc,.xls or .txt according to the ease we can select the FM's

FM for excel as it will have rows and columns

PARAMETER p_infile like rlgrap-filename.

call function u2018ALSM_EXCEL_TO_INTERNAL_TABLEu2019
exporting
filename                = p_infile
i_begin_col             = u20181u2032
i_begin_row             = u20182u2032  u201CDo not require headings
i_end_col               = u201814u2032
i_end_row               = u201831u2032
tables
intern                  = itab
exceptions
inconsistent_parameters = 1
upload_ole              = 2
others                  = 3.
if sy-subrc <> 0.
message e010(zz) with text-001. u201CProblem uploading Excel Spreadsheet
endif.

PARAMETERS: p_file TYPE rlgrap-filename.

CALL FUNCTION 'TEXT_CONVERT_XLS_TO_SAP'

 EXPORTING

 i_line_header        = 'X'

 i_tab_raw_data       = it_raw

 i_filename           = p_file

 TABLES

 i_tab_converted_data = it_datatab[]

 EXCEPTIONS

 conversion_failed    = 1

 OTHERS               = 2.

 IF sy-subrc <> 0.

 *  key in some message

 ENDIF.

FM for text files as our friend suggested GUI_UPLOAD

PARAMETERS: p_file(80)
DEFAULT 'C:\Temp\Zexample.txt'.

CALL FUNCTION 'GUI_UPLOAD'
EXPORTING
filename = filename
* FILETYPE = 'ASC
has_field_separator = 'X'
* HEADER_LENGTH = 0
* READ_BY_LINE = 'X'
* IMPORTING
* FILELENGTH =
* HEADER =
TABLES
data_tab = i_tab
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 <> 0.
v_subrc = sy-subrc.
MESSAGE e899 WITH 'Your Error message.
ENDIF.

hope this helps

Thanks and Regards

Srikanth.P

Edited by: SRIKANTH P on May 30, 2009 8:58 PM

Read only

Former Member
0 Likes
2,315

Hi,

use function module GUI_UPLOAD to get data from any source file while the source file is in presentation server.

read the data using this function module into a table type of string. then split the string accordingly by the given separator and display them in any way you want.

but if the source file is in application server then use the following approach to read data in string format:

open dataset.

read dataset.

close dataset.

regards,

Prinan

Read only

Former Member
0 Likes
2,315

Hi,

Function module GUI_UPLOAD will works for you.

once the data pass into the internal table then you can display .

Aswarth

Read only

Former Member
0 Likes
2,316

Hi Hitesh Nangrani,

You can use the following function module to read a file and upload it to some internal table, inorder to use the data further.

I'll give you an example wherein theres a selection screen to select any file (word or excel) from ur PC and then upload it to an internal table.

Example :

Define a variable of type string to hold ur file name

Data : lv_file_name type string.

      • SELECTION SCREEN ***

Selection Screen to take the file, where p_f_name is the parameter of type rlgrap-filename and it is made mandatory & text-100 is a text field where u can give any description like "please input file path"

selection-screen begin of block b1 with frame title text-100.

parameters : p_f_name like rlgrap-filename obligatory.

selection-screen end of block b1.

      • AT SELECTION-SCREEN ***

at selection-screen on value-request for p_f_name.

      • Function Module To Open The Input File Containing The Input Values ***

following function module will open the file, in IMPORTING give the parameter p_f_name, which has the file path, it will open the file at that path i.e. the file u want

call function 'WS_FILENAME_GET'

EXPORTING

def_filename = 'C:\test.xls'

def_path = 'C:\'

mask = ',.,..'

mode = 'O' "O --> Open S --> Save

title = 'OPEN'

IMPORTING

filename = p_f_name

EXCEPTIONS

inv_winsys = 1

no_batch = 2

selection_cancel = 3

selection_error = 4

others = 5.

Assign the file path to the variable u had declared to hold the file name

      • START-OF-SELECTION ***

start-of-selection.

lv_file_name = p_f_name.

      • Function Module To Upload Input File To The Internal Table ***

following function module will read the file and transfer it data to the internal table

In FILENAME assign the filename variabe and in TABLES assign the internal table name in which u want the file data to come

call function 'GUI_UPLOAD'

EXPORTING

filename = lv_file_name

filetype = 'ASC'

has_field_separator = 'X'

TABLES

data_tab = lt_material_details.

NOTE : See to it that ur internal table has all the fields that are there in ur excel sheet / word, so that the contents of file properly go into respective fields of internal table. otherwise there would be mis-matching of fields and data.

Hope this reply would help you...do let me knw if it worked...All The Best

Regards,

Radhika

Read only

0 Likes
2,315

thanks a lot everyone