abap2xlsx/ZA2X/PROG/ZDEMO_EXCEL37.slnk
StefanSchmoecker 600ac8a159 Fixed issue #348: Referenced formulae truncated
CLASS ZCL_EXCEL_COMMON : Extended length of internal field - now we'd really need huuuuuuuge formulae before we truncate ( was afraid to switch to string, no time to analyze if that is feasible )
CLASS ZCL_EXCEL_STYLE_COLOR: Added a method to allow creating rgb-colors with integer input instead of hex-input for the colors
Report ZDEMO_EXCEL37:  Added line to create output when using alternate writerclass ( was buggy after my extension of the report ), added code to save .xlsm templates as .xlsm when downloading, added set-get-parameter for better handling
2015-01-05 13:46:41 +01:00

327 lines
13 KiB
XML

<?xml version="1.0" encoding="utf-8"?>
<PROG NAME="ZDEMO_EXCEL37" VARCL="X" SUBC="1" RSTAT="T" RMAND="100" RLOAD="E" FIXPT="X" UCCHECK="X">
<textPool>
<language SPRAS="D">
<textElement ID="I" KEY="CLS" ENTRY="Auswahl der Reader- und Writerklasse" LENGTH="70 "/>
<textElement ID="I" KEY="DUM" ENTRY="Dump bei unbeh. Exception. Man weiß wenn man das braucht" LENGTH="60 "/>
<textElement ID="I" KEY="ERR" ENTRY="Besondere Schalter" LENGTH="60 "/>
<textElement ID="R" ENTRY="Xlsx-Datei lesen und dann wieder ausgeben = Arbeiten mit Templates" LENGTH="70 "/>
<textElement ID="S" KEY="CB_DUMP" ENTRY=" Dump bei unbeh. Exceptions" LENGTH="36 "/>
<textElement ID="S" KEY="CB_ERRL" ENTRY=" Exceptionposition anzeigen" LENGTH="34 "/>
<textElement ID="S" KEY="LB_READ" ENTRY=" Reader-Klasse" LENGTH="21 "/>
<textElement ID="S" KEY="LB_WRITE" ENTRY=" Writer-Klasse" LENGTH="21 "/>
<textElement ID="S" KEY="P_UPFILE" ENTRY=" Hochzuladende Datei" LENGTH="27 "/>
</language>
<language SPRAS="E">
<textElement ID="I" KEY="CLS" ENTRY="Choose reader- and writerclass to use" LENGTH="70 "/>
<textElement ID="I" KEY="DUM" ENTRY="Dump on uncaught exception - you&apos;ll know when you need this" LENGTH="60 "/>
<textElement ID="I" KEY="ERR" ENTRY="Special switches" LENGTH="60 "/>
<textElement ID="R" ENTRY="Read xlsx-file and output = using templates" LENGTH="43 "/>
<textElement ID="S" KEY="CB_DUMP" ENTRY=" Dump for uncaught exceptions" LENGTH="36 "/>
<textElement ID="S" KEY="CB_ERRL" ENTRY=" Show position of exception" LENGTH="34 "/>
<textElement ID="S" KEY="LB_READ" ENTRY=" Reader-class" LENGTH="20 "/>
<textElement ID="S" KEY="LB_WRITE" ENTRY=" Writer-class" LENGTH="20 "/>
<textElement ID="S" KEY="P_UPFILE" ENTRY=" File to upload" LENGTH="22 "/>
</language>
</textPool>
<source>REPORT zdemo_excel37.
DATA: excel TYPE REF TO zcl_excel,
reader TYPE REF TO zif_excel_reader,
go_error TYPE REF TO cx_root,
gv_memid_gr8 TYPE text255,
gv_message TYPE string,
lv_extension TYPE string,
gv_error_program_name TYPE syrepid,
gv_error_include_name TYPE syrepid,
gv_error_line TYPE i.
DATA: gc_save_file_name TYPE string VALUE &apos;37- Read template and output.&amp;&apos;.
SELECTION-SCREEN BEGIN OF BLOCK blx WITH FRAME.
PARAMETERS: p_upfile TYPE string LOWER CASE MEMORY ID gr8.
SELECTION-SCREEN END OF BLOCK blx.
INCLUDE zdemo_excel_outputopt_incl.
SELECTION-SCREEN BEGIN OF BLOCK cls WITH FRAME TITLE text-cls.
PARAMETERS: lb_read TYPE seoclsname AS LISTBOX VISIBLE LENGTH 40 LOWER CASE OBLIGATORY DEFAULT &apos;Autodetect&apos;(001).
PARAMETERS: lb_write TYPE seoclsname AS LISTBOX VISIBLE LENGTH 40 LOWER CASE OBLIGATORY DEFAULT &apos;Autodetect&apos;(001).
SELECTION-SCREEN END OF BLOCK cls.
SELECTION-SCREEN BEGIN OF BLOCK bl_err WITH FRAME TITLE text-err.
PARAMETERS: cb_errl AS CHECKBOX DEFAULT &apos;X&apos;.
SELECTION-SCREEN BEGIN OF LINE.
PARAMETERS: cb_dump AS CHECKBOX DEFAULT space.
SELECTION-SCREEN COMMENT (60) cmt_dump FOR FIELD cb_dump.
SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN END OF BLOCK bl_err.
INITIALIZATION.
PERFORM setup_listboxes.
cmt_dump = text-dum.
GET PARAMETER ID &apos;GR8&apos; FIELD gv_memid_gr8.
p_upfile = gv_memid_gr8.
IF p_upfile IS INITIAL.
p_upfile = &apos;c:\temp\whatever.xlsx&apos;.
ENDIF.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_upfile.
PERFORM f4_p_upfile CHANGING p_upfile.
START-OF-SELECTION.
IF cb_dump IS INITIAL.
TRY.
PERFORM read_template.
PERFORM write_template.
*** Create output
CATCH cx_root INTO go_error.
MESSAGE &apos;Error reading excelfile&apos; TYPE &apos;I&apos;.
gv_message = go_error-&gt;get_text( ).
IF cb_errl = &apos; &apos;.
IF gv_message IS NOT INITIAL.
MESSAGE gv_message TYPE &apos;I&apos;.
ENDIF.
ELSE.
go_error-&gt;get_source_position( IMPORTING program_name = gv_error_program_name
include_name = gv_error_include_name
source_line = gv_error_line ).
WRITE:/ &apos;Errormessage:&apos; ,gv_message.
WRITE:/ &apos;Errorposition:&apos;,
AT /10 &apos;Program:&apos; ,gv_error_program_name,
AT /10 &apos;include_name:&apos; ,gv_error_include_name,
AT /10 &apos;source_line:&apos; ,gv_error_line.
ENDIF.
ENDTRY.
ELSE. &quot; This will dump if an error occurs. In some cases the information given in cx_root is not helpful - this will show exactly where the problem is
PERFORM read_template.
PERFORM write_template.
ENDIF.
*&amp;---------------------------------------------------------------------*
*&amp; Form F4_P_UPFILE
*&amp;---------------------------------------------------------------------*
FORM f4_p_upfile CHANGING p_upfile TYPE string.
DATA: lv_repid TYPE syrepid,
lt_fields TYPE dynpread_tabtype,
ls_field LIKE LINE OF lt_fields,
lt_files TYPE filetable,
lv_file_filter TYPE string.
lv_repid = sy-repid.
CALL FUNCTION &apos;DYNP_VALUES_READ&apos;
EXPORTING
dyname = lv_repid
dynumb = &apos;1000&apos;
request = &apos;A&apos;
TABLES
dynpfields = lt_fields
EXCEPTIONS
invalid_abapworkarea = 01
invalid_dynprofield = 02
invalid_dynproname = 03
invalid_dynpronummer = 04
invalid_request = 05
no_fielddescription = 06
undefind_error = 07.
READ TABLE lt_fields INTO ls_field WITH KEY fieldname = &apos;P_UPFILE&apos;.
p_upfile = ls_field-fieldvalue.
lv_file_filter = &apos;Excel Files (*.XLSX;*.XLSM)|*.XLSX;*.XLSM&apos;.
cl_gui_frontend_services=&gt;file_open_dialog( EXPORTING
default_filename = p_upfile
file_filter = lv_file_filter
CHANGING
file_table = lt_files
rc = sy-tabix
EXCEPTIONS
OTHERS = 1 ).
READ TABLE lt_files INDEX 1 INTO p_upfile.
ENDFORM. &quot; F4_P_UPFILE
*&amp;---------------------------------------------------------------------*
*&amp; Form SETUP_LISTBOXES
*&amp;---------------------------------------------------------------------*
FORM setup_listboxes .
DATA: lv_id TYPE vrm_id,
lt_values TYPE vrm_values,
lt_implementing_classes TYPE seo_relkeys.
FIELD-SYMBOLS: &lt;ls_implementing_class&gt; LIKE LINE OF lt_implementing_classes,
&lt;ls_value&gt; LIKE LINE OF lt_values.
*--------------------------------------------------------------------*
* Possible READER-Classes
*--------------------------------------------------------------------*
lv_id = &apos;LB_READ&apos;.
APPEND INITIAL LINE TO lt_values ASSIGNING &lt;ls_value&gt;.
&lt;ls_value&gt;-key = &apos;Autodetect&apos;(001).
&lt;ls_value&gt;-text = &apos;Autodetect&apos;(001).
PERFORM get_implementing_classds USING &apos;ZIF_EXCEL_READER&apos;
CHANGING lt_implementing_classes.
CLEAR lt_values.
LOOP AT lt_implementing_classes ASSIGNING &lt;ls_implementing_class&gt;.
APPEND INITIAL LINE TO lt_values ASSIGNING &lt;ls_value&gt;.
&lt;ls_value&gt;-key = &lt;ls_implementing_class&gt;-clsname.
&lt;ls_value&gt;-text = &lt;ls_implementing_class&gt;-clsname.
ENDLOOP.
CALL FUNCTION &apos;VRM_SET_VALUES&apos;
EXPORTING
id = lv_id
values = lt_values
EXCEPTIONS
id_illegal_name = 1
OTHERS = 2.
*--------------------------------------------------------------------*
* Possible WRITER-Classes
*--------------------------------------------------------------------*
lv_id = &apos;LB_WRITE&apos;.
APPEND INITIAL LINE TO lt_values ASSIGNING &lt;ls_value&gt;.
&lt;ls_value&gt;-key = &apos;Autodetect&apos;(001).
&lt;ls_value&gt;-text = &apos;Autodetect&apos;(001).
PERFORM get_implementing_classds USING &apos;ZIF_EXCEL_WRITER&apos;
CHANGING lt_implementing_classes.
CLEAR lt_values.
LOOP AT lt_implementing_classes ASSIGNING &lt;ls_implementing_class&gt;.
APPEND INITIAL LINE TO lt_values ASSIGNING &lt;ls_value&gt;.
&lt;ls_value&gt;-key = &lt;ls_implementing_class&gt;-clsname.
&lt;ls_value&gt;-text = &lt;ls_implementing_class&gt;-clsname.
ENDLOOP.
CALL FUNCTION &apos;VRM_SET_VALUES&apos;
EXPORTING
id = lv_id
values = lt_values
EXCEPTIONS
id_illegal_name = 1
OTHERS = 2.
ENDFORM. &quot; SETUP_LISTBOXES
*&amp;---------------------------------------------------------------------*
*&amp; Form GET_IMPLEMENTING_CLASSDS
*&amp;---------------------------------------------------------------------*
FORM get_implementing_classds USING iv_interface_name TYPE clike
CHANGING ct_implementing_classes TYPE seo_relkeys.
DATA: lo_oo_interface TYPE REF TO cl_oo_interface,
lo_oo_class TYPE REF TO cl_oo_class,
lt_implementing_subclasses TYPE seo_relkeys.
FIELD-SYMBOLS: &lt;ls_implementing_class&gt; LIKE LINE OF ct_implementing_classes.
TRY.
lo_oo_interface ?= cl_oo_interface=&gt;get_instance( iv_interface_name ).
CATCH cx_class_not_existent.
RETURN.
ENDTRY.
ct_implementing_classes = lo_oo_interface-&gt;get_implementing_classes( ).
LOOP AT ct_implementing_classes ASSIGNING &lt;ls_implementing_class&gt;.
TRY.
lo_oo_class ?= cl_oo_class=&gt;get_instance( &lt;ls_implementing_class&gt;-clsname ).
lt_implementing_subclasses = lo_oo_class-&gt;get_subclasses( ).
APPEND LINES OF lt_implementing_subclasses TO ct_implementing_classes.
CATCH cx_class_not_existent.
ENDTRY.
ENDLOOP.
ENDFORM. &quot; GET_IMPLEMENTING_CLASSDS
*&amp;---------------------------------------------------------------------*
*&amp; Form READ_TEMPLATE
*&amp;---------------------------------------------------------------------*
FORM read_template RAISING zcx_excel .
CASE lb_read.
WHEN &apos;Autodetect&apos;(001).
FIND REGEX &apos;(\.xlsx|\.xlsm)\s*$&apos; IN p_upfile SUBMATCHES lv_extension.
TRANSLATE lv_extension TO UPPER CASE.
CASE lv_extension.
WHEN &apos;.XLSX&apos;.
CREATE OBJECT reader TYPE zcl_excel_reader_2007.
excel = reader-&gt;load_file( p_upfile ).
&quot;Use template for charts
excel-&gt;use_template = abap_true.
WHEN &apos;.XLSM&apos;.
CREATE OBJECT reader TYPE zcl_excel_reader_xlsm.
excel = reader-&gt;load_file( p_upfile ).
&quot;Use template for charts
excel-&gt;use_template = abap_true.
WHEN OTHERS.
MESSAGE &apos;Unsupported filetype&apos; TYPE &apos;I&apos;.
RETURN.
ENDCASE.
WHEN OTHERS.
CREATE OBJECT reader TYPE (lb_read).
excel = reader-&gt;load_file( p_upfile ).
&quot;Use template for charts
excel-&gt;use_template = abap_true.
ENDCASE.
ENDFORM. &quot; READ_TEMPLATE
*&amp;---------------------------------------------------------------------*
*&amp; Form WRITE_TEMPLATE
*&amp;---------------------------------------------------------------------*
FORM write_template RAISING zcx_excel.
CASE lb_write.
WHEN &apos;Autodetect&apos;(001).
FIND REGEX &apos;(\.xlsx|\.xlsm)\s*$&apos; IN p_upfile SUBMATCHES lv_extension.
TRANSLATE lv_extension TO UPPER CASE.
CASE lv_extension.
WHEN &apos;.XLSX&apos;.
REPLACE &apos;&amp;&apos; IN gc_save_file_name WITH &apos;xlsx&apos;. &quot; Pass extension for standard writer
lcl_output=&gt;output( excel ).
WHEN &apos;.XLSM&apos;.
REPLACE &apos;&amp;&apos; IN gc_save_file_name WITH &apos;xlsm&apos;. &quot; Pass extension for macro-writer
lcl_output=&gt;output( cl_excel = excel
iv_writerclass_name = &apos;ZCL_EXCEL_WRITER_XLSM&apos; ).
WHEN OTHERS.
MESSAGE &apos;Unsupported filetype&apos; TYPE &apos;I&apos;.
RETURN.
ENDCASE.
WHEN OTHERS.
lcl_output=&gt;output( cl_excel = excel
iv_writerclass_name = lb_write ).
ENDCASE.
ENDFORM. &quot; WRITE_TEMPLATE</source>
</PROG>