From 9b9915e3e854f9add03acf941053c678a2877327 Mon Sep 17 00:00:00 2001 From: Ivan Femia Date: Thu, 28 Apr 2011 19:38:23 +0000 Subject: [PATCH] Alpha version of CSV Writer by Daniel.St-Jacques git-svn-id: https://subversion.assembla.com/svn/abap2xlsx/trunk@168 b7d68dce-7c3c-4a99-8ce0-9ea847f5d049 --- ZA2X/CLAS/ZCL_EXCEL_WRITER_CSV.slnk | 218 ++++++++++++++++++++++++++++ ZA2X/PROG/ZDEMO_EXCEL28.slnk | 82 +++++++++++ 2 files changed, 300 insertions(+) create mode 100644 ZA2X/CLAS/ZCL_EXCEL_WRITER_CSV.slnk create mode 100644 ZA2X/PROG/ZDEMO_EXCEL28.slnk diff --git a/ZA2X/CLAS/ZCL_EXCEL_WRITER_CSV.slnk b/ZA2X/CLAS/ZCL_EXCEL_WRITER_CSV.slnk new file mode 100644 index 0000000..c2b1fa0 --- /dev/null +++ b/ZA2X/CLAS/ZCL_EXCEL_WRITER_CSV.slnk @@ -0,0 +1,218 @@ + + + + class ZCL_EXCEL_WRITER_CSV definition + public + final + create public . + +*"* public components of class ZCL_EXCEL_WRITER_CSV +*"* do not include other source files here!!! +public section. + + interfaces ZIF_EXCEL_WRITER . + *"* protected components of class ZCL_EXCEL_WRITER_2007 +*"* do not include other source files here!!! +protected section. + *"* private components of class ZCL_EXCEL_WRITER_CSV +*"* do not include other source files here!!! +private section. + + data EXCEL type ref to ZCL_EXCEL . + + methods CREATE + returning + value(EP_EXCEL) type XSTRING . + methods CREATE_CSV + returning + value(EP_CONTENT) type XSTRING . + *"* local class implementation for public class +*"* use this source file for the implementation part of +*"* local helper classes + *"* use this source file for any type declarations (class +*"* definitions, interfaces or data types) you need for method +*"* 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 + + ep_excel = me->CREATE_CSV( ). + +endmethod. + + + + method CREATE_CSV. + + TYPES: BEGIN OF lty_format, + cmpname TYPE SEOCMPNAME, + attvalue TYPE SEOVALUE, + END OF lty_format. + DATA: lt_format TYPE STANDARD TABLE OF lty_format, + ls_format LIKE LINE OF lt_format. + + DATA: lo_iterator TYPE REF TO cl_object_collection_iterator, + lo_worksheet TYPE REF TO zcl_excel_worksheet. + + DATA: lt_cell_data TYPE zexcel_t_cell_data_unsorted, + lv_row TYPE sytabix, + lv_col TYPE sytabix, + lv_string TYPE string, + lc_value TYPE string, + lv_attrname TYPE SEOCMPNAME. + + DATA: ls_numfmt TYPE zexcel_s_style_numfmt, + lo_style TYPE REF TO zcl_excel_style. + + FIELD-SYMBOLS: <fs_sheet_content> TYPE zexcel_s_cell_data. + +* --- Retrieve supported cell format + REFRESH lt_format. + SELECT * INTO CORRESPONDING FIELDS OF TABLE lt_format + FROM seocompodf + WHERE clsname = 'ZCL_EXCEL_STYLE_NUMBER_FORMAT' + AND typtype = 1 + AND type = 'ZEXCEL_NUMBER_FORMAT'. + + LOOP AT lt_format INTO ls_format. + TRANSLATE ls_format-attvalue TO UPPER CASE. + MODIFY lt_format FROM ls_format. + ENDLOOP. + +* STEP 1: Collect strings from the first worksheet + lo_iterator = excel->get_worksheets_iterator( ). + WHILE lo_iterator->if_object_collection_iterator~has_next( ) EQ abap_true. + lo_worksheet ?= lo_iterator->if_object_collection_iterator~get_next( ). + APPEND LINES OF lo_worksheet->sheet_content TO lt_cell_data. + EXIT. " Take first worksheet only + ENDWHILE. + + DELETE lt_cell_data WHERE cell_formula IS NOT INITIAL. " delete formula content + + SORT lt_cell_data BY cell_row + cell_column. + lv_row = 1. + lv_col = 1. + CLEAR lv_string. + LOOP AT lt_cell_data ASSIGNING <fs_sheet_content>. + +* --- Retrieve Cell Style format and data type + CLEAR ls_numfmt. + IF <fs_sheet_content>-data_type IS INITIAL AND <fs_sheet_content>-cell_style IS NOT INITIAL. + lo_iterator = excel->get_styles_iterator( ). + WHILE lo_iterator->if_object_collection_iterator~has_next( ) EQ abap_true. + lo_style ?= lo_iterator->if_object_collection_iterator~get_next( ). + CHECK lo_style->get_guid( ) = <fs_sheet_content>-cell_style. + ls_numfmt = lo_style->number_format->get_structure( ). + EXIT. + ENDWHILE. + ENDIF. + IF <fs_sheet_content>-data_type IS INITIAL AND ls_numfmt IS NOT INITIAL. + " determine data-type + CLEAR lv_attrname. + CONCATENATE '''' ls_numfmt-NUMFMT '''' INTO ls_numfmt-NUMFMT. + TRANSLATE ls_numfmt-numfmt TO UPPER CASE. + READ TABLE lt_format INTO ls_format WITH KEY attvalue = ls_numfmt-NUMFMT. + IF sy-subrc = 0. + lv_attrname = ls_format-cmpname. + ENDIF. + + IF lv_attrname IS NOT INITIAL. + FIND FIRST OCCURRENCE OF 'DATETIME' IN lv_attrname. + IF sy-subrc = 0. + <fs_sheet_content>-data_type = 'd'. + ELSE. + FIND FIRST OCCURRENCE OF 'TIME' IN lv_attrname. + IF sy-subrc = 0. + <fs_sheet_content>-data_type = 't'. + ELSE. + FIND FIRST OCCURRENCE OF 'DATE' IN lv_attrname. + IF sy-subrc = 0. + <fs_sheet_content>-data_type = 'd'. + ELSE. + FIND FIRST OCCURRENCE OF 'CURRENCY' IN lv_attrname. + IF sy-subrc = 0. + <fs_sheet_content>-data_type = 'n'. + ELSE. + FIND FIRST OCCURRENCE OF 'NUMBER' IN lv_attrname. + IF sy-subrc = 0. + <fs_sheet_content>-data_type = 'n'. + ELSE. + FIND FIRST OCCURRENCE OF 'PERCENTAGE' IN lv_attrname. + IF sy-subrc = 0. + <fs_sheet_content>-data_type = 'n'. + ENDIF. " Purcentage + ENDIF. " Number + ENDIF. " Currency + ENDIF. " Date + ENDIF. " TIME + ENDIF. " DATETIME + ENDIF. " lv_attrname IS NOT INITIAL. + ENDIF. " <fs_sheet_content>-data_type IS INITIAL AND ls_numfmt IS NOT INITIAL. + +* --- Add empty rows + WHILE lv_row < <fs_sheet_content>-cell_row. + CONCATENATE lv_string cl_abap_char_utilities=>newline INTO lv_string. + lv_row = lv_row + 1. + lv_col = 1. + ENDWHILE. + +* --- Add empty columns + WHILE lv_col < <fs_sheet_content>-cell_column. + CONCATENATE lv_string ';' INTO lv_string. " ls_shared_string-string_value. + lv_col = lv_col + 1. + ENDWHILE. + +* ----- Use format to determine the data type and display format. + CASE <fs_sheet_content>-data_type. +* WHEN 'n' OR 'N'. +* lc_value = zcl_excel_common=>excel_number_to_string( ip_value = <fs_sheet_content>-cell_value ). + + WHEN 'd' OR 'D'. + lc_value = zcl_excel_common=>excel_string_to_date( ip_value = <fs_sheet_content>-cell_value ). + + WHEN 't' OR 'T'. + lc_value = zcl_excel_common=>excel_string_to_time( ip_value = <fs_sheet_content>-cell_value ). + + WHEN OTHERS. + lc_value = <fs_sheet_content>-cell_value. + + ENDCASE. + REPLACE ALL OCCURRENCES OF '"' in lc_value with '""'. + FIND FIRST OCCURRENCE OF ';' IN lc_value. " <fs_sheet_content>-cell_value. + IF sy-subrc = 0. + CONCATENATE lv_string '"' lc_value '"' INTO lv_string. + ELSE. + CONCATENATE lv_string lc_value INTO lv_string. + ENDIF. + + ENDLOOP. " AT lt_cell_data + + CLEAR ep_content. + + CALL FUNCTION 'SCMS_STRING_TO_XSTRING' + EXPORTING + TEXT = lv_string +* MIMETYPE = ' ' +* ENCODING = + IMPORTING + BUFFER = ep_content + EXCEPTIONS + FAILED = 1 + OTHERS = 2 + . + +endmethod. + + diff --git a/ZA2X/PROG/ZDEMO_EXCEL28.slnk b/ZA2X/PROG/ZDEMO_EXCEL28.slnk new file mode 100644 index 0000000..9e9e83c --- /dev/null +++ b/ZA2X/PROG/ZDEMO_EXCEL28.slnk @@ -0,0 +1,82 @@ + + + + + + + + *&---------------------------------------------------------------------* +*& Report ZDEMO_EXCEL28 +*& +*&---------------------------------------------------------------------* +*& +*& +*&---------------------------------------------------------------------* + +REPORT zdemo_excel28. + +DATA: lo_excel TYPE REF TO zcl_excel, + lo_excel_writer TYPE REF TO zif_excel_writer, + lo_worksheet TYPE REF TO zcl_excel_worksheet, + lo_hyperlink TYPE REF TO zcl_excel_hyperlink, + column_dimension TYPE REF TO zcl_excel_worksheet_columndime. + +DATA: lv_file TYPE xstring, + lv_bytecount TYPE i, + lt_file_tab TYPE solix_tab. + +DATA: lv_file_name TYPE string, + lv_file_path TYPE string, + lv_full_path TYPE string, + lv_workdir TYPE string, + lv_file_separator TYPE c. + +CONSTANTS: lv_default_file_name TYPE string VALUE '01_HelloWorld.csv'. + +PARAMETERS: p_path TYPE string. + +AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_path. + + cl_gui_frontend_services=>directory_browse( EXPORTING initial_folder = p_path + CHANGING selected_folder = p_path ). + +INITIALIZATION. + cl_gui_frontend_services=>get_sapgui_workdir( CHANGING sapworkdir = lv_workdir ). + p_path = lv_workdir. + +START-OF-SELECTION. + + IF p_path IS INITIAL. + p_path = lv_workdir. + ENDIF. + cl_gui_frontend_services=>get_file_separator( CHANGING file_separator = lv_file_separator ). + CONCATENATE p_path lv_file_separator lv_default_file_name INTO p_path. + + " Creates active sheet + CREATE OBJECT lo_excel. + + " Get active sheet + lo_worksheet = lo_excel->get_active_worksheet( ). + lo_worksheet->set_title( ip_title = 'Sheet1' ). + 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 ). + + CREATE OBJECT lo_excel_writer TYPE zcl_excel_writer_csv. + lv_file = lo_excel_writer->write_file( lo_excel ). + +* Convert to binary + lt_file_tab = cl_bcs_convert=>xstring_to_solix( iv_xstring = lv_file ). + lv_bytecount = xstrlen( lv_file ). + + " Save the file + cl_gui_frontend_services=>gui_download( EXPORTING bin_filesize = lv_bytecount + filename = p_path + filetype = 'BIN' + CHANGING data_tab = lt_file_tab ). +