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
This commit is contained in:
Ivan Femia 2011-04-28 19:38:23 +00:00
parent b380c8ed4a
commit 9b9915e3e8
2 changed files with 300 additions and 0 deletions

View File

@ -0,0 +1,218 @@
<?xml version="1.0" encoding="utf-16"?>
<CLAS CLSNAME="ZCL_EXCEL_WRITER_CSV" VERSION="1" LANGU="E" DESCRIPT="Excel writer 2007" UUID="ADD4B74D93C52E57E1000000C0A8FA28" CATEGORY="00" EXPOSURE="2" STATE="1" RELEASE="0" AUTHOR="JACQUED" CREATEDON="20110420" CHANGEDON="00000000" CHGDANYON="00000000" CLSFINAL="X" CLSCCINCL="X" FIXPT="X" UNICODE="X" R3RELEASE="701" CLSBCCAT="00" DURATION_TYPE="0 " RISK_LEVEL="0 ">
<implementing CLSNAME="ZCL_EXCEL_WRITER_CSV" REFCLSNAME="ZIF_EXCEL_WRITER" VERSION="1" EXPOSURE="2" STATE="1" AUTHOR="FEMIA" CREATEDON="20110428" CHANGEDON="00000000" RELTYPE="1"/>
<publicSection>class ZCL_EXCEL_WRITER_CSV definition
public
final
create public .
*&quot;* public components of class ZCL_EXCEL_WRITER_CSV
*&quot;* do not include other source files here!!!
public section.
interfaces ZIF_EXCEL_WRITER .</publicSection>
<protectedSection>*&quot;* protected components of class ZCL_EXCEL_WRITER_2007
*&quot;* do not include other source files here!!!
protected section.</protectedSection>
<privateSection>*&quot;* private components of class ZCL_EXCEL_WRITER_CSV
*&quot;* 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 .</privateSection>
<localImplementation>*&quot;* local class implementation for public class
*&quot;* use this source file for the implementation part of
*&quot;* local helper classes</localImplementation>
<localTypes>*&quot;* use this source file for any type declarations (class
*&quot;* definitions, interfaces or data types) you need for method
*&quot;* implementation or private method&apos;s signature</localTypes>
<localMacros>*&quot;* use this source file for any macro definitions you need
*&quot;* in the implementation part of the class</localMacros>
<attribute CLSNAME="ZCL_EXCEL_WRITER_CSV" CMPNAME="EXCEL" VERSION="1" LANGU="E" DESCRIPT="Excel creator" EXPOSURE="0" STATE="1" EDITORDER="1 " AUTHOR="FEMIA" CREATEDON="20110428" CHANGEDON="00000000" ATTDECLTYP="0" ATTEXPVIRT="0" TYPTYPE="3" TYPE="ZCL_EXCEL" SRCROW1="0 " SRCCOLUMN1="0 " SRCROW2="0 " SRCCOLUMN2="0 " R3RELEASE="701" TYPESRC_LENG="0 "/>
<interfaceMethod CLSNAME="ZCL_EXCEL_WRITER_CSV" CPDNAME="ZIF_EXCEL_WRITER~WRITE_FILE">
<source>method ZIF_EXCEL_WRITER~WRITE_FILE.
me-&gt;excel = io_excel.
ep_file = me-&gt;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 " AUTHOR="FEMIA" CREATEDON="20110428" CHANGEDON="00000000" MTDTYPE="0" MTDDECLTYP="0" R3RELEASE="701" 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 " AUTHOR="FEMIA" CREATEDON="20110428" CHANGEDON="00000000" PARDECLTYP="3" PARPASSTYP="0" TYPTYPE="1" TYPE="XSTRING"/>
<source>method CREATE.
* .csv format with ; delimiter
ep_excel = me-&gt;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 " AUTHOR="FEMIA" CREATEDON="20110428" CHANGEDON="00000000" MTDTYPE="0" MTDDECLTYP="0" R3RELEASE="701" 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 " AUTHOR="FEMIA" CREATEDON="20110428" CHANGEDON="00000000" 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.
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: &lt;fs_sheet_content&gt; TYPE zexcel_s_cell_data.
* --- Retrieve supported cell format
REFRESH lt_format.
SELECT * INTO CORRESPONDING FIELDS OF TABLE lt_format
FROM seocompodf
WHERE clsname = &apos;ZCL_EXCEL_STYLE_NUMBER_FORMAT&apos;
AND typtype = 1
AND type = &apos;ZEXCEL_NUMBER_FORMAT&apos;.
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-&gt;get_worksheets_iterator( ).
WHILE lo_iterator-&gt;if_object_collection_iterator~has_next( ) EQ abap_true.
lo_worksheet ?= lo_iterator-&gt;if_object_collection_iterator~get_next( ).
APPEND LINES OF lo_worksheet-&gt;sheet_content TO lt_cell_data.
EXIT. &quot; Take first worksheet only
ENDWHILE.
DELETE lt_cell_data WHERE cell_formula IS NOT INITIAL. &quot; 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 &lt;fs_sheet_content&gt;.
* --- Retrieve Cell Style format and data type
CLEAR ls_numfmt.
IF &lt;fs_sheet_content&gt;-data_type IS INITIAL AND &lt;fs_sheet_content&gt;-cell_style IS NOT INITIAL.
lo_iterator = excel-&gt;get_styles_iterator( ).
WHILE lo_iterator-&gt;if_object_collection_iterator~has_next( ) EQ abap_true.
lo_style ?= lo_iterator-&gt;if_object_collection_iterator~get_next( ).
CHECK lo_style-&gt;get_guid( ) = &lt;fs_sheet_content&gt;-cell_style.
ls_numfmt = lo_style-&gt;number_format-&gt;get_structure( ).
EXIT.
ENDWHILE.
ENDIF.
IF &lt;fs_sheet_content&gt;-data_type IS INITIAL AND ls_numfmt IS NOT INITIAL.
&quot; determine data-type
CLEAR lv_attrname.
CONCATENATE &apos;&apos;&apos;&apos; ls_numfmt-NUMFMT &apos;&apos;&apos;&apos; 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 &apos;DATETIME&apos; IN lv_attrname.
IF sy-subrc = 0.
&lt;fs_sheet_content&gt;-data_type = &apos;d&apos;.
ELSE.
FIND FIRST OCCURRENCE OF &apos;TIME&apos; IN lv_attrname.
IF sy-subrc = 0.
&lt;fs_sheet_content&gt;-data_type = &apos;t&apos;.
ELSE.
FIND FIRST OCCURRENCE OF &apos;DATE&apos; IN lv_attrname.
IF sy-subrc = 0.
&lt;fs_sheet_content&gt;-data_type = &apos;d&apos;.
ELSE.
FIND FIRST OCCURRENCE OF &apos;CURRENCY&apos; IN lv_attrname.
IF sy-subrc = 0.
&lt;fs_sheet_content&gt;-data_type = &apos;n&apos;.
ELSE.
FIND FIRST OCCURRENCE OF &apos;NUMBER&apos; IN lv_attrname.
IF sy-subrc = 0.
&lt;fs_sheet_content&gt;-data_type = &apos;n&apos;.
ELSE.
FIND FIRST OCCURRENCE OF &apos;PERCENTAGE&apos; IN lv_attrname.
IF sy-subrc = 0.
&lt;fs_sheet_content&gt;-data_type = &apos;n&apos;.
ENDIF. &quot; Purcentage
ENDIF. &quot; Number
ENDIF. &quot; Currency
ENDIF. &quot; Date
ENDIF. &quot; TIME
ENDIF. &quot; DATETIME
ENDIF. &quot; lv_attrname IS NOT INITIAL.
ENDIF. &quot; &lt;fs_sheet_content&gt;-data_type IS INITIAL AND ls_numfmt IS NOT INITIAL.
* --- Add empty rows
WHILE lv_row &lt; &lt;fs_sheet_content&gt;-cell_row.
CONCATENATE lv_string cl_abap_char_utilities=&gt;newline INTO lv_string.
lv_row = lv_row + 1.
lv_col = 1.
ENDWHILE.
* --- Add empty columns
WHILE lv_col &lt; &lt;fs_sheet_content&gt;-cell_column.
CONCATENATE lv_string &apos;;&apos; INTO lv_string. &quot; ls_shared_string-string_value.
lv_col = lv_col + 1.
ENDWHILE.
* ----- Use format to determine the data type and display format.
CASE &lt;fs_sheet_content&gt;-data_type.
* WHEN &apos;n&apos; OR &apos;N&apos;.
* lc_value = zcl_excel_common=&gt;excel_number_to_string( ip_value = &lt;fs_sheet_content&gt;-cell_value ).
WHEN &apos;d&apos; OR &apos;D&apos;.
lc_value = zcl_excel_common=&gt;excel_string_to_date( ip_value = &lt;fs_sheet_content&gt;-cell_value ).
WHEN &apos;t&apos; OR &apos;T&apos;.
lc_value = zcl_excel_common=&gt;excel_string_to_time( ip_value = &lt;fs_sheet_content&gt;-cell_value ).
WHEN OTHERS.
lc_value = &lt;fs_sheet_content&gt;-cell_value.
ENDCASE.
REPLACE ALL OCCURRENCES OF &apos;&quot;&apos; in lc_value with &apos;&quot;&quot;&apos;.
FIND FIRST OCCURRENCE OF &apos;;&apos; IN lc_value. &quot; &lt;fs_sheet_content&gt;-cell_value.
IF sy-subrc = 0.
CONCATENATE lv_string &apos;&quot;&apos; lc_value &apos;&quot;&apos; INTO lv_string.
ELSE.
CONCATENATE lv_string lc_value INTO lv_string.
ENDIF.
ENDLOOP. &quot; AT lt_cell_data
CLEAR ep_content.
CALL FUNCTION &apos;SCMS_STRING_TO_XSTRING&apos;
EXPORTING
TEXT = lv_string
* MIMETYPE = &apos; &apos;
* ENCODING =
IMPORTING
BUFFER = ep_content
EXCEPTIONS
FAILED = 1
OTHERS = 2
.
endmethod.</source>
</method>
</CLAS>

