From d3f95ed657bda20b9c5967200203b69da6f854af Mon Sep 17 00:00:00 2001 From: Ivan Femia Date: Thu, 2 Jun 2011 09:23:58 +0000 Subject: [PATCH] Macro-Enabled workbook issue #89 git-svn-id: https://subversion.assembla.com/svn/abap2xlsx/trunk@194 b7d68dce-7c3c-4a99-8ce0-9ea847f5d049 --- ZA2X/CLAS/ZCL_EXCEL.slnk | 101 ++-- ZA2X/CLAS/ZCL_EXCEL_CUSTOMUI.slnk | 25 + ZA2X/CLAS/ZCL_EXCEL_CUSTOMUI_BUTTON.slnk | 28 ++ ZA2X/CLAS/ZCL_EXCEL_PROPERTIES.slnk | 50 +- ZA2X/CLAS/ZCL_EXCEL_READER_2007.slnk | 258 ++++++---- ZA2X/CLAS/ZCL_EXCEL_READER_XLSM.slnk | 161 +++++++ ZA2X/CLAS/ZCL_EXCEL_WORKSHEET.slnk | 16 +- ZA2X/CLAS/ZCL_EXCEL_WRITER_2007.slnk | 124 ++--- ZA2X/CLAS/ZCL_EXCEL_WRITER_XLSM.slnk | 519 +++++++++++++++++++++ ZA2X/INTF/ZIF_EXCEL_BOOK_VBA_PROJECT.slnk | 15 + ZA2X/INTF/ZIF_EXCEL_CUSTOMUI_ELEMENT.slnk | 5 + ZA2X/INTF/ZIF_EXCEL_SHEET_VBA_PROJECT.slnk | 12 + ZA2X/PROG/ZDEMO_EXCEL.slnk | 5 +- ZA2X/PROG/ZDEMO_EXCEL29.slnk | 89 ++++ 14 files changed, 1174 insertions(+), 234 deletions(-) create mode 100644 ZA2X/CLAS/ZCL_EXCEL_CUSTOMUI.slnk create mode 100644 ZA2X/CLAS/ZCL_EXCEL_CUSTOMUI_BUTTON.slnk create mode 100644 ZA2X/CLAS/ZCL_EXCEL_READER_XLSM.slnk create mode 100644 ZA2X/CLAS/ZCL_EXCEL_WRITER_XLSM.slnk create mode 100644 ZA2X/INTF/ZIF_EXCEL_BOOK_VBA_PROJECT.slnk create mode 100644 ZA2X/INTF/ZIF_EXCEL_CUSTOMUI_ELEMENT.slnk create mode 100644 ZA2X/INTF/ZIF_EXCEL_SHEET_VBA_PROJECT.slnk create mode 100644 ZA2X/PROG/ZDEMO_EXCEL29.slnk diff --git a/ZA2X/CLAS/ZCL_EXCEL.slnk b/ZA2X/CLAS/ZCL_EXCEL.slnk index f4f1edd..b245b60 100644 --- a/ZA2X/CLAS/ZCL_EXCEL.slnk +++ b/ZA2X/CLAS/ZCL_EXCEL.slnk @@ -1,16 +1,18 @@ - - + + + class ZCL_EXCEL definition public final create public . +public section. *"* public components of class ZCL_EXCEL *"* do not include other source files here!!! -public section. interfaces ZIF_EXCEL_BOOK_PROTECTION . + interfaces ZIF_EXCEL_BOOK_VBA_PROJECT . data PROPERTIES type ref to ZCL_EXCEL_PROPERTIES . data SECURITY type ref to ZCL_EXCEL_SECURITY . @@ -84,12 +86,12 @@ 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_BOOK_PROTECTION~INITIALIZE. me->zif_excel_book_protection~protected = zif_excel_book_protection=>c_unprotected. @@ -100,9 +102,24 @@ private section. CLEAR me->zif_excel_book_protection~revisionspassword. endmethod. - - - + + method ZIF_EXCEL_BOOK_VBA_PROJECT~SET_CODENAME. + me->zif_excel_book_vba_project~codename = ip_codename. +endmethod. + + + method ZIF_EXCEL_BOOK_VBA_PROJECT~SET_CODENAME_PR. + me->zif_excel_book_vba_project~codename_pr = ip_codename_pr. +endmethod. + + + METHOD zif_excel_book_vba_project~set_vbaproject. + me->zif_excel_book_vba_project~vbaproject = ip_vbaproject. +ENDMETHOD. + + + + method ADD_NEW_DRAWING. DATA: lv_guid TYPE guid_16. * Create default blank worksheet @@ -112,16 +129,16 @@ endmethod. drawings->add( eo_drawing ). endmethod. - - + + method ADD_NEW_RANGE. * Create default blank range CREATE OBJECT eo_range. ranges->add( eo_range ). endmethod. - - + + method ADD_NEW_STYLE. * Create default style CREATE OBJECT eo_style. @@ -129,9 +146,9 @@ endmethod. endmethod. - - - + + + method ADD_NEW_WORKSHEET. DATA: lv_guid TYPE guid_16. * Create default blank worksheet @@ -143,7 +160,7 @@ endmethod. worksheets->active_worksheet = worksheets->size( ). endmethod. - + method CONSTRUCTOR. DATA: lo_worksheet TYPE REF TO zcl_excel_worksheet, lo_style TYPE REF TO zcl_excel_style. @@ -164,30 +181,30 @@ endmethod. lo_style->fill->filltype = zcl_excel_style_fill=>c_fill_pattern_gray125. endmethod. - - + + method GET_ACTIVE_SHEET_INDEX. r_active_worksheet = me->worksheets->active_worksheet. endmethod. - - + + method GET_ACTIVE_WORKSHEET. eo_worksheet = me->worksheets->get( me->worksheets->active_worksheet ). endmethod. - - + + method GET_DRAWINGS_ITERATOR. eo_iterator = me->drawings->get_iterator( ). endmethod. - - + + method GET_NEXT_TABLE_ID. DATA: lo_worksheet TYPE REF TO zcl_excel_worksheet, lo_iterator TYPE REF TO cl_object_collection_iterator, @@ -206,54 +223,54 @@ endmethod. endmethod. - - + + method GET_RANGES_ITERATOR. eo_iterator = me->ranges->get_iterator( ). endmethod. - - + + method GET_STYLES_ITERATOR. eo_iterator = me->styles->get_iterator( ). endmethod. - - + + method GET_WORKSHEETS_ITERATOR. eo_iterator = me->worksheets->get_iterator( ). endmethod. - - + + method GET_WORKSHEETS_NAME. ep_name = me->worksheets->name. endmethod. - - + + method GET_WORKSHEETS_SIZE. ep_size = me->worksheets->size( ). endmethod. - - + + method SET_ACTIVE_SHEET_INDEX. me->worksheets->active_worksheet = i_active_worksheet. endmethod. - - + + method SET_ACTIVE_SHEET_INDEX_BY_NAME. DATA: ws_it TYPE REF TO cl_object_collection_iterator, diff --git a/ZA2X/CLAS/ZCL_EXCEL_CUSTOMUI.slnk b/ZA2X/CLAS/ZCL_EXCEL_CUSTOMUI.slnk new file mode 100644 index 0000000..9bb6669 --- /dev/null +++ b/ZA2X/CLAS/ZCL_EXCEL_CUSTOMUI.slnk @@ -0,0 +1,25 @@ + + + class ZCL_EXCEL_CUSTOMUI definition + public + final + create public . + +public section. +*"* public components of class ZCL_EXCEL_CUSTOMUI +*"* do not include other source files here!!! + protected section. +*"* protected components of class ZCL_EXCEL_CUSTOMUI +*"* do not include other source files here!!! + private section. +*"* private components of class ZCL_EXCEL_CUSTOMUI +*"* do not include other source files here!!! + *"* use this source file for the definition and implementation of +*"* local helper classes, interface definitions and type +*"* declarations + *"* use this source file for any type of declarations (class +*"* definitions, interfaces or type declarations) you need for +*"* components in the private section + *"* use this source file for any macro definitions you need +*"* in the implementation part of the class + diff --git a/ZA2X/CLAS/ZCL_EXCEL_CUSTOMUI_BUTTON.slnk b/ZA2X/CLAS/ZCL_EXCEL_CUSTOMUI_BUTTON.slnk new file mode 100644 index 0000000..d292e70 --- /dev/null +++ b/ZA2X/CLAS/ZCL_EXCEL_CUSTOMUI_BUTTON.slnk @@ -0,0 +1,28 @@ + + + + class ZCL_EXCEL_CUSTOMUI_BUTTON definition + public + final + create public . + +public section. +*"* public components of class ZCL_EXCEL_CUSTOMUI_BUTTON +*"* do not include other source files here!!! + + interfaces ZIF_EXCEL_CUSTOMUI_ELEMENT . + protected section. +*"* protected components of class ZCL_EXCEL_CUSTOMUI_BUTTON +*"* do not include other source files here!!! + private section. +*"* private components of class ZCL_EXCEL_CUSTOMUI_BUTTON +*"* do not include other source files here!!! + *"* use this source file for the definition and implementation of +*"* local helper classes, interface definitions and type +*"* declarations + *"* use this source file for any type of declarations (class +*"* definitions, interfaces or type declarations) you need for +*"* components in the private section + *"* use this source file for any macro definitions you need +*"* in the implementation part of the class + diff --git a/ZA2X/CLAS/ZCL_EXCEL_PROPERTIES.slnk b/ZA2X/CLAS/ZCL_EXCEL_PROPERTIES.slnk index 906064e..bc419ee 100644 --- a/ZA2X/CLAS/ZCL_EXCEL_PROPERTIES.slnk +++ b/ZA2X/CLAS/ZCL_EXCEL_PROPERTIES.slnk @@ -1,24 +1,24 @@ - + class ZCL_EXCEL_PROPERTIES definition public final create public . +public section. *"* public components of class ZCL_EXCEL_PROPERTIES *"* do not include other source files here!!! -public section. - data CREATOR type ZEXCEL_CREATOR value 'Unknown Creator'. "#EC NOTEXT . - data LASTMODIFIEDBY type ZEXCEL_CREATOR value 'Unknown Creator'. "#EC NOTEXT . + data CREATOR type ZEXCEL_CREATOR value 'Ivan Femia'. "#EC NOTEXT . + data LASTMODIFIEDBY type ZEXCEL_CREATOR value 'Ivan Femia'. "#EC NOTEXT . data CREATED type TIMESTAMPL . data MODIFIED type TIMESTAMPL . - data TITLE type ZEXCEL_TITLE value 'Untitled Spreadsheet'. "#EC NOTEXT . + data TITLE type ZEXCEL_TITLE value 'abap2xlsx'. "#EC NOTEXT . data SUBJECT type ZEXCEL_SUBJECT . - data DESCRIPTION type ZEXCEL_DESCRIPTION . + data DESCRIPTION type ZEXCEL_DESCRIPTION value 'Created using abap2xlsx'. "#EC NOTEXT . data KEYWORDS type ZEXCEL_KEYWORDS . data CATEGORY type ZEXCEL_CATEGORY . - data COMPANY type ZEXCEL_COMPANY value 'Microsoft Corporation'. "#EC NOTEXT . + data COMPANY type ZEXCEL_COMPANY value 'abap2xlsx'. "#EC NOTEXT . data APPLICATION type ZEXCEL_APPLICATION value 'Microsoft Excel'. "#EC NOTEXT . data DOCSECURITY type ZEXCEL_DOCSECURITY value '0'. "#EC NOTEXT . data SCALECROP type ZEXCEL_SCALECROP value ''. "#EC NOTEXT . @@ -42,24 +42,24 @@ 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 CONSTRUCTOR. DATA: lv_timestamp TYPE timestampl. diff --git a/ZA2X/CLAS/ZCL_EXCEL_READER_2007.slnk b/ZA2X/CLAS/ZCL_EXCEL_READER_2007.slnk index 0966de3..e6249a4 100644 --- a/ZA2X/CLAS/ZCL_EXCEL_READER_2007.slnk +++ b/ZA2X/CLAS/ZCL_EXCEL_READER_2007.slnk @@ -1,9 +1,37 @@ - - + + + + + + + class ZCL_EXCEL_READER_2007 definition public - final create public . public section. @@ -12,9 +40,67 @@ public section. type-pools IXML . interfaces ZIF_EXCEL_READER . - *"* protected components of class ZCL_EXCEL_READER_2007 + PROTECTED SECTION. +*"* protected components of class ZCL_EXCEL_READER_2007 *"* do not include other source files here!!! -protected section. + + TYPES: + BEGIN OF t_relationship, + id TYPE string, + type TYPE string, + target TYPE string, + END OF t_relationship . + TYPES: + BEGIN OF t_fileversion, + appname TYPE string, + lastedited TYPE string, + lowestedited TYPE string, + rupbuild TYPE string, + codename TYPE string, + END OF t_fileversion . + TYPES: + BEGIN OF t_sheet, + name TYPE string, + sheetid TYPE string, + id TYPE string, + END OF t_sheet . + TYPES: + BEGIN OF t_workbookpr, + codename TYPE string, + defaultthemeversion TYPE string, + END OF t_workbookpr . + TYPES: + BEGIN OF t_sheetpr, + codename TYPE string, + END OF t_sheetpr . + + METHODS fill_struct_from_attributes + IMPORTING + !ip_element TYPE REF TO if_ixml_element + CHANGING + !cp_structure TYPE any . + METHODS get_from_zip_archive + IMPORTING + !i_filename TYPE string + RETURNING + value(r_content) TYPE xstring + RAISING + zcx_excel . + METHODS get_ixml_from_zip_archive + IMPORTING + !i_filename TYPE string + RETURNING + value(r_ixml) TYPE REF TO if_ixml_document + RAISING + zcx_excel . + METHODS load_workbook + IMPORTING + !ip_path TYPE string + !ip_excel TYPE REF TO zcl_excel . + METHODS load_worksheet + IMPORTING + !ip_path TYPE string + !io_worksheet TYPE REF TO zcl_excel_worksheet . private section. *"* private components of class ZCL_EXCEL_READER_2007 *"* do not include other source files here!!! @@ -24,30 +110,10 @@ protected section. data SHARED_STRINGS type STRINGTAB . data STYLES type T_STYLE_REFS . - methods GET_FROM_ZIP_ARCHIVE + methods LOAD_SHARED_STRINGS importing - !I_FILENAME type STRING - returning - value(R_CONTENT) type XSTRING - raising - ZCX_EXCEL . - methods GET_IXML_FROM_ZIP_ARCHIVE - importing - !I_FILENAME type STRING - returning - value(R_IXML) type ref to IF_IXML_DOCUMENT - raising - ZCX_EXCEL . - methods LOAD_WORKSHEET - importing - !IP_PATH type STRING - !IO_WORKSHEET type ref to ZCL_EXCEL_WORKSHEET . - methods FILL_STRUCT_FROM_ATTRIBUTES - importing - !IP_ELEMENT type ref to IF_IXML_ELEMENT - changing - !CP_STRUCTURE type ANY . - methods LOAD_WORKBOOK + !IP_PATH type STRING . + methods LOAD_STYLES importing !IP_PATH type STRING !IP_EXCEL type ref to ZCL_EXCEL . @@ -56,28 +122,21 @@ protected section. !IP_XML type ref to IF_IXML_DOCUMENT returning value(EP_BORDERS) type T_BORDERS . - methods LOAD_STYLE_FONTS - importing - !IP_XML type ref to IF_IXML_DOCUMENT - returning - value(EP_FONTS) type T_FONTS . methods LOAD_STYLE_FILLS importing !IP_XML type ref to IF_IXML_DOCUMENT returning value(EP_FILLS) type T_FILLS . + methods LOAD_STYLE_FONTS + importing + !IP_XML type ref to IF_IXML_DOCUMENT + returning + value(EP_FONTS) type T_FONTS . methods LOAD_STYLE_NUM_FORMATS importing !IP_XML type ref to IF_IXML_DOCUMENT returning - value(EP_NUM_FORMATS) type T_NUM_FORMATS . - methods LOAD_STYLES - importing - !IP_PATH type STRING - !IP_EXCEL type ref to ZCL_EXCEL . - methods LOAD_SHARED_STRINGS - importing - !IP_PATH type STRING . + value(EP_NUM_FORMATS) type T_NUM_FORMATS . *"* local class implementation for public class *"* use this source file for the implementation part of *"* local helper classes @@ -112,12 +171,12 @@ protected section. *"* in the implementation part of the class IXML - - - - + + + + - METHOD zif_excel_reader~load. + method ZIF_EXCEL_READER~LOAD. CONSTANTS: lc_core_properties TYPE string VALUE 'http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties', @@ -151,7 +210,7 @@ protected section. node ?= node->get_next( ). ENDWHILE. -ENDMETHOD. +endmethod. method ZIF_EXCEL_READER~LOAD_FILE. @@ -218,10 +277,10 @@ ENDMETHOD. r_excel = me->zif_excel_reader~load( excel_data ). endmethod. - - - - METHOD fill_struct_from_attributes. + + + + method FILL_STRUCT_FROM_ATTRIBUTES. DATA: name TYPE string, attributes TYPE REF TO if_ixml_named_node_map, attribute TYPE REF TO if_ixml_attribute, @@ -243,12 +302,12 @@ endmethod. ENDIF. attribute ?= iterator->get_next( ). ENDWHILE. -ENDMETHOD. +endmethod. - - - - + + + + method GET_FROM_ZIP_ARCHIVE. IF me->zip IS NOT BOUND. @@ -285,10 +344,10 @@ ENDMETHOD. endmethod. - - - - + + + + method GET_IXML_FROM_ZIP_ARCHIVE. TYPE-POOLS: ixml. @@ -313,9 +372,9 @@ endmethod. endmethod. - - - METHOD load_shared_strings. + + + method LOAD_SHARED_STRINGS. DATA: value TYPE string, shared_strings_xml TYPE REF TO if_ixml_document, tag_name TYPE string, @@ -337,12 +396,12 @@ endmethod. node ?= node->get_next( ). ENDWHILE. -ENDMETHOD. +endmethod. - - - - METHOD load_styles. + + + + method LOAD_STYLES. TYPES: BEGIN OF t_xf, applyalignment TYPE string, applyborder TYPE string, @@ -508,12 +567,12 @@ ENDMETHOD. ENDWHILE. ENDIF. -ENDMETHOD. +endmethod. - - - - METHOD load_style_borders. + + + + method LOAD_STYLE_BORDERS. DATA: node TYPE REF TO if_ixml_element, node2 TYPE REF TO if_ixml_element, node3 TYPE REF TO if_ixml_element, @@ -580,12 +639,12 @@ ENDMETHOD. INSERT cell_border INTO TABLE ep_borders. node ?= node->get_next( ). ENDWHILE. -ENDMETHOD. +endmethod. - - - - METHOD load_style_fills. + + + + method LOAD_STYLE_FILLS. DATA: value TYPE string, node TYPE REF TO if_ixml_element, node2 TYPE REF TO if_ixml_element, @@ -649,12 +708,12 @@ ENDMETHOD. ENDWHILE. -ENDMETHOD. +endmethod. - - - - METHOD load_style_fonts. + + + + method LOAD_STYLE_FONTS. DATA: value TYPE string, node TYPE REF TO if_ixml_element, node2 TYPE REF TO if_ixml_element, @@ -727,12 +786,12 @@ ENDMETHOD. ENDWHILE. -ENDMETHOD. +endmethod. - - - - METHOD LOAD_STYLE_NUM_FORMATS. + + + + method LOAD_STYLE_NUM_FORMATS. DATA: node TYPE REF TO if_ixml_element, num_format TYPE t_num_format. @@ -748,17 +807,12 @@ ENDMETHOD. ENDWHILE. -ENDMETHOD. +endmethod. - - - + + + METHOD load_workbook. - TYPES: BEGIN OF t_sheet, - name TYPE string, - sheetid TYPE string, - id TYPE string, - END OF t_sheet. 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', @@ -854,10 +908,10 @@ ENDMETHOD. ENDWHILE. ENDMETHOD. - - - - METHOD load_worksheet. + + + + method LOAD_WORKSHEET. TYPES: BEGIN OF t_cell, r TYPE string, t TYPE string, @@ -949,6 +1003,6 @@ ENDMETHOD. row ?= iterator->get_next( ). ENDWHILE. -ENDMETHOD. +endmethod. diff --git a/ZA2X/CLAS/ZCL_EXCEL_READER_XLSM.slnk b/ZA2X/CLAS/ZCL_EXCEL_READER_XLSM.slnk new file mode 100644 index 0000000..fa665d7 --- /dev/null +++ b/ZA2X/CLAS/ZCL_EXCEL_READER_XLSM.slnk @@ -0,0 +1,161 @@ + + + class ZCL_EXCEL_READER_XLSM definition + public + inheriting from ZCL_EXCEL_READER_2007 + final + create public . + +public section. +*"* public components of class ZCL_EXCEL_READER_XLSM +*"* do not include other source files here!!! + protected section. +*"* protected components of class ZCL_EXCEL_READER_XLSM +*"* do not include other source files here!!! + + methods LOAD_WORKBOOK + redefinition . + methods LOAD_WORKSHEET + redefinition . + private section. +*"* private components of class ZCL_EXCEL_READER_XLSM +*"* do not include other source files here!!! + + methods LOAD_VBAPROJECT + importing + !IP_PATH type STRING + !IP_EXCEL type ref to ZCL_EXCEL . + *"* use this source file for the definition and implementation of +*"* local helper classes, interface definitions and type +*"* declarations + *"* use this source file for any type of declarations (class +*"* definitions, interfaces or type declarations) you need for +*"* components in the private section + *"* use this source file for any macro definitions you need +*"* in the implementation part of the class + + + + + + + + METHOD load_vbaproject. + + DATA lv_content TYPE xstring. + + lv_content = me->get_from_zip_archive( ip_path ). + + ip_excel->zif_excel_book_vba_project~set_vbaproject( lv_content ). + +ENDMETHOD. + + + METHOD load_workbook. + super->load_workbook( EXPORTING ip_path = ip_path + ip_excel = ip_excel ). + + CONSTANTS: lc_vba_project TYPE string VALUE 'http://schemas.microsoft.com/office/2006/relationships/vbaProject'. + + DATA: rels_workbook_path TYPE string, + rels_workbook TYPE REF TO if_ixml_document, + path TYPE string, + node TYPE REF TO if_ixml_element, + workbook TYPE REF TO if_ixml_document, + stripped_name TYPE chkfile, + dirname TYPE string, + relationship TYPE t_relationship, + fileversion TYPE t_fileversion, + workbookPr TYPE t_workbookPr. + + FIELD-SYMBOLS: <worksheet> TYPE t_relationship. + + CALL FUNCTION 'TRINT_SPLIT_FILE_AND_PATH' + EXPORTING + full_name = ip_path + IMPORTING + stripped_name = stripped_name + file_path = dirname. + + " Read Workbook Relationships + CONCATENATE dirname '_rels/' stripped_name '.rels' + INTO rels_workbook_path. + + rels_workbook = me->get_ixml_from_zip_archive( rels_workbook_path ). + + node ?= rels_workbook->find_from_name( 'Relationship' ). + WHILE node IS BOUND. + me->fill_struct_from_attributes( EXPORTING ip_element = node CHANGING cp_structure = relationship ). + + CASE relationship-type. + WHEN lc_vba_project. + " Read VBA binary + CONCATENATE dirname relationship-target INTO path. + me->load_vbaproject( ip_path = path + ip_excel = ip_excel ). + WHEN OTHERS. + ENDCASE. + + node ?= node->get_next( ). + ENDWHILE. + + " Read Workbook codeName + workbook = me->get_ixml_from_zip_archive( ip_path ). + node ?= workbook->find_from_name( 'fileVersion' ). + IF node IS BOUND. + + fill_struct_from_attributes( EXPORTING ip_element = node + CHANGING cp_structure = fileversion ). + + ip_excel->zif_excel_book_vba_project~set_codename( fileversion-codename ). + ENDIF. + + " Read Workbook codeName + workbook = me->get_ixml_from_zip_archive( ip_path ). + node ?= workbook->find_from_name( 'workbookPr' ). + IF node IS BOUND. + + fill_struct_from_attributes( EXPORTING ip_element = node + CHANGING cp_structure = workbookPr ). + + ip_excel->zif_excel_book_vba_project~set_codename_pr( workbookPr-codename ). + ENDIF. + +ENDMETHOD. + + + METHOD load_worksheet. + + super->load_worksheet( EXPORTING ip_path = ip_path + io_worksheet = io_worksheet ). + + DATA: path TYPE string, + node TYPE REF TO if_ixml_element, + worksheet TYPE REF TO if_ixml_document, + sheetPr TYPE t_sheetPr. + + +* " Read Workbook codeName +* workbook = me->get_ixml_from_zip_archive( ip_path ). +* node ?= workbook->find_from_name( 'fileVersion' ). +* IF node IS BOUND. +* +* fill_struct_from_attributes( EXPORTING ip_element = node +* CHANGING cp_structure = fileversion ). +* +* IO_WORKSHEET->zif_excel_book_vba_project~set_codename( fileversion-codename ). +* ENDIF. + + " Read Workbook codeName + worksheet = me->get_ixml_from_zip_archive( ip_path ). + node ?= worksheet->find_from_name( 'sheetPr' ). + IF node IS BOUND. + + fill_struct_from_attributes( EXPORTING ip_element = node + CHANGING cp_structure = sheetpr ). + + io_worksheet->zif_excel_sheet_vba_project~set_codename_pr( sheetpr-codename ). + ENDIF. +ENDMETHOD. + + diff --git a/ZA2X/CLAS/ZCL_EXCEL_WORKSHEET.slnk b/ZA2X/CLAS/ZCL_EXCEL_WORKSHEET.slnk index f6ce1b7..6491dc9 100644 --- a/ZA2X/CLAS/ZCL_EXCEL_WORKSHEET.slnk +++ b/ZA2X/CLAS/ZCL_EXCEL_WORKSHEET.slnk @@ -1,7 +1,8 @@ - - + + + class ZCL_EXCEL_WORKSHEET definition public final @@ -14,6 +15,7 @@ public section. interfaces ZIF_EXCEL_SHEET_PROPERTIES . interfaces ZIF_EXCEL_SHEET_PROTECTION . + interfaces ZIF_EXCEL_SHEET_VBA_PROJECT . constants C_BREAK_COLUMN type ZEXCEL_BREAK value 2. "#EC NOTEXT constants C_BREAK_NONE type ZEXCEL_BREAK value 0. "#EC NOTEXT @@ -472,6 +474,16 @@ endmethod. me->zif_excel_sheet_protection~sort = zif_excel_sheet_protection=>c_noactive. endmethod. + + + METHOD zif_excel_sheet_vba_project~set_codename. + me->zif_excel_sheet_vba_project~codename = ip_codename. +ENDMETHOD. + + + METHOD zif_excel_sheet_vba_project~set_codename_pr. + me->zif_excel_sheet_vba_project~codename_pr = ip_codename_pr. +ENDMETHOD. diff --git a/ZA2X/CLAS/ZCL_EXCEL_WRITER_2007.slnk b/ZA2X/CLAS/ZCL_EXCEL_WRITER_2007.slnk index 581225e..c6f2a1a 100644 --- a/ZA2X/CLAS/ZCL_EXCEL_WRITER_2007.slnk +++ b/ZA2X/CLAS/ZCL_EXCEL_WRITER_2007.slnk @@ -1,28 +1,22 @@ - + class ZCL_EXCEL_WRITER_2007 definition public - final create public . +public section. *"* public components of class ZCL_EXCEL_WRITER_2007 *"* 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 section. -*"* private components of class ZCL_EXCEL_WRITER_2007 + protected section. +*"* protected components of class ZCL_EXCEL_WRITER_2007 *"* do not include other source files here!!! constants C_CONTENT_TYPES type STRING value '[Content_Types].xml'. "#EC NOTEXT constants C_DOCPROPS_APP type STRING value 'docProps/app.xml'. "#EC NOTEXT constants C_DOCPROPS_CORE type STRING value 'docProps/core.xml'. "#EC NOTEXT - constants C_OFF type STRING value '0'. "#EC NOTEXT - constants C_ON type STRING value '1'. "#EC NOTEXT constants C_RELATIONSHIPS type STRING value '_rels/.rels'. "#EC NOTEXT constants C_XL_DRAWINGS type STRING value 'xl/drawings/drawing#.xml'. "#EC NOTEXT constants C_XL_DRAWINGS_RELS type STRING value 'xl/drawings/_rels/drawing#.xml.rels'. "#EC NOTEXT @@ -34,9 +28,6 @@ protected section. constants C_XL_THEME type STRING value 'xl/theme/theme1.xml'. "#EC NOTEXT constants C_XL_WORKBOOK type STRING value 'xl/workbook.xml'. "#EC NOTEXT data EXCEL type ref to ZCL_EXCEL . - data SHARED_STRINGS type ZEXCEL_T_SHARED_STRING . - data STYLES_COND_MAPPING type ZEXCEL_T_STYLES_COND_MAPPING . - data STYLES_MAPPING type ZEXCEL_T_STYLES_MAPPING . methods CREATE returning @@ -100,7 +91,17 @@ protected section. value(EP_CONTENT) type XSTRING . methods CREATE_XL_WORKBOOK returning - value(EP_CONTENT) type XSTRING . + value(EP_CONTENT) type XSTRING . + private section. +*"* private components of class ZCL_EXCEL_WRITER_2007 +*"* do not include other source files here!!! + + constants C_OFF type STRING value '0'. "#EC NOTEXT + constants C_ON type STRING value '1'. "#EC NOTEXT + data SHARED_STRINGS type ZEXCEL_T_SHARED_STRING . + data STYLES_COND_MAPPING type ZEXCEL_T_STYLES_COND_MAPPING . + data STYLES_MAPPING type ZEXCEL_T_STYLES_MAPPING . + methods FLAG2BOOL importing !IP_FLAG type FLAG @@ -119,22 +120,22 @@ 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 - - - + + + - - - - - - - - - - - + + + + + + + + + + + @@ -144,7 +145,7 @@ protected section. ep_file = me->create( ). endmethod. - + method CREATE. @@ -315,7 +316,7 @@ endmethod. endmethod. - + method CREATE_CONTENT_TYPES. @@ -396,23 +397,6 @@ endmethod. ********************************************************************** * STEP 4: Create subnodes - " Theme node - lo_element = lo_document->create_simple_element( name = lc_xml_node_override - parent = lo_document ). - lo_element->set_attribute_ns( name = lc_xml_attr_partname - value = lc_xml_node_theme_pn ). - lo_element->set_attribute_ns( name = lc_xml_attr_contenttype - value = lc_xml_node_theme_ct ). - lo_element_root->append_child( new_child = lo_element ). - - " Styles node - lo_element = lo_document->create_simple_element( name = lc_xml_node_override - parent = lo_document ). - lo_element->set_attribute_ns( name = lc_xml_attr_partname - value = lc_xml_node_styles_pn ). - lo_element->set_attribute_ns( name = lc_xml_attr_contenttype - value = lc_xml_node_styles_ct ). - lo_element_root->append_child( new_child = lo_element ). " rels node lo_element = lo_document->create_simple_element( name = lc_xml_node_default @@ -432,6 +416,24 @@ endmethod. value = lc_xml_node_xml_ct ). lo_element_root->append_child( new_child = lo_element ). + " Theme node + lo_element = lo_document->create_simple_element( name = lc_xml_node_override + parent = lo_document ). + lo_element->set_attribute_ns( name = lc_xml_attr_partname + value = lc_xml_node_theme_pn ). + lo_element->set_attribute_ns( name = lc_xml_attr_contenttype + value = lc_xml_node_theme_ct ). + lo_element_root->append_child( new_child = lo_element ). + + " Styles node + lo_element = lo_document->create_simple_element( name = lc_xml_node_override + parent = lo_document ). + lo_element->set_attribute_ns( name = lc_xml_attr_partname + value = lc_xml_node_styles_pn ). + lo_element->set_attribute_ns( name = lc_xml_attr_contenttype + value = lc_xml_node_styles_ct ). + lo_element_root->append_child( new_child = lo_element ). + " Workbook node lo_element = lo_document->create_simple_element( name = lc_xml_node_override parent = lo_document ). @@ -569,7 +571,7 @@ endmethod. endmethod. - + method CREATE_DOCPROPS_APP. @@ -790,7 +792,7 @@ endmethod. endmethod. - + method CREATE_DOCPROPS_CORE. @@ -906,7 +908,7 @@ endmethod. endmethod. - + method CREATE_RELATIONSHIPS. @@ -1005,7 +1007,7 @@ endmethod. endmethod. - + method CREATE_XL_DRAWINGS. @@ -1235,7 +1237,7 @@ endmethod. endmethod. - + method CREATE_XL_DRAWINGS_RELS. @@ -1321,7 +1323,7 @@ endmethod. endmethod. - + method CREATE_XL_RELATIONSHIPS. @@ -1469,7 +1471,7 @@ endmethod. endmethod. - + method CREATE_XL_SHAREDSTRINGS. @@ -1586,7 +1588,7 @@ endmethod. endmethod. - + @@ -2829,7 +2831,7 @@ endmethod. endmethod. - + @@ -2974,7 +2976,7 @@ endmethod. endmethod. - + METHOD create_xl_styles. @@ -3852,7 +3854,7 @@ endmethod. ENDMETHOD. - + @@ -3903,7 +3905,7 @@ ENDMETHOD. io_parent->append_child( new_child = lo_sub_element ). endmethod. - + method CREATE_XL_TABLE. @@ -4074,7 +4076,7 @@ endmethod. endmethod. - + method CREATE_XL_THEME. @@ -4163,7 +4165,7 @@ endmethod. endmethod. - + method CREATE_XL_WORKBOOK. diff --git a/ZA2X/CLAS/ZCL_EXCEL_WRITER_XLSM.slnk b/ZA2X/CLAS/ZCL_EXCEL_WRITER_XLSM.slnk new file mode 100644 index 0000000..d5bfd67 --- /dev/null +++ b/ZA2X/CLAS/ZCL_EXCEL_WRITER_XLSM.slnk @@ -0,0 +1,519 @@ + + + class ZCL_EXCEL_WRITER_XLSM definition + public + inheriting from ZCL_EXCEL_WRITER_2007 + final + create public . + +public section. +*"* public components of class ZCL_EXCEL_WRITER_XLSM +*"* do not include other source files here!!! + + methods ZIF_EXCEL_WRITER~WRITE_FILE + redefinition . + protected section. +*"* protected components of class ZCL_EXCEL_WRITER_XLSM +*"* do not include other source files here!!! + + constants C_XL_VBAPROJECT type STRING value 'xl/vbaProject.bin'. "#EC NOTEXT + + methods CREATE + redefinition . + methods CREATE_XL_RELATIONSHIPS + redefinition . + methods CREATE_XL_SHEET + redefinition . + methods CREATE_XL_WORKBOOK + redefinition . + methods CREATE_CONTENT_TYPES + redefinition . + private section. +*"* private components of class ZCL_EXCEL_WRITER_XLSM +*"* do not include other source files here!!! + *"* use this source file for the definition and implementation of +*"* local helper classes, interface definitions and type +*"* declarations + *"* use this source file for any type of declarations (class +*"* definitions, interfaces or type declarations) you need for +*"* components in the private section + *"* 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. + + +* Office 2007 file format is a cab of several xml files with extension .xlsx + + DATA: lo_zip TYPE REF TO cl_abap_zip, + lo_worksheet TYPE REF TO zcl_excel_worksheet, + lo_active_worksheet TYPE REF TO zcl_excel_worksheet, + lo_iterator TYPE REF TO cl_object_collection_iterator, + lo_nested_iterator TYPE REF TO cl_object_collection_iterator, + lo_table TYPE REF TO zcl_excel_table, + lo_drawing TYPE REF TO zcl_excel_drawing, + lo_drawings TYPE REF TO zcl_excel_drawings. + + DATA: lv_content TYPE xstring, + lv_active TYPE flag, + lv_xl_sheet TYPE string, + lv_xl_sheet_rels TYPE string, + lv_xl_drawing TYPE string, + lv_xl_drawing_rels TYPE string, + lv_syindex TYPE string, + lv_value TYPE string, + lv_drawing_index TYPE i. + + +********************************************************************** +* STEP 1: Create archive object file (ZIP) + CREATE OBJECT lo_zip. + +********************************************************************** +* STEP 2: Add [Content_Types].xml to zip + lv_content = me->create_content_types( ). + lo_zip->add( name = me->c_content_types + content = lv_content ). + +********************************************************************** +* STEP 3: Add _rels/.rels to zip + lv_content = me->create_relationships( ). + lo_zip->add( name = me->c_relationships + content = lv_content ). + +********************************************************************** +* STEP 4: Add docProps/app.xml to zip + lv_content = me->create_docprops_app( ). + lo_zip->add( name = me->c_docprops_app + content = lv_content ). + +********************************************************************** +* STEP 5: Add docProps/core.xml to zip + lv_content = me->create_docprops_core( ). + lo_zip->add( name = me->c_docprops_core + content = lv_content ). + +********************************************************************** +* STEP 6: Add xl/_rels/workbook.xml.rels to zip + lv_content = me->create_xl_relationships( ). + lo_zip->add( name = me->c_xl_relationships + content = lv_content ). + +********************************************************************** +* STEP 6: Add xl/_rels/workbook.xml.rels to zip + lv_content = me->create_xl_theme( ). + lo_zip->add( name = me->c_xl_theme + content = lv_content ). + +********************************************************************** +* STEP 7: Add xl/workbook.xml to zip + lv_content = me->create_xl_workbook( ). + lo_zip->add( name = me->c_xl_workbook + content = lv_content ). + +********************************************************************** +* STEP 8: Add xl/workbook.xml to zip +* lv_content = me->create_xl_styles_static( ). + lv_content = me->create_xl_styles( ). + lo_zip->add( name = me->c_xl_styles + content = lv_content ). + +********************************************************************** +* STEP 9: Add sharedStrings.xml to zip + lv_content = me->create_xl_sharedstrings( ). + lo_zip->add( name = me->c_xl_sharedstrings + content = lv_content ). + +********************************************************************** +* STEP 10: Add sheet#.xml and drawing#.xml to zip + lo_iterator = me->excel->get_worksheets_iterator( ). + lo_active_worksheet = me->excel->get_active_worksheet( ). + lv_drawing_index = 1. + + WHILE lo_iterator->if_object_collection_iterator~has_next( ) EQ abap_true. + lo_worksheet ?= lo_iterator->if_object_collection_iterator~get_next( ). + IF lo_active_worksheet->get_guid( ) EQ lo_worksheet->get_guid( ). + lv_active = abap_true. + ELSE. + lv_active = abap_false. + ENDIF. + + lv_content = me->create_xl_sheet( io_worksheet = lo_worksheet + iv_active = lv_active ). + lv_xl_sheet = me->c_xl_sheet. + MOVE sy-index TO lv_syindex. + SHIFT lv_syindex RIGHT DELETING TRAILING space. + SHIFT lv_syindex LEFT DELETING LEADING space. + REPLACE ALL OCCURRENCES OF '#' IN lv_xl_sheet WITH lv_syindex. + lo_zip->add( name = lv_xl_sheet + content = lv_content ). + + lv_xl_sheet_rels = me->c_xl_sheet_rels. + lv_content = me->create_xl_sheet_rels( io_worksheet = lo_worksheet + iv_drawing_index = lv_drawing_index ). + REPLACE ALL OCCURRENCES OF '#' IN lv_xl_sheet_rels WITH lv_syindex. + lo_zip->add( name = lv_xl_sheet_rels + content = lv_content ). + + lo_nested_iterator = lo_worksheet->get_tables_iterator( ). + + WHILE lo_nested_iterator->if_object_collection_iterator~has_next( ) EQ abap_true. + lo_table ?= lo_nested_iterator->if_object_collection_iterator~get_next( ). + lv_content = me->create_xl_table( lo_table ). + + lv_value = lo_table->get_name( ). + CONCATENATE 'xl/tables/' lv_value '.xml' INTO lv_value. + lo_zip->add( name = lv_value + content = lv_content ). + ENDWHILE. + +* Add drawings ********************************** + lo_drawings = lo_worksheet->get_drawings( ). + IF lo_drawings->is_empty( ) = abap_false. + MOVE lv_drawing_index TO lv_syindex. + SHIFT lv_syindex RIGHT DELETING TRAILING space. + SHIFT lv_syindex LEFT DELETING LEADING space. + + lv_content = me->create_xl_drawings( lo_worksheet ). + lv_xl_drawing = me->c_xl_drawings. + REPLACE ALL OCCURRENCES OF '#' IN lv_xl_drawing WITH lv_syindex. + lo_zip->add( name = lv_xl_drawing + content = lv_content ). + + lv_content = me->create_xl_drawings_rels( lo_worksheet ). + lv_xl_drawing_rels = me->c_xl_drawings_rels. + REPLACE ALL OCCURRENCES OF '#' IN lv_xl_drawing_rels WITH lv_syindex. + lo_zip->add( name = lv_xl_drawing_rels + content = lv_content ). + ADD 1 TO lv_drawing_index. + ENDIF. + ENDWHILE. + +********************************************************************** +* STEP 11: Add media + lo_iterator = me->excel->get_drawings_iterator( ). + + WHILE lo_iterator->if_object_collection_iterator~has_next( ) EQ abap_true. + lo_drawing ?= lo_iterator->if_object_collection_iterator~get_next( ). + + lv_content = lo_drawing->get_media( ). + lv_value = lo_drawing->get_media_name( ). + CONCATENATE 'xl/media/' lv_value INTO lv_value. + lo_zip->add( name = lv_value + content = lv_content ). + ENDWHILE. + +********************************************************************** +* STEP 9: Add vbaProject.bin to zip + lo_zip->add( name = me->c_xl_vbaproject + content = me->excel->zif_excel_book_vba_project~vbaproject ). + +********************************************************************** +* STEP 12: Create the final zip + ep_excel = lo_zip->save( ). + +ENDMETHOD. + + + METHOD create_content_types. +** Constant node name + DATA: lc_xml_node_workb_ct TYPE string VALUE 'application/vnd.ms-excel.sheet.macroEnabled.main+xml', + lc_xml_node_default TYPE string VALUE 'Default', + " Node attributes + lc_xml_attr_partname TYPE string VALUE 'PartName', + lc_xml_attr_extension TYPE string VALUE 'Extension', + lc_xml_attr_contenttype TYPE string VALUE 'ContentType', + lc_xml_attr_codename TYPE string VALUE 'codeName', + lc_xml_node_workb_pn TYPE string VALUE '/xl/workbook.xml', + lc_xml_node_bin_ext TYPE string VALUE 'bin', + lc_xml_node_bin_ct TYPE string VALUE 'application/vnd.ms-office.vbaProject'. + + + DATA: lo_ixml TYPE REF TO if_ixml, + lo_document TYPE REF TO if_ixml_document, + lo_document_xml TYPE REF TO cl_xml_document, + lo_element_root TYPE REF TO if_ixml_node, + lo_element TYPE REF TO if_ixml_element, + lo_collection TYPE REF TO if_ixml_node_collection, + lo_iterator TYPE REF TO if_ixml_node_iterator, + lo_node TYPE REF TO if_ixml_node, + lo_encoding TYPE REF TO if_ixml_encoding, + lo_streamfactory TYPE REF TO if_ixml_stream_factory, + lo_ostream TYPE REF TO if_ixml_ostream, + lo_renderer TYPE REF TO if_ixml_renderer. + + DATA: lv_subrc TYPE sysubrc, + lv_contenttype TYPE string, + lv_syindex(2) TYPE c. + +********************************************************************** +* STEP 3: Create standard contentType + ep_content = super->create_content_types( ). + +********************************************************************** +* STEP 2: modify XML adding the extension bin definition + + CREATE OBJECT lo_document_xml. + lv_subrc = lo_document_xml->parse_xstring( ep_content ). + + lo_document ?= lo_document_xml->m_document. + lo_element_root = lo_document->if_ixml_node~get_first_child( ). + + " extension node + lo_element = lo_document->create_simple_element( name = lc_xml_node_default + parent = lo_document ). + lo_element->set_attribute_ns( name = lc_xml_attr_extension + value = lc_xml_node_bin_ext ). + lo_element->set_attribute_ns( name = lc_xml_attr_contenttype + value = lc_xml_node_bin_ct ). + lo_element_root->append_child( new_child = lo_element ). + +********************************************************************** +* STEP 3: modify XML changing the contentType of node Override /xl/workbook.xml + + lo_collection = lo_document->get_elements_by_tag_name( 'Override' ). + lo_iterator = lo_collection->create_iterator( ). + lo_element ?= lo_iterator->get_next( ). + WHILE lo_element IS BOUND. + lv_contenttype = lo_element->get_attribute_ns( lc_xml_attr_partname ). + IF lv_contenttype EQ lc_xml_node_workb_pn. + lo_element->remove_attribute_ns( lc_xml_attr_contenttype ). + lo_element->set_attribute_ns( name = lc_xml_attr_contenttype + value = lc_xml_node_workb_ct ). + EXIT. + ENDIF. + lo_element ?= lo_iterator->get_next( ). + ENDWHILE. + +********************************************************************** +* STEP 3: Create xstring stream + CLEAR ep_content. + lo_ixml = cl_ixml=>create( ). + lo_streamfactory = lo_ixml->create_stream_factory( ). + lo_ostream = lo_streamfactory->create_ostream_xstring( string = ep_content ). + lo_renderer = lo_ixml->create_renderer( ostream = lo_ostream document = lo_document ). + lo_renderer->render( ). + +ENDMETHOD. + + + METHOD create_xl_relationships. + +** Constant node name + DATA: lc_xml_node_relationships TYPE string VALUE 'Relationships', + lc_xml_node_relationship TYPE string VALUE 'Relationship', + " Node attributes + lc_xml_attr_id TYPE string VALUE 'Id', + lc_xml_attr_type TYPE string VALUE 'Type', + lc_xml_attr_target TYPE string VALUE 'Target', + " Node id + lc_xml_node_ridx_id TYPE string VALUE 'rId#', + " Node type + lc_xml_node_rid_vba_tp TYPE string VALUE 'http://schemas.microsoft.com/office/2006/relationships/vbaProject', + " Node target + lc_xml_node_rid_vba_tg TYPE string VALUE 'vbaProject.bin'. + + DATA: lo_ixml TYPE REF TO if_ixml, + lo_document TYPE REF TO if_ixml_document, + lo_document_xml TYPE REF TO cl_xml_document, + lo_element_root TYPE REF TO if_ixml_node, + lo_element TYPE REF TO if_ixml_element, + lo_node TYPE REF TO if_ixml_node, + lo_encoding TYPE REF TO if_ixml_encoding, + lo_streamfactory TYPE REF TO if_ixml_stream_factory, + lo_ostream TYPE REF TO if_ixml_ostream, + lo_renderer TYPE REF TO if_ixml_renderer. + + DATA: lv_xml_node_ridx_tg TYPE string, + lv_xml_node_ridx_id TYPE string, + lv_size TYPE i, + lv_subrc TYPE sysubrc, + lv_syindex(2) TYPE c. + +********************************************************************** +* STEP 3: Create standard relationship + ep_content = super->create_xl_relationships( ). + +********************************************************************** +* STEP 2: modify XML adding the vbaProject relation + + CREATE OBJECT lo_document_xml. + lv_subrc = lo_document_xml->parse_xstring( ep_content ). + + lo_document ?= lo_document_xml->m_document. + lo_element_root = lo_document->if_ixml_node~get_first_child( ). + + + lv_size = excel->get_worksheets_size( ). + + " Relationship node + lo_element = lo_document->create_simple_element( name = lc_xml_node_relationship + parent = lo_document ). + ADD 4 TO lv_size. + lv_syindex = lv_size. + SHIFT lv_syindex RIGHT DELETING TRAILING space. + SHIFT lv_syindex LEFT DELETING LEADING space. + lv_xml_node_ridx_id = lc_xml_node_ridx_id. + REPLACE ALL OCCURRENCES OF '#' IN lv_xml_node_ridx_id WITH lv_syindex. + lo_element->set_attribute_ns( name = lc_xml_attr_id + value = lv_xml_node_ridx_id ). + lo_element->set_attribute_ns( name = lc_xml_attr_type + value = lc_xml_node_rid_vba_tp ). + lo_element->set_attribute_ns( name = lc_xml_attr_target + value = lc_xml_node_rid_vba_tg ). + lo_element_root->append_child( new_child = lo_element ). + +********************************************************************** +* STEP 3: Create xstring stream + CLEAR ep_content. + lo_ixml = cl_ixml=>create( ). + lo_streamfactory = lo_ixml->create_stream_factory( ). + lo_ostream = lo_streamfactory->create_ostream_xstring( string = ep_content ). + lo_renderer = lo_ixml->create_renderer( ostream = lo_ostream document = lo_document ). + lo_renderer->render( ). + +ENDMETHOD. + + + METHOD create_xl_sheet. + +** Constant node name + DATA: lc_xml_attr_codename TYPE string VALUE 'codeName'. + + DATA: lo_ixml TYPE REF TO if_ixml, + lo_document TYPE REF TO if_ixml_document, + lo_document_xml TYPE REF TO cl_xml_document, + lo_element_root TYPE REF TO if_ixml_node, + lo_element TYPE REF TO if_ixml_element, + lo_collection TYPE REF TO if_ixml_node_collection, + lo_iterator TYPE REF TO if_ixml_node_iterator, + lo_node TYPE REF TO if_ixml_node, + lo_encoding TYPE REF TO if_ixml_encoding, + lo_streamfactory TYPE REF TO if_ixml_stream_factory, + lo_ostream TYPE REF TO if_ixml_ostream, + lo_renderer TYPE REF TO if_ixml_renderer. + + DATA: lv_subrc TYPE sysubrc, + lv_syindex(2) TYPE c. + +********************************************************************** +* STEP 3: Create standard relationship + ep_content = super->create_xl_sheet( io_worksheet = io_worksheet + iv_active = iv_active ). + +********************************************************************** +* STEP 2: modify XML adding the vbaProject relation + + CREATE OBJECT lo_document_xml. + lv_subrc = lo_document_xml->parse_xstring( ep_content ). + + lo_document ?= lo_document_xml->m_document. + lo_element_root = lo_document->if_ixml_node~get_first_child( ). + +* lo_collection = lo_document->get_elements_by_tag_name( 'fileVersion' ). +* lo_iterator = lo_collection->create_iterator( ). +* lo_element ?= lo_iterator->get_next( ). +* WHILE lo_element IS BOUND. +* lo_element->set_attribute_ns( name = lc_xml_attr_codename +* value = me->excel->zif_excel_book_vba_project~codename ). +* lo_element ?= lo_iterator->get_next( ). +* ENDWHILE. + + lo_collection = lo_document->get_elements_by_tag_name( 'sheetPr' ). + lo_iterator = lo_collection->create_iterator( ). + lo_element ?= lo_iterator->get_next( ). + WHILE lo_element IS BOUND. + lo_element->set_attribute_ns( name = lc_xml_attr_codename + value = io_worksheet->zif_excel_sheet_vba_project~codename_pr ). + lo_element ?= lo_iterator->get_next( ). + ENDWHILE. + +********************************************************************** +* STEP 3: Create xstring stream + CLEAR ep_content. + lo_ixml = cl_ixml=>create( ). + lo_streamfactory = lo_ixml->create_stream_factory( ). + lo_ostream = lo_streamfactory->create_ostream_xstring( string = ep_content ). + lo_renderer = lo_ixml->create_renderer( ostream = lo_ostream document = lo_document ). + lo_renderer->render( ). +ENDMETHOD. + + + METHOD create_xl_workbook. + +** Constant node name + DATA: lc_xml_attr_codename TYPE string VALUE 'codeName'. + + DATA: lo_ixml TYPE REF TO if_ixml, + lo_document TYPE REF TO if_ixml_document, + lo_document_xml TYPE REF TO cl_xml_document, + lo_element_root TYPE REF TO if_ixml_node, + lo_element TYPE REF TO if_ixml_element, + lo_collection TYPE REF TO if_ixml_node_collection, + lo_iterator TYPE REF TO if_ixml_node_iterator, + lo_node TYPE REF TO if_ixml_node, + lo_encoding TYPE REF TO if_ixml_encoding, + lo_streamfactory TYPE REF TO if_ixml_stream_factory, + lo_ostream TYPE REF TO if_ixml_ostream, + lo_renderer TYPE REF TO if_ixml_renderer. + + DATA: lv_subrc TYPE sysubrc, + lv_syindex(2) TYPE c. + +********************************************************************** +* STEP 3: Create standard relationship + ep_content = super->create_xl_workbook( ). + +********************************************************************** +* STEP 2: modify XML adding the vbaProject relation + + CREATE OBJECT lo_document_xml. + lv_subrc = lo_document_xml->parse_xstring( ep_content ). + + lo_document ?= lo_document_xml->m_document. + lo_element_root = lo_document->if_ixml_node~get_first_child( ). + + lo_collection = lo_document->get_elements_by_tag_name( 'fileVersion' ). + lo_iterator = lo_collection->create_iterator( ). + lo_element ?= lo_iterator->get_next( ). + WHILE lo_element IS BOUND. + lo_element->set_attribute_ns( name = lc_xml_attr_codename + value = me->excel->zif_excel_book_vba_project~codename ). + lo_element ?= lo_iterator->get_next( ). + ENDWHILE. + + lo_collection = lo_document->get_elements_by_tag_name( 'workbookPr' ). + lo_iterator = lo_collection->create_iterator( ). + lo_element ?= lo_iterator->get_next( ). + WHILE lo_element IS BOUND. + lo_element->set_attribute_ns( name = lc_xml_attr_codename + value = me->excel->zif_excel_book_vba_project~codename_pr ). + lo_element ?= lo_iterator->get_next( ). + ENDWHILE. + +********************************************************************** +* STEP 3: Create xstring stream + CLEAR ep_content. + lo_ixml = cl_ixml=>create( ). + lo_streamfactory = lo_ixml->create_stream_factory( ). + lo_ostream = lo_streamfactory->create_ostream_xstring( string = ep_content ). + lo_renderer = lo_ixml->create_renderer( ostream = lo_ostream document = lo_document ). + lo_renderer->render( ). +ENDMETHOD. + + diff --git a/ZA2X/INTF/ZIF_EXCEL_BOOK_VBA_PROJECT.slnk b/ZA2X/INTF/ZIF_EXCEL_BOOK_VBA_PROJECT.slnk new file mode 100644 index 0000000..1a08efe --- /dev/null +++ b/ZA2X/INTF/ZIF_EXCEL_BOOK_VBA_PROJECT.slnk @@ -0,0 +1,15 @@ + + + + + + + + + + + + + + + diff --git a/ZA2X/INTF/ZIF_EXCEL_CUSTOMUI_ELEMENT.slnk b/ZA2X/INTF/ZIF_EXCEL_CUSTOMUI_ELEMENT.slnk new file mode 100644 index 0000000..5694492 --- /dev/null +++ b/ZA2X/INTF/ZIF_EXCEL_CUSTOMUI_ELEMENT.slnk @@ -0,0 +1,5 @@ + + + + + diff --git a/ZA2X/INTF/ZIF_EXCEL_SHEET_VBA_PROJECT.slnk b/ZA2X/INTF/ZIF_EXCEL_SHEET_VBA_PROJECT.slnk new file mode 100644 index 0000000..f9d8887 --- /dev/null +++ b/ZA2X/INTF/ZIF_EXCEL_SHEET_VBA_PROJECT.slnk @@ -0,0 +1,12 @@ + + + + + + + + + + + + diff --git a/ZA2X/PROG/ZDEMO_EXCEL.slnk b/ZA2X/PROG/ZDEMO_EXCEL.slnk index 0a84d57..c5191cd 100644 --- a/ZA2X/PROG/ZDEMO_EXCEL.slnk +++ b/ZA2X/PROG/ZDEMO_EXCEL.slnk @@ -1,5 +1,5 @@ - + @@ -64,5 +64,6 @@ START-OF-SELECTION. SUBMIT zdemo_excel25 AND RETURN. " abap2xlsx Demo: Create and xlsx on Application Server (could be executed in batch mode) SUBMIT zdemo_excel26 WITH p_path = p_path AND RETURN. " abap2xlsx Demo: Reader demo SUBMIT zdemo_excel27 WITH p_path = p_path AND RETURN. " abap2xlsx Demo: Conditional Formatting - SUBMIT zdemo_excel28 WITH p_path = p_path AND RETURN. " abap2xlsx Demo: CSV writer + SUBMIT zdemo_excel28 WITH p_path = p_path AND RETURN. " abap2xlsx Demo: CSV writer +* SUBMIT zdemo_excel29 WITH p_path = p_path AND RETURN. " abap2xlsx Demo: Macro enabled workbook diff --git a/ZA2X/PROG/ZDEMO_EXCEL29.slnk b/ZA2X/PROG/ZDEMO_EXCEL29.slnk new file mode 100644 index 0000000..4d993e8 --- /dev/null +++ b/ZA2X/PROG/ZDEMO_EXCEL29.slnk @@ -0,0 +1,89 @@ + + + + + + + + + *&---------------------------------------------------------------------* +*& Report ZDEMO_EXCEL26 +*& +*&---------------------------------------------------------------------* +*& +*& +*&---------------------------------------------------------------------* + +REPORT zdemo_excel29. + +DATA: lo_excel TYPE REF TO zcl_excel, + lo_excel_writer TYPE REF TO zif_excel_writer, + lo_excel_reader TYPE REF TO zif_excel_reader. + +DATA: lv_file TYPE xstring, + lv_bytecount TYPE i, + lt_file_tab TYPE solix_tab. + +DATA: lv_full_path TYPE string, + lv_filename TYPE string, + lv_workdir TYPE string, + lv_file_separator TYPE c. + +PARAMETERS: p_path TYPE zexcel_export_dir OBLIGATORY. + +AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_path. + + DATA: lt_filetable TYPE filetable, + lv_rc TYPE i. + + cl_gui_frontend_services=>get_sapgui_workdir( CHANGING sapworkdir = lv_workdir ). + cl_gui_cfw=>flush( ). + p_path = lv_workdir. + + CALL METHOD cl_gui_frontend_services=>file_open_dialog + EXPORTING + window_title = 'Select Macro-Enabled Workbook template' + default_extension = '*.xlsm' + file_filter = 'Excel Macro-Enabled Workbook (*.xlsm)|*.xlsm' + initial_directory = lv_workdir + CHANGING + file_table = lt_filetable + rc = lv_rc + EXCEPTIONS + file_open_dialog_failed = 1 + cntl_error = 2 + error_no_gui = 3 + not_supported_by_gui = 4 + OTHERS = 5. + IF sy-subrc <> 0. + MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno + WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4. + ENDIF. + READ TABLE lt_filetable INTO lv_filename INDEX 1. + p_path = lv_filename. + +START-OF-SELECTION. + + lv_full_path = p_path. + + CREATE OBJECT lo_excel_reader TYPE zcl_excel_reader_xlsm. + CREATE OBJECT lo_excel_writer TYPE zcl_excel_writer_xlsm. + lo_excel = lo_excel_reader->load_file( lv_full_path ). + lv_file = lo_excel_writer->write_file( lo_excel ). + REPLACE '.xlsm' IN lv_full_path WITH 'FromReader.xlsm'. + + " Convert to binary + CALL FUNCTION 'SCMS_XSTRING_TO_BINARY' + EXPORTING + buffer = lv_file + IMPORTING + output_length = lv_bytecount + TABLES + binary_tab = lt_file_tab. + + " Save the file + cl_gui_frontend_services=>gui_download( EXPORTING bin_filesize = lv_bytecount + filename = lv_full_path + filetype = 'BIN' + CHANGING data_tab = lt_file_tab ). +