Skip to end of metadataGo to start of metadata
Nov 11, 2013 ABAP WebDynpro – Upload Excel File using Class CLFDTXLSPREADSHEET. Lets see how we can upload the excel file using the class CLFDTXLSPREADSHEET in ABAP WebDynpro application. Preface: Data can be copied into the Excel from various sources. These sources can copy the data using Unicode vs non-Unicode.
Downloading a file into excel sheet
1. Create a mainview and mainwindow.
.
2. Goto mainview and select context, right click on it and create->node
3. Give name for node and select dictionary structure asVBAKand select add attribute from structure.
4. Go to layout and in the right side selectrootuielementcontainer, right click on it select insert element.
5. Give name for it and type as table and save it.
6. Click on the table and selectcreate binding. Click oncontextthen select vbak.
7. Gotomethodsand doubleclick onWDDOINIT.
Select webdynpro code wizard(ctrl+f7).
Select webdynpro code wizard(ctrl+f7).
select radiobuttonread contextgive the node name and press enter.
*Then call method* bind_tableand write code as.
method* WDDOINIT .*
* **DATA lo_nd_vbak TYPEREFTO if_wd_context_node.*
* **DATA lo_el_vbak TYPEREFTO if_wd_context_element.*
* **DATA ls_vbak TYPE wd_this->elements_vbak.*
* navigate from <CONTEXT> to <VBAK> via lead selection
lo_nd_vbak = wd_context->get_child_node( name = wd_this->wdctx_vbak ).
* **DATA lo_nd_vbak TYPEREFTO if_wd_context_node.*
* **DATA lo_el_vbak TYPEREFTO if_wd_context_element.*
* **DATA ls_vbak TYPE wd_this->elements_vbak.*
* navigate from <CONTEXT> to <VBAK> via lead selection
lo_nd_vbak = wd_context->get_child_node( name = wd_this->wdctx_vbak ).
select* *from vbak into corresponding fieldsoftable ls_vbak upto10rows.*
* **CALLMETHOD lo_nd_vbak->bind_table*
* *EXPORTING
new_items = ls_vbak
* set_initial_elements = ABAP_TRUE
* index =
.
endmethod*.*
* *EXPORTING
new_items = ls_vbak
* set_initial_elements = ABAP_TRUE
* index =
.
endmethod*.*
8. Select the button and go toeventsand selectonactiongive as download.
9. Doubleclick on download and call ctrl+f7, select radio button read context and select the node.
Then call the methodget_static_attributes_table.
And function module'SCMS_STRING_TO_XSTRING'and again call the methodattach_file_to_responseand write code as.
method* ONACTIONDOWNLOAD .*
* **DATA lo_nd_vbak TYPEREFTO if_wd_context_node.*
* **DATA lo_el_vbak TYPEREFTO if_wd_context_element.*
* **DATA ls_vbak TYPE wd_this->elements_vbak.*
* **data lw_vbak likelineof ls_vbak.*
* **data str type string.*
* **data xstr type xstring.*
* navigate from <CONTEXT> to <VBAK> via lead selection
lo_nd_vbak = wd_context->get_child_node( name = wd_this->wdctx_vbak ).
* **DATA lo_nd_vbak TYPEREFTO if_wd_context_node.*
* **DATA lo_el_vbak TYPEREFTO if_wd_context_element.*
* **DATA ls_vbak TYPE wd_this->elements_vbak.*
* **data lw_vbak likelineof ls_vbak.*
* **data str type string.*
* **data xstr type xstring.*
* navigate from <CONTEXT> to <VBAK> via lead selection
lo_nd_vbak = wd_context->get_child_node( name = wd_this->wdctx_vbak ).
* **CALLMETHOD lo_nd_vbak->get_static_attributes_table*
* EXPORTING
* from = 1
* to = 2147483647
* *IMPORTING
* **table = ls_vbak*
.
* EXPORTING
* from = 1
* to = 2147483647
* *IMPORTING
* **table = ls_vbak*
.
loop* at ls_vbak into lw_vbak.*
* **concatenate str*
lw_vbak-vbeln
lw_vbak-erdat
lw_vbak-erzet
* cl_abap_char_utilities=>newline **into str*
* **separatedby cl_abap_char_utilities=>horizontal_tab.*
endloop*.*
lw_vbak-vbeln
lw_vbak-erdat
lw_vbak-erzet
* cl_abap_char_utilities=>newline **into str*
* **separatedby cl_abap_char_utilities=>horizontal_tab.*
endloop*.*
CALL* FUNCTION *'SCMS_STRING_TO_XSTRING'
* *EXPORTING
* **text = str*
* MIMETYPE = ' '
* ENCODING =
* *IMPORTING
* **BUFFER = xstr*
* EXCEPTIONS
* FAILED = 1
.
CALL* METHOD cl_wd_runtime_services=>attach_file_to_response*
* *EXPORTING
* i_filename = *'Sales.xls'
i_content = xstr
* i_mime_type = *'EXCEL'
* i_in_new_window = ABAP_FALSE
* i_inplace = ABAP_FALSE
.
endmethod*.*
* *EXPORTING
* **text = str*
* MIMETYPE = ' '
* ENCODING =
* *IMPORTING
* **BUFFER = xstr*
* EXCEPTIONS
* FAILED = 1
.
CALL* METHOD cl_wd_runtime_services=>attach_file_to_response*
* *EXPORTING
* i_filename = *'Sales.xls'
i_content = xstr
* i_mime_type = *'EXCEL'
* i_in_new_window = ABAP_FALSE
* i_inplace = ABAP_FALSE
.
endmethod*.*
10.Save it and execute.
Output will be as below. When we press the download button then the file will be downloaded and saved as excel file.
Skip to end of metadataGo to start of metadataby Nagendran R, Enteg Infotech, Bangalore, India
Go to T-Code- SE80, Create a Web Dynpro Component(Zfile_upload in this example)
Double click on the view MAIN -> choose Context tab -> right-click on the topmost context and choose Create> Node.
Create two Nodes
Node1: N_UPLOAD
Cardinality 1.1
Attributes | Type |
FILE_NAME | STRING |
FILE_TYPE | STRING |
FILE_SIZE | STRING |
FILE_CONTENTS | XSTRING |
Node2: N_FILE_DOWNLOAD
Cardinality is 0.n
Attributes | Type |
FILE_NAME | STRING |
FILE_TYPE | STRING |
FILE_SIZE | STRING |
FILE_CONTENTS | XSTRING |
After creating the two Nodes,
Now go to Layout tab of MAIN view,
Now go to Layout tab of MAIN view,
- Create a group.
- Create the element FileUpload.
After adding the File Upload UI element,
Create a BUTTON UI element with text property as Upload and action property as UPLOAD.
Afrer creating Button, Create a group and inside group create a UI element TABLE.
After creating the table right-click on the table UI element and choose Create Binding.
Click on Context and choose the Node N_FILE_DOWNLOAD.
Create binding as below.
Now, in the Table columns choose the column File contents, and in the TEXT property BIND the attribute FILE_NAME OF NODE N_FILE_DOWNLOAD.
After Binding, the table look like the table in the below screen shot.
Now, Choose the UI element FILE_UPLOAD and create binding for the property data, Filename, mimetype.
After Binding the UI element FILE UPLOAD,
Create a z-table to store all the attachments that has been uploaded, so that the files can be downloaded later.
The z-table fields and types are mentioned below.
Go to the methods tab and double-click on ONACTIONUPLOADA(action property for UPLOAD button) and place the below code:
Go to the methods tab and double-click on ONACTIONUPLOADA(action property for UPLOAD button) and place the below code:
method ONACTIONUPLOAD .
DATA lo_nd_n_upload TYPE REF TO if_wd_context_node.
DATA lo_el_n_upload TYPE REF TO if_wd_context_element.
DATA ls_n_upload TYPE wd_this->element_n_upload.
DATA lo_el_n_upload TYPE REF TO if_wd_context_element.
DATA ls_n_upload TYPE wd_this->element_n_upload.
DATA lo_nd_n_file_download TYPE REF TO if_wd_context_node.
DATA lt_n_file_download TYPE wd_this->elements_n_file_download.
DATA lt_n_file_download TYPE wd_this->elements_n_file_download.
data ls_file_upload TYPE ZFILE_UPLOAD1.
* navigate from <CONTEXT> to <N_FILE_DOWNLOAD> via lead selection
lo_nd_n_file_download = wd_context->get_child_node( name = wd_this->wdctx_n_file_download ).
* navigate from <CONTEXT> to <N_FILE_DOWNLOAD> via lead selection
lo_nd_n_file_download = wd_context->get_child_node( name = wd_this->wdctx_n_file_download ).
* navigate from <CONTEXT> to <N_UPLOAD> via lead selection
lo_nd_n_upload = wd_context->get_child_node( name = wd_this->wdctx_n_upload ).
lo_nd_n_upload = wd_context->get_child_node( name = wd_this->wdctx_n_upload ).
* get element via lead selection
lo_el_n_upload = lo_nd_n_upload->get_element( ).
* @TODO handle not set lead selection
IF lo_el_n_upload IS not INITIAL.
lo_el_n_upload = lo_nd_n_upload->get_element( ).
* @TODO handle not set lead selection
IF lo_el_n_upload IS not INITIAL.
* get all declared attributes
lo_el_n_upload->get_static_attributes(
IMPORTING
static_attributes = ls_n_upload ).
lo_el_n_upload->get_static_attributes(
IMPORTING
static_attributes = ls_n_upload ).
* ls_n_upload will contain the File name file type and file contents *
ls_n_upload-file_size = xstrlen( ls_n_upload-file_contents ).
ls_file_upload-FILE_NAME = ls_n_upload-FILE_NAME.
ls_file_upload-FILE_TYPE = ls_n_upload-FILE_TYPE.
ls_file_upload-FILE_SIZE = ls_n_upload-FILE_SIZE.
ls_file_upload-FILE_CONTENTS = ls_n_upload-FILE_CONTENTS.
ls_file_upload-FILE_TYPE = ls_n_upload-FILE_TYPE.
ls_file_upload-FILE_SIZE = ls_n_upload-FILE_SIZE.
ls_file_upload-FILE_CONTENTS = ls_n_upload-FILE_CONTENTS.
insert zfile_upload1 from ls_file_upload.
if sy-subrc = 0.
select file_name
file_type
file_size
file_contents
from zfile_upload1
into TABLE lt_n_file_download.
if sy-subrc = 0.
select file_name
file_type
file_size
file_contents
from zfile_upload1
into TABLE lt_n_file_download.
lo_nd_n_file_download->bind_table( new_items = lt_n_file_download set_initial_elements = abap_true ).
endif.
ENDIF.
endmethod.
Activate all the Web Dynpro components .Create a Web Ddynpro Application and test it,
The Output Will be,
The Output Will be,
Click on Browse -> Choose a file for Upload -> Click on Upload.
After Uploading,file details can be seen in the Table and a Link to Download will be availablle.
Click on the Link to download the Uploaded file.
The Download file can be viewed in a Pop-up window.