View File

@ -0,0 +1,82 @@
<?xml version="1.0" encoding="utf-16"?>
<PROG NAME="ZDEMO_EXCEL28" VARCL="X" SUBC="1" CNAM="JACQUED" CDAT="20110427" UNAM="FEMIA" UDAT="20110428" VERN="000017" RSTAT="T" RMAND="001" RLOAD="E" FIXPT="X" SDATE="20110428" STIME="212317" IDATE="20110428" ITIME="212317" UCCHECK="X">
<textPool>
<language SPRAS="E">
<textElement ID="R" LENGTH="27 "/>
</language>
</textPool>
<source>*&amp;---------------------------------------------------------------------*
*&amp; Report ZDEMO_EXCEL28
*&amp;
*&amp;---------------------------------------------------------------------*
*&amp;
*&amp;
*&amp;---------------------------------------------------------------------*
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 &apos;01_HelloWorld.csv&apos;.
PARAMETERS: p_path TYPE string.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_path.
cl_gui_frontend_services=&gt;directory_browse( EXPORTING initial_folder = p_path
CHANGING selected_folder = p_path ).
INITIALIZATION.
cl_gui_frontend_services=&gt;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=&gt;get_file_separator( CHANGING file_separator = lv_file_separator ).
CONCATENATE p_path lv_file_separator lv_default_file_name INTO p_path.
&quot; Creates active sheet
CREATE OBJECT lo_excel.
&quot; Get active sheet
lo_worksheet = lo_excel-&gt;get_active_worksheet( ).
lo_worksheet-&gt;set_title( ip_title = &apos;Sheet1&apos; ).
lo_worksheet-&gt;set_cell( ip_column = &apos;B&apos; ip_row = 2 ip_value = &apos;Hello ;&quot;; world&apos; ).
lo_worksheet-&gt;set_cell( ip_column = &apos;B&apos; ip_row = 3 ip_value = sy-datum ).
lo_worksheet-&gt;set_cell( ip_column = &apos;C&apos; ip_row = 3 ip_value = sy-uzeit ).
lo_hyperlink = zcl_excel_hyperlink=&gt;create_external_link( iv_url = &apos;https://cw.sdn.sap.com/cw/groups/abap2xlsx&apos; ).
lo_worksheet-&gt;set_cell( ip_column = &apos;B&apos; ip_row = 4 ip_value = &apos;Click here to visit abap2xlsx homepage&apos; ip_hyperlink = lo_hyperlink ).
column_dimension = lo_worksheet-&gt;get_column_dimension( &apos;B&apos; ).
column_dimension-&gt;set_width( 11 ).
CREATE OBJECT lo_excel_writer TYPE zcl_excel_writer_csv.
lv_file = lo_excel_writer-&gt;write_file( lo_excel ).
* Convert to binary
lt_file_tab = cl_bcs_convert=&gt;xstring_to_solix( iv_xstring = lv_file ).
lv_bytecount = xstrlen( lv_file ).
&quot; Save the file
cl_gui_frontend_services=&gt;gui_download( EXPORTING bin_filesize = lv_bytecount
filename = p_path
filetype = &apos;BIN&apos;
CHANGING data_tab = lt_file_tab ).</source>
</PROG>