Upload XLSX file in WebDynpro ABAP and view its content
You can see many posts in WebDynpro ABAP Form about reading an Excel file and showing its content in a WebDynpro ABAP Table, however many of them would be closed unanswered since it was not directly possible.
Now it is possible with this interesting and collaborative project ABAP2XLSX!!! Way to go guys for innovative thinking by starting this project!!!
Prerequisites:
- Install SAPLINK 1.4 or higher version http://code.google.com/p/saplink/downloads/list
- Import and activate needed plug-ins for DDIC , Function Group, Dev Class, Interfaces , WebDynpro ABAP http://code.google.com/p/saplink-plugins/downloads/list or https://cw.sdn.sap.com/cw/docs/DOC-145448
- Import most important Plug-in ABAP2XLSX https://cw.sdn.sap.com/cw/releases/86?container=1016 Installation guide https://cw.sdn.sap.com/cw/docs/DOC-137513
- An XLSX excel file with some data.
- Join ABAP2XLSX and SAPLINKGroup for more updates on these awsome discoveries https://cw.sdn.sap.com/cw/groups/saplink and https://cw.sdn.sap.com/cw/groups/abap2xlsx
Create a WD Component with File upload UI Element:
- Create a WD Component in SE80 with a window and a view
- In View controller, create a node” upload_content” and an attribute “file_content” to store Uploaded Excel file data in XSTRING format.
- Create a File Upload UI Element and bind its datasource property to XSTRING attribute.
- Create a Button and assign an action upload to it, when an excel file will be uploaded
- And here comes the magical code to read the excel 2007 (.xlsx) file.
Method OnActionUpload. |
METHOD onactionupload . DATA lo_nd_upload_content TYPE REF TO if_wd_context_node. DATA lo_el_upload_content TYPE REF TO if_wd_context_element. DATA ls_upload_content TYPE wd_this->element_upload_content. DATA: lo_componentcontroller TYPE REF TO ig_componentcontroller, lo_current_controller TYPE REF TO if_wd_controller, lo_view_controller TYPE REF TO if_wd_view_controller, lo_message_manager TYPE REF TO if_wd_message_manager . DATA: excel TYPE REF TO zcl_excel, lo_excel_writer TYPE REF TO zif_excel_writer, reader TYPE REF TO zif_excel_reader. DATA: worksheet TYPE REF TO zcl_excel_worksheet, highest_column TYPE zexcel_cell_column, highest_row TYPE int4, column TYPE zexcel_cell_column VALUE 1, col_str TYPE zexcel_cell_column_alpha, row TYPE int4 VALUE 1, value TYPE zexcel_cell_value. DATA: ex TYPE REF TO zcx_excel, msg TYPE string, lv_highest_row TYPE string, lv_highest_column TYPE string, lv_rowdata TYPE string, lv_rownumber TYPE string.
lo_current_controller ?= wd_this->wd_get_api( ). lo_message_manager = lo_current_controller->get_message_manager( ). lo_view_controller = wd_this->wd_get_api( ).
* navigate from <CONTEXT> to <UPLOAD_CONTENT> via lead selection lo_nd_upload_content = wd_context->get_child_node( name = wd_this->wdctx_upload_content ).
* get element via lead selection lo_el_upload_content = lo_nd_upload_content->get_element( ).
* get all declared attributes lo_el_upload_content->get_static_attributes( IMPORTING static_attributes = ls_upload_content ).
TRY. CREATE OBJECT reader TYPE zcl_excel_reader_2007. excel = reader->load( ls_upload_content-file_content ).
worksheet = excel->get_active_worksheet( ). highest_column = worksheet->get_highest_column( ). MOVE highest_column TO lv_highest_column. highest_row = worksheet->get_highest_row( ). MOVE highest_row TO lv_highest_row. CONCATENATE 'Highest column: ' lv_highest_column 'Highest row: ' lv_highest_row INTO msg. CALL METHOD lo_message_manager->report_message EXPORTING message_text = msg.
CLEAR lv_rowdata.
WHILE row <= highest_row. WHILE column <= highest_column. col_str = zcl_excel_common=>convert_column2alpha( column ). worksheet->get_cell( EXPORTING ip_column = col_str ip_row = row IMPORTING ep_value = value ). CONCATENATE lv_rowdata value INTO lv_rowdata SEPARATED BY space. column = column + 1. ENDWHILE. CLEAR msg. MOVE row TO lv_rownumber. CONCATENATE 'Row# ' lv_rownumber ' Data = ' lv_rowdata INTO msg. CLEAR lv_rowdata. CALL METHOD lo_message_manager->report_message EXPORTING message_text = msg. column = 1. row = row + 1. ENDWHILE.
CATCHzcx_excel INTO ex. " Exceptions for ABAP2XLSX CLEAR msg. msg = ex->error.
* report message CALL METHOD lo_message_manager->report_message EXPORTING message_text = msg.
ENDTRY. ENDMETHOD. |
Sample Excel file Content
Sample WebDynpro ABAP Application accepting xlsx file:
Output :
Happy programming!!!!
Feel free to share your experience with Excel upload in WDA.