From c21a49617b6673db85b10b3b91af9245c4400ee7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20Schm=C3=B6cker?= Date: Mon, 5 Nov 2012 23:13:19 +0000 Subject: [PATCH] #230 - Pimp my Code - ZCL_EXCEL_READER_2007->ZIF_EXCEL_READER~LOAD_FILE git-svn-id: https://subversion.assembla.com/svn/abap2xlsx/trunk@360 b7d68dce-7c3c-4a99-8ce0-9ea847f5d049 --- ZA2X/CLAS/ZCL_EXCEL_READER_2007.slnk | 289 +++++++++++++++++---------- 1 file changed, 183 insertions(+), 106 deletions(-) diff --git a/ZA2X/CLAS/ZCL_EXCEL_READER_2007.slnk b/ZA2X/CLAS/ZCL_EXCEL_READER_2007.slnk index b6673a2..c108348 100644 --- a/ZA2X/CLAS/ZCL_EXCEL_READER_2007.slnk +++ b/ZA2X/CLAS/ZCL_EXCEL_READER_2007.slnk @@ -1,71 +1,71 @@ - - - - - - - - - - - - - - - - + class ZCL_EXCEL_READER_2007 definition public create public . @@ -246,6 +246,11 @@ protected section. *"* implementation or private method's signature *"* use this source file for any macro definitions you need *"* in the implementation part of the class + + + + + IXML @@ -297,67 +302,139 @@ endmethod. method ZIF_EXCEL_READER~LOAD_FILE. - DATA: excel_data TYPE xstring. - DATA filelength TYPE i. - DATA bin_tab TYPE TABLE OF x255. - " Background processing - DATA bin_data LIKE LINE OF bin_tab. - DATA len TYPE i. - DATA alen TYPE i. +*--------------------------------------------------------------------* +* ToDos: +* 2do§1 decision whether to load from frontend or backend +* current behavior ( autodecide ) should be default +* add optional parameter to allow user to choose +* to load from backend even when online +* 2do§2 loosen typing of i_filename to CLIKE +*--------------------------------------------------------------------* +*--------------------------------------------------------------------* +* issue #230 - Pimp my Code +* - Stefan Schmöcker, 2012-11-05 +* - ... +* changes: renaming variables to naming conventions +* renaming variables to indicate what they are used for +* adding comments to explain what we are trying to achieve +* message made to support multilinguality +* aligning code +* commenting on problems/future enhancements/todos we already know of or should decide upon +* adding issue # that has initiated the change - date provided to allow cleaning of code after a certain period +* explicit declaration of type of table instead of implicit declaration +* added errorhandling for open dataset +*--------------------------------------------------------------------* + + CONSTANTS: lcv_load_from_frontend TYPE char1 VALUE 'F', + lcv_load_from_backend TYPE char1 VALUE 'B'. + + DATA: lv_load_from_source TYPE char1, + + lv_filelength TYPE i, + lt_binary_data TYPE STANDARD TABLE OF x255 WITH NON-UNIQUE DEFAULT KEY, + ls_binary_data LIKE LINE OF lt_binary_data, +* Background processing + lv_max_length_line TYPE i, + lv_actual_length_line TYPE i, + + lv_errormessage TYPE string, " Can't pass '...'(abc) to exception-class + lv_excel_data TYPE xstring. " Binary content of .xlsx file + + +*--------------------------------------------------------------------* +* ToDos: 2do§1 Decision whether to load from frontend or backend +*--------------------------------------------------------------------* + +*--------------------------------------------------------------------* +* Autodecide on frontend or backend reading +* Background-processing --> backend reading +* Online-processing --> frontend reading +*--------------------------------------------------------------------* IF sy-batch = abap_true. - DESCRIBE FIELD bin_data LENGTH len IN BYTE MODE. - OPEN DATASET i_filename FOR INPUT IN BINARY MODE. - WHILE sy-subrc = 0. - READ DATASET i_filename INTO bin_data MAXIMUM LENGTH len ACTUAL LENGTH alen. - APPEND bin_data TO bin_tab. - filelength = filelength + alen. - ENDWHILE. - CLOSE DATASET i_filename. + lv_load_from_source = lcv_load_from_backend. ELSE. - cl_gui_frontend_services=>gui_upload( - EXPORTING - filename = i_filename " Name of file - filetype = 'BIN' " File Type (ASCII, Binary) - IMPORTING - filelength = filelength - CHANGING - data_tab = bin_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 - not_supported_by_gui = 17 - error_no_gui = 18 - OTHERS = 19 - ). - IF sy-subrc <> 0. - RAISE EXCEPTION TYPE zcx_excel - EXPORTING - error = 'A problem occured when reading the file'. - ENDIF. + lv_load_from_source = lcv_load_from_frontend. ENDIF. + + CASE lv_load_from_source. + +*--------------------------------------------------------------------* +* Read from backend +*--------------------------------------------------------------------* + WHEN lcv_load_from_backend. + DESCRIBE FIELD ls_binary_data LENGTH lv_max_length_line IN BYTE MODE. + OPEN DATASET i_filename FOR INPUT IN BINARY MODE. + IF sy-subrc <> 0. + lv_errormessage = 'A problem occured when reading the file'(001). + RAISE EXCEPTION TYPE zcx_excel + EXPORTING + error = lv_errormessage. + ENDIF. + WHILE sy-subrc = 0. + + READ DATASET i_filename INTO ls_binary_data MAXIMUM LENGTH lv_max_length_line ACTUAL LENGTH lv_actual_length_line. + APPEND ls_binary_data TO lt_binary_data. + lv_filelength = lv_filelength + lv_actual_length_line. + + ENDWHILE. + CLOSE DATASET i_filename. + +*--------------------------------------------------------------------* +* Read from frontend +*--------------------------------------------------------------------* + WHEN lcv_load_from_frontend. + cl_gui_frontend_services=>gui_upload( EXPORTING + filename = i_filename + filetype = 'BIN' " We are basically working with zipped directories --> force binary read + IMPORTING + filelength = lv_filelength + CHANGING + data_tab = lt_binary_data + 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 + not_supported_by_gui = 17 + error_no_gui = 18 + OTHERS = 19 ). + IF sy-subrc <> 0. + lv_errormessage = 'A problem occured when reading the file'(001). + RAISE EXCEPTION TYPE zcx_excel + EXPORTING + error = lv_errormessage. + ENDIF. + + ENDCASE. + + +*--------------------------------------------------------------------* +* Binary data needs to be provided as XSTRING for further processing +*--------------------------------------------------------------------* CALL FUNCTION 'SCMS_BINARY_TO_XSTRING' EXPORTING - input_length = filelength + input_length = lv_filelength IMPORTING - buffer = excel_data + buffer = lv_excel_data TABLES - binary_tab = bin_tab. - r_excel = me->zif_excel_reader~load( excel_data ). + binary_tab = lt_binary_data. + + r_excel = me->zif_excel_reader~load( lv_excel_data ). + + endmethod. @@ -1117,7 +1194,7 @@ endmethod. - METHOD load_workbook. + method LOAD_WORKBOOK. CONSTANTS: lc_shared_strings TYPE string VALUE 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings', lc_worksheet TYPE string VALUE 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet', @@ -1273,7 +1350,7 @@ endmethod. * #229: Set active worksheet - end coding *--------------------------------------------------------------------* -ENDMETHOD. +endmethod.