From 3f39940cb407a3107e3bcd212e8ffbb7c62b94ee Mon Sep 17 00:00:00 2001 From: Ivan Femia Date: Sun, 25 Sep 2011 10:29:04 +0000 Subject: [PATCH] Fix issue #113 git-svn-id: https://subversion.assembla.com/svn/abap2xlsx/trunk@221 b7d68dce-7c3c-4a99-8ce0-9ea847f5d049 --- ZA2X/CLAS/ZCL_EXCEL_WRITER_CSV.slnk | 71 +++++++++++++++++++++-------- ZA2X/PROG/ZDEMO_EXCEL28.slnk | 38 +++++++++++++-- 2 files changed, 86 insertions(+), 23 deletions(-) diff --git a/ZA2X/CLAS/ZCL_EXCEL_WRITER_CSV.slnk b/ZA2X/CLAS/ZCL_EXCEL_WRITER_CSV.slnk index 370b7e2..5cf7852 100644 --- a/ZA2X/CLAS/ZCL_EXCEL_WRITER_CSV.slnk +++ b/ZA2X/CLAS/ZCL_EXCEL_WRITER_CSV.slnk @@ -1,6 +1,6 @@ - - + + class ZCL_EXCEL_WRITER_CSV definition public final @@ -18,10 +18,15 @@ public section. class-methods SET_ENCLOSURE importing value(IP_VALUE) type CHAR01 default '"' . - class CL_ABAP_CHAR_UTILITIES definition load . class-methods SET_ENDOFLINE importing - value(IP_VALUE) type ANY default CL_ABAP_CHAR_UTILITIES=>CR_LF . + value(IP_VALUE) type ANY default CL_ABAP_CHAR_UTILITIES=>CR_LF . + class-methods SET_ACTIVE_SHEET_INDEX + importing + !I_ACTIVE_WORKSHEET type ZEXCEL_ACTIVE_WORKSHEET . + class-methods SET_ACTIVE_SHEET_INDEX_BY_NAME + importing + !I_WORKSHEET_NAME type ZEXCEL_WORKSHEETS_NAME . *"* protected components of class ZCL_EXCEL_WRITER_2007 *"* do not include other source files here!!! protected section. @@ -33,6 +38,8 @@ private section. class-data DELIMITER type CHAR01 value ';'. "#EC NOTEXT . class-data ENCLOSURE type CHAR01 value '"'. "#EC NOTEXT . class-data EOL type CHAR01 value CL_ABAP_CHAR_UTILITIES=>CR_LF. "#EC NOTEXT . + class-data WORKSHEET_NAME type ZEXCEL_WORKSHEETS_NAME . + class-data WORKSHEET_INDEX type ZEXCEL_ACTIVE_WORKSHEET . methods CREATE returning @@ -48,18 +55,20 @@ private section. *"* implementation or private method's signature *"* use this source file for any macro definitions you need *"* in the implementation part of the class - - - - + + + + + + method ZIF_EXCEL_WRITER~WRITE_FILE. me->excel = io_excel. ep_file = me->create( ). endmethod. - - + + method CREATE. * .csv format with ; delimiter @@ -68,8 +77,8 @@ endmethod. endmethod. - - + + method CREATE_CSV. TYPES: BEGIN OF lty_format, @@ -124,8 +133,20 @@ endmethod. * STEP 1: Collect strings from the first worksheet lo_iterator = excel->get_worksheets_iterator( ). + data: current_worksheet_title type ZEXCEL_SHEET_TITLE. + WHILE lo_iterator->if_object_collection_iterator~has_next( ) EQ abap_true. lo_worksheet ?= lo_iterator->if_object_collection_iterator~get_next( ). + + IF worksheet_name IS NOT INITIAL. + current_worksheet_title = lo_worksheet->get_title( ). + CHECK current_worksheet_title = worksheet_name. + ELSE. + IF worksheet_index IS INITIAL. + worksheet_index = 1. + ENDIF. + CHECK worksheet_index = sy-index. + ENDIF. APPEND LINES OF lo_worksheet->sheet_content TO lt_cell_data. EXIT. " Take first worksheet only ENDWHILE. @@ -275,20 +296,34 @@ endmethod. endmethod. - - + + + method SET_ACTIVE_SHEET_INDEX. + CLEAR WORKSHEET_NAME. + WORKSHEET_INDEX = i_active_worksheet. +endmethod. + + + + method SET_ACTIVE_SHEET_INDEX_BY_NAME. + CLEAR WORKSHEET_INDEX. + WORKSHEET_NAME = i_worksheet_name. +endmethod. + + + method SET_DELIMITER. delimiter = ip_value. endmethod. - - + + method SET_ENCLOSURE. zcl_excel_writer_csv=>enclosure = ip_value. endmethod. - - + + method SET_ENDOFLINE. zcl_excel_writer_csv=>eol = ip_value. endmethod. diff --git a/ZA2X/PROG/ZDEMO_EXCEL28.slnk b/ZA2X/PROG/ZDEMO_EXCEL28.slnk index 35af7fb..0dc262b 100644 --- a/ZA2X/PROG/ZDEMO_EXCEL28.slnk +++ b/ZA2X/PROG/ZDEMO_EXCEL28.slnk @@ -1,8 +1,8 @@ - + - + *&---------------------------------------------------------------------* @@ -31,7 +31,7 @@ DATA: lv_file_name TYPE string, lv_workdir TYPE string, lv_file_separator TYPE c. -CONSTANTS: lv_default_file_name TYPE string VALUE '01_HelloWorld.csv'. +CONSTANTS: lv_default_file_name TYPE string VALUE '28_HelloWorld.csv'. PARAMETERS: p_path TYPE string. @@ -62,19 +62,47 @@ START-OF-SELECTION. lo_worksheet->set_cell( ip_column = 'B' ip_row = 2 ip_value = 'Hello world' ). lo_worksheet->set_cell( ip_column = 'B' ip_row = 3 ip_value = sy-datum ). lo_worksheet->set_cell( ip_column = 'C' ip_row = 3 ip_value = sy-uzeit ). - lo_hyperlink = zcl_excel_hyperlink=>create_external_link( iv_url = 'https://cw.sdn.sap.com/cw/groups/abap2xlsx' ). - lo_worksheet->set_cell( ip_column = 'B' ip_row = 4 ip_value = 'Click here to visit abap2xlsx homepage' ip_hyperlink = lo_hyperlink ). column_dimension = lo_worksheet->get_column_dimension( 'B' ). column_dimension->set_width( 11 ). + lo_worksheet = lo_excel->add_new_worksheet( ). + lo_worksheet->set_title( ip_title = 'Sheet2' ). + lo_worksheet->set_cell( ip_column = 'B' ip_row = 2 ip_value = 'This is the second sheet' ). + CREATE OBJECT lo_excel_writer TYPE zcl_excel_writer_csv. zcl_excel_writer_csv=>set_delimiter( ip_value = cl_abap_char_utilities=>horizontal_tab ). zcl_excel_writer_csv=>set_enclosure( ip_value = '''' ). zcl_excel_writer_csv=>set_endofline( ip_value = cl_abap_char_utilities=>cr_lf ). + zcl_excel_writer_csv=>set_active_sheet_index( i_active_worksheet = 2 ). +* zcl_excel_writer_csv=>set_active_sheet_index_by_name( I_WORKSHEET_NAME = 'Sheet2' ). + lv_file = lo_excel_writer->write_file( lo_excel ). + " Convert to binary + CALL FUNCTION 'SCMS_XSTRING_TO_BINARY' + EXPORTING + buffer = lv_file + IMPORTING + output_length = lv_bytecount + TABLES + binary_tab = lt_file_tab. +* " This method is only available on AS ABAP > 6.40 +* lt_file_tab = cl_bcs_convert=>xstring_to_solix( iv_xstring = lv_file ). +* lv_bytecount = xstrlen( lv_file ). + + " Save the file + REPLACE FIRST OCCURRENCE OF '.csv' IN lv_full_path WITH '_Sheet2.csv'. + cl_gui_frontend_services=>gui_download( EXPORTING bin_filesize = lv_bytecount + filename = lv_full_path + filetype = 'BIN' + CHANGING data_tab = lt_file_tab ). + +* zcl_excel_writer_csv=>set_active_sheet_index( i_active_worksheet = 2 ). + zcl_excel_writer_csv=>set_active_sheet_index_by_name( I_WORKSHEET_NAME = 'Sheet1' ). + lv_file = lo_excel_writer->write_file( lo_excel ). + REPLACE FIRST OCCURRENCE OF '_Sheet2.csv' IN lv_full_path WITH '_Sheet1.csv'. " Convert to binary CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'