mirror of
https://github.com/abap2xlsx/abap2xlsx.git
synced 2025-05-04 20:28:22 +08:00

Update of all the slinkees files with the latest version of SAPLink to simplify merge of pull request
286 lines
16 KiB
XML
286 lines
16 KiB
XML
<?xml version="1.0" encoding="utf-8"?>
|
|
<CLAS CLSNAME="ZCL_EXCEL_WRITER_CSV" VERSION="1" LANGU="E" DESCRIPT="Excel writer 2007" CATEGORY="00" EXPOSURE="2" STATE="1" RELEASE="0" CLSFINAL="X" CLSCCINCL="X" FIXPT="X" UNICODE="X" CLSBCCAT="00" DURATION_TYPE="0 " RISK_LEVEL="0 ">
|
|
<implementing CLSNAME="ZCL_EXCEL_WRITER_CSV" REFCLSNAME="ZIF_EXCEL_WRITER" VERSION="1" EXPOSURE="2" STATE="1" RELTYPE="1" EDITORDER="0 "/>
|
|
<localImplementation>*"* local class implementation for public class
|
|
*"* use this source file for the implementation part of
|
|
*"* local helper classes</localImplementation>
|
|
<localTypes>*"* use this source file for any type declarations (class
|
|
*"* definitions, interfaces or data types) you need for method
|
|
*"* implementation or private method's signature</localTypes>
|
|
<localMacros>*"* use this source file for any macro definitions you need
|
|
*"* in the implementation part of the class</localMacros>
|
|
<attribute CLSNAME="ZCL_EXCEL_WRITER_CSV" CMPNAME="DELIMITER" VERSION="1" LANGU="E" DESCRIPT="Delimiter" EXPOSURE="0" STATE="1" EDITORDER="2 " ATTDECLTYP="1" ATTVALUE="';'" ATTEXPVIRT="0" TYPTYPE="1" TYPE="CHAR01" SRCROW1="0 " SRCCOLUMN1="0 " SRCROW2="0 " SRCCOLUMN2="0 " TYPESRC_LENG="0 "/>
|
|
<attribute CLSNAME="ZCL_EXCEL_WRITER_CSV" CMPNAME="ENCLOSURE" VERSION="1" LANGU="E" DESCRIPT="Text inclusure character" EXPOSURE="0" STATE="1" EDITORDER="3 " ATTDECLTYP="1" ATTVALUE="'"'" ATTEXPVIRT="0" TYPTYPE="1" TYPE="CHAR01" SRCROW1="0 " SRCCOLUMN1="0 " SRCROW2="0 " SRCCOLUMN2="0 " TYPESRC_LENG="0 "/>
|
|
<attribute CLSNAME="ZCL_EXCEL_WRITER_CSV" CMPNAME="EOL" VERSION="1" LANGU="E" DESCRIPT="End Of line" EXPOSURE="0" STATE="1" EDITORDER="4 " ATTDECLTYP="1" ATTVALUE="CL_ABAP_CHAR_UTILITIES=>CR_LF" ATTEXPVIRT="0" TYPTYPE="1" TYPE="CHAR01" SRCROW1="0 " SRCCOLUMN1="0 " SRCROW2="0 " SRCCOLUMN2="0 " TYPESRC_LENG="0 "/>
|
|
<attribute CLSNAME="ZCL_EXCEL_WRITER_CSV" CMPNAME="EXCEL" VERSION="1" LANGU="E" DESCRIPT="Excel creator" EXPOSURE="0" STATE="1" EDITORDER="1 " ATTDECLTYP="0" ATTEXPVIRT="0" TYPTYPE="3" TYPE="ZCL_EXCEL" SRCROW1="0 " SRCCOLUMN1="0 " SRCROW2="0 " SRCCOLUMN2="0 " TYPESRC_LENG="0 "/>
|
|
<attribute CLSNAME="ZCL_EXCEL_WRITER_CSV" CMPNAME="WORKSHEET_INDEX" VERSION="1" LANGU="E" DESCRIPT="Active Worksheet" EXPOSURE="0" STATE="1" EDITORDER="6 " ATTDECLTYP="1" ATTEXPVIRT="0" TYPTYPE="1" TYPE="ZEXCEL_ACTIVE_WORKSHEET" SRCROW1="0 " SRCCOLUMN1="0 " SRCROW2="0 " SRCCOLUMN2="0 " TYPESRC_LENG="0 "/>
|
|
<attribute CLSNAME="ZCL_EXCEL_WRITER_CSV" CMPNAME="WORKSHEET_NAME" VERSION="1" LANGU="E" DESCRIPT="Worksheets name" EXPOSURE="0" STATE="1" EDITORDER="5 " ATTDECLTYP="1" ATTEXPVIRT="0" TYPTYPE="1" TYPE="ZEXCEL_WORKSHEETS_NAME" SRCROW1="0 " SRCCOLUMN1="0 " SRCROW2="0 " SRCCOLUMN2="0 " TYPESRC_LENG="0 "/>
|
|
<interfaceMethod CLSNAME="ZCL_EXCEL_WRITER_CSV" CPDNAME="ZIF_EXCEL_WRITER~WRITE_FILE">
|
|
<source>method ZIF_EXCEL_WRITER~WRITE_FILE.
|
|
me->excel = io_excel.
|
|
ep_file = me->create( ).
|
|
endmethod.</source>
|
|
</interfaceMethod>
|
|
<method CLSNAME="ZCL_EXCEL_WRITER_CSV" CMPNAME="CREATE" VERSION="1" LANGU="E" DESCRIPT="Create" EXPOSURE="0" STATE="1" EDITORDER="1 " DISPID="0 " MTDTYPE="0" MTDDECLTYP="0" BCMTDCAT="00" BCMTDSYN="0">
|
|
<parameter CLSNAME="ZCL_EXCEL_WRITER_CSV" CMPNAME="CREATE" SCONAME="EP_EXCEL" VERSION="1" LANGU="E" CMPTYPE="1" MTDTYPE="0" EDITORDER="1 " DISPID="0 " PARDECLTYP="3" PARPASSTYP="0" TYPTYPE="1" TYPE="XSTRING"/>
|
|
<source>method CREATE.
|
|
|
|
* .csv format with ; delimiter
|
|
|
|
ep_excel = me->CREATE_CSV( ).
|
|
|
|
endmethod.</source>
|
|
</method>
|
|
<method CLSNAME="ZCL_EXCEL_WRITER_CSV" CMPNAME="CREATE_CSV" VERSION="1" LANGU="E" DESCRIPT="Create CSV ; Delimited format" EXPOSURE="0" STATE="1" EDITORDER="2 " DISPID="0 " MTDTYPE="0" MTDDECLTYP="0" BCMTDCAT="00" BCMTDSYN="0">
|
|
<parameter CLSNAME="ZCL_EXCEL_WRITER_CSV" CMPNAME="CREATE_CSV" SCONAME="EP_CONTENT" VERSION="1" LANGU="E" CMPTYPE="1" MTDTYPE="0" EDITORDER="1 " DISPID="0 " PARDECLTYP="3" PARPASSTYP="0" TYPTYPE="1" TYPE="XSTRING"/>
|
|
<source>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,
|
|
lv_date TYPE DATS,
|
|
lv_tmp TYPE string,
|
|
lv_time TYPE CHAR08.
|
|
|
|
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'.
|
|
|
|
* --- Retrieve SAP date format
|
|
CLEAR ls_format.
|
|
SELECT ddtext INTO ls_format-attvalue FROM dd07t WHERE domname = 'XUDATFM'
|
|
AND ddlanguage = sy-langu.
|
|
ls_format-cmpname = 'DATE'.
|
|
CONDENSE ls_format-attvalue.
|
|
CONCATENATE '''' ls_format-attvalue '''' INTO ls_format-attvalue.
|
|
APPEND ls_format TO lt_format.
|
|
ENDSELECT.
|
|
|
|
|
|
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( ).
|
|
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.
|
|
|
|
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.
|
|
* CONCATENATE lv_string cl_abap_char_utilities=>cr_lf INTO lv_string.
|
|
CONCATENATE lv_string zcl_excel_writer_csv=>eol 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.
|
|
CONCATENATE lv_string zcl_excel_writer_csv=>delimiter INTO lv_string.
|
|
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 ).
|
|
TRY.
|
|
lv_date = lc_value.
|
|
CALL FUNCTION 'CONVERT_DATE_TO_EXTERNAL'
|
|
EXPORTING
|
|
DATE_INTERNAL = lv_date
|
|
IMPORTING
|
|
DATE_EXTERNAL = lv_tmp
|
|
EXCEPTIONS
|
|
DATE_INTERNAL_IS_INVALID = 1
|
|
OTHERS = 2
|
|
.
|
|
IF SY-SUBRC = 0.
|
|
lc_value = lv_tmp.
|
|
ENDIF.
|
|
|
|
CATCH CX_SY_CONVERSION_NO_NUMBER.
|
|
|
|
ENDTRY.
|
|
|
|
WHEN 't' OR 'T'.
|
|
lc_value = zcl_excel_common=>excel_string_to_time( ip_value = <fs_sheet_content>-cell_value ).
|
|
write lc_value to lv_time USING EDIT MASK '__:__:__'.
|
|
lc_value = lv_time.
|
|
WHEN OTHERS.
|
|
lc_value = <fs_sheet_content>-cell_value.
|
|
|
|
ENDCASE.
|
|
|
|
* REPLACE ALL OCCURRENCES OF '"' in lc_value with '""'.
|
|
CONCATENATE zcl_excel_writer_csv=>enclosure zcl_excel_writer_csv=>enclosure INTO lv_tmp.
|
|
CONDENSE lv_tmp.
|
|
REPLACE ALL OCCURRENCES OF zcl_excel_writer_csv=>enclosure in lc_value with lv_tmp.
|
|
|
|
* FIND FIRST OCCURRENCE OF ';' IN lc_value.
|
|
FIND FIRST OCCURRENCE OF zcl_excel_writer_csv=>delimiter IN lc_value.
|
|
IF sy-subrc = 0.
|
|
CONCATENATE lv_string zcl_excel_writer_csv=>enclosure lc_value zcl_excel_writer_csv=>enclosure 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.</source>
|
|
</method>
|
|
<method CLSNAME="ZCL_EXCEL_WRITER_CSV" CMPNAME="SET_ACTIVE_SHEET_INDEX" VERSION="1" LANGU="E" DESCRIPT="Activate worksheet by index before the CSV file creation" EXPOSURE="2" STATE="1" EDITORDER="6 " DISPID="0 " MTDTYPE="0" MTDDECLTYP="1" BCMTDCAT="00" BCMTDSYN="0">
|
|
<parameter CLSNAME="ZCL_EXCEL_WRITER_CSV" CMPNAME="SET_ACTIVE_SHEET_INDEX" SCONAME="I_ACTIVE_WORKSHEET" VERSION="1" LANGU="E" DESCRIPT="Active Worksheet" CMPTYPE="1" MTDTYPE="0" EDITORDER="1 " DISPID="0 " PARDECLTYP="0" PARPASSTYP="1" TYPTYPE="1" TYPE="ZEXCEL_ACTIVE_WORKSHEET"/>
|
|
<source>method SET_ACTIVE_SHEET_INDEX.
|
|
CLEAR WORKSHEET_NAME.
|
|
WORKSHEET_INDEX = i_active_worksheet.
|
|
endmethod.</source>
|
|
</method>
|
|
<method CLSNAME="ZCL_EXCEL_WRITER_CSV" CMPNAME="SET_ACTIVE_SHEET_INDEX_BY_NAME" VERSION="1" LANGU="E" DESCRIPT="Activate worksheet by name before the CSV file creation" EXPOSURE="2" STATE="1" EDITORDER="7 " DISPID="0 " MTDTYPE="0" MTDDECLTYP="1" BCMTDCAT="00" BCMTDSYN="0">
|
|
<parameter CLSNAME="ZCL_EXCEL_WRITER_CSV" CMPNAME="SET_ACTIVE_SHEET_INDEX_BY_NAME" SCONAME="I_WORKSHEET_NAME" VERSION="1" LANGU="E" DESCRIPT="Worksheets name" CMPTYPE="1" MTDTYPE="0" EDITORDER="1 " DISPID="0 " PARDECLTYP="0" PARPASSTYP="1" TYPTYPE="1" TYPE="ZEXCEL_WORKSHEETS_NAME"/>
|
|
<source>method SET_ACTIVE_SHEET_INDEX_BY_NAME.
|
|
CLEAR WORKSHEET_INDEX.
|
|
WORKSHEET_NAME = i_worksheet_name.
|
|
endmethod.</source>
|
|
</method>
|
|
<method CLSNAME="ZCL_EXCEL_WRITER_CSV" CMPNAME="SET_DELIMITER" VERSION="1" LANGU="E" DESCRIPT="Set Delimiter value" EXPOSURE="2" STATE="1" EDITORDER="3 " DISPID="0 " MTDTYPE="0" MTDDECLTYP="1" BCMTDCAT="00" BCMTDSYN="0">
|
|
<parameter CLSNAME="ZCL_EXCEL_WRITER_CSV" CMPNAME="SET_DELIMITER" SCONAME="IP_VALUE" VERSION="1" LANGU="E" DESCRIPT="Delimiter Value" CMPTYPE="1" MTDTYPE="0" EDITORDER="1 " DISPID="0 " PARDECLTYP="0" PARPASSTYP="0" TYPTYPE="1" TYPE="CHAR01" PARVALUE="';'"/>
|
|
<source>method SET_DELIMITER.
|
|
delimiter = ip_value.
|
|
endmethod.</source>
|
|
</method>
|
|
<method CLSNAME="ZCL_EXCEL_WRITER_CSV" CMPNAME="SET_ENCLOSURE" VERSION="1" LANGU="E" DESCRIPT="Set Enclosure character" EXPOSURE="2" STATE="1" EDITORDER="4 " DISPID="0 " MTDTYPE="0" MTDDECLTYP="1" BCMTDCAT="00" BCMTDSYN="0">
|
|
<parameter CLSNAME="ZCL_EXCEL_WRITER_CSV" CMPNAME="SET_ENCLOSURE" SCONAME="IP_VALUE" VERSION="1" LANGU="E" DESCRIPT="Text field enclosure character" CMPTYPE="1" MTDTYPE="0" EDITORDER="1 " DISPID="0 " PARDECLTYP="0" PARPASSTYP="0" TYPTYPE="1" TYPE="CHAR01" PARVALUE="'"'"/>
|
|
<source>method SET_ENCLOSURE.
|
|
zcl_excel_writer_csv=>enclosure = ip_value.
|
|
endmethod.</source>
|
|
</method>
|
|
<method CLSNAME="ZCL_EXCEL_WRITER_CSV" CMPNAME="SET_ENDOFLINE" VERSION="1" LANGU="E" DESCRIPT="Set End Of Line character" EXPOSURE="2" STATE="1" EDITORDER="5 " DISPID="0 " MTDTYPE="0" MTDDECLTYP="1" BCMTDCAT="00" BCMTDSYN="0">
|
|
<parameter CLSNAME="ZCL_EXCEL_WRITER_CSV" CMPNAME="SET_ENDOFLINE" SCONAME="IP_VALUE" VERSION="1" LANGU="E" DESCRIPT="End Of Line Character" CMPTYPE="1" MTDTYPE="0" EDITORDER="1 " DISPID="0 " PARDECLTYP="0" PARPASSTYP="0" TYPTYPE="1" TYPE="ANY" PARVALUE="CL_ABAP_CHAR_UTILITIES=>CR_LF"/>
|
|
<source>method SET_ENDOFLINE.
|
|
zcl_excel_writer_csv=>eol = ip_value.
|
|
endmethod.</source>
|
|
</method>
|
|
</CLAS>
|