Skip to content

Commit

Permalink
Unicode characters not written to excel (#743)
Browse files Browse the repository at this point in the history
* Fix output of "non-XML" characters 672 and 688

Fix #672 ("vietnamese") and #688 ("emoji").
It works for a recent system but unfortunately I can't test it on a old system to make sure that the new method `is_support_non_xml_characters` returns false.

* Remove useless code and rename variables

* Demo "Hello World" in 10 major languages + Emoji

Co-authored-by: sandraros <sandra.rossi@gmail.com>
Co-authored-by: Gregor Wolf <gregor.wolf@gmail.com>
  • Loading branch information
3 people committed May 21, 2021
1 parent 85dd442 commit 6eac426
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/demos/zdemo_excel1.prog.abap
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,27 @@ START-OF-SELECTION.
lo_hyperlink = zcl_excel_hyperlink=>create_external_link( iv_url = 'https://sapmentors.github.io/abap2xlsx' ).
lo_worksheet->set_cell( ip_column = 'B' ip_row = 4 ip_value = 'Click here to visit abap2xlsx homepage' ip_hyperlink = lo_hyperlink ).

lo_worksheet->set_cell( ip_column = 'B' ip_row = 6 ip_value = '你好,世界' ).
lo_worksheet->set_cell( ip_column = 'C' ip_row = 6 ip_value = '(Chinese)' ).
lo_worksheet->set_cell( ip_column = 'B' ip_row = 7 ip_value = 'नमस्ते दुनिया' ).
lo_worksheet->set_cell( ip_column = 'C' ip_row = 7 ip_value = '(Hindi)' ).
lo_worksheet->set_cell( ip_column = 'B' ip_row = 8 ip_value = 'Hola Mundo' ).
lo_worksheet->set_cell( ip_column = 'C' ip_row = 8 ip_value = '(Spanish)' ).
lo_worksheet->set_cell( ip_column = 'B' ip_row = 9 ip_value = 'مرحبا بالعالم' ).
lo_worksheet->set_cell( ip_column = 'C' ip_row = 9 ip_value = '(Arabic)' ).
lo_worksheet->set_cell( ip_column = 'B' ip_row = 10 ip_value = 'ওহে বিশ্ব ' ).
lo_worksheet->set_cell( ip_column = 'C' ip_row = 10 ip_value = '(Bengali)' ).
lo_worksheet->set_cell( ip_column = 'B' ip_row = 11 ip_value = 'Bonjour le monde' ).
lo_worksheet->set_cell( ip_column = 'C' ip_row = 11 ip_value = '(French)' ).
lo_worksheet->set_cell( ip_column = 'B' ip_row = 12 ip_value = 'Olá Mundo' ).
lo_worksheet->set_cell( ip_column = 'C' ip_row = 12 ip_value = '(Portuguese)' ).
lo_worksheet->set_cell( ip_column = 'B' ip_row = 13 ip_value = 'Привет, мир' ).
lo_worksheet->set_cell( ip_column = 'C' ip_row = 13 ip_value = '(Russian)' ).
lo_worksheet->set_cell( ip_column = 'B' ip_row = 14 ip_value = 'ہیلو دنیا' ).
lo_worksheet->set_cell( ip_column = 'C' ip_row = 14 ip_value = '(Urdu)' ).
lo_worksheet->set_cell( ip_column = 'B' ip_row = 15 ip_value = '👋🌎, 👋🌍, 👋🌏' ).
lo_worksheet->set_cell( ip_column = 'C' ip_row = 15 ip_value = '(Emoji waving hand + 3 parts of the world)' ).

lo_column = lo_worksheet->get_column( ip_column = 'B' ).
lo_column->set_width( ip_width = 11 ).

Expand Down
42 changes: 42 additions & 0 deletions src/zcl_excel_writer_2007.clas.abap
Original file line number Diff line number Diff line change
Expand Up @@ -199,12 +199,16 @@ private section.
constants C_OFF type STRING value '0'. "#EC NOTEXT
constants C_ON type STRING value '1'. "#EC NOTEXT
constants C_XL_PRINTERSETTINGS type STRING value 'xl/printerSettings/printerSettings#.bin'. "#EC NOTEXT
DATA support_non_xml_characters TYPE abap_bool.

methods FLAG2BOOL
importing
!IP_FLAG type FLAG
returning
value(EP_BOOLEAN) type CHAR5 .
METHODS is_support_non_xml_characters
RETURNING
VALUE(r_result) TYPE abap_bool.
ENDCLASS.


Expand All @@ -213,6 +217,7 @@ CLASS ZCL_EXCEL_WRITER_2007 IMPLEMENTATION.

METHOD CONSTRUCTOR.
me->ixml = cl_ixml=>create( ).
me->support_non_xml_characters = is_support_non_xml_characters( ).
ENDMETHOD.


Expand Down Expand Up @@ -7836,6 +7841,7 @@ METHOD render_xml_document.
lo_streamfactory = me->ixml->create_stream_factory( ).
lo_ostream = lo_streamfactory->create_ostream_xstring( string = ep_content ).

IF support_non_xml_characters = abap_false.
" remove illegal characters in the XML according to the note 1750204
" the following method is only available if the corresponding kernel is used
TRY.
Expand All @@ -7845,6 +7851,7 @@ METHOD render_xml_document.
CATCH cx_sy_dyn_call_illegal_method.

ENDTRY.
ENDIF.

lo_renderer = me->ixml->create_renderer( ostream = lo_ostream document = io_document ).
lo_renderer->render( ).
Expand All @@ -7856,4 +7863,39 @@ method ZIF_EXCEL_WRITER~WRITE_FILE.

ep_file = me->create( ).
endmethod.

METHOD is_support_non_xml_characters.
DATA:
"! Unicode character U+10000
lv_u10000 TYPE string,
lo_ixml TYPE REF TO if_ixml,
lo_document TYPE REF TO if_ixml_document,
lo_root TYPE REF TO if_ixml_element,
lo_text TYPE REF TO if_ixml_text,
lo_stream_factory TYPE REF TO if_ixml_stream_factory,
lo_ostream TYPE REF TO if_ixml_ostream,
lv_xstring TYPE xstring,
lo_renderer TYPE REF TO if_ixml_renderer,
lv_rc TYPE i.
CONSTANTS lc_utf8_value_of_u10000 TYPE xstring VALUE 'F0908080'.

lv_u10000 = cl_abap_conv_in_ce=>uccp( 'D800' ) && cl_abap_conv_in_ce=>uccp( 'DC00' ).
lo_ixml = cl_ixml=>create( ).
lo_document = lo_ixml->create_document( ).
lo_root = lo_document->create_simple_element( name = 'ROOT' parent = lo_document ).
lo_text = lo_document->create_text( lv_u10000 ).
lv_rc = lo_root->append_child( lo_text ).
lo_stream_factory = lo_ixml->create_stream_factory( ).
lo_ostream = lo_stream_factory->create_ostream_xstring( string = lv_xstring ).
lo_renderer = lo_ixml->create_renderer( ostream = lo_ostream
document = lo_document ).
lv_rc = lo_renderer->render( ).

FIND lc_utf8_value_of_u10000 IN lv_xstring IN BYTE MODE.
IF sy-subrc = 0.
r_result = abap_true.
ELSE.
r_result = abap_false.
ENDIF.
ENDMETHOD.
ENDCLASS.

0 comments on commit 6eac426

Please sign in to comment.