abap2xlsx/ZA2X/PROG/ZDEMO_EXCEL22.slnk
2012-03-24 10:25:37 +00:00

136 lines
6.4 KiB
XML

<?xml version="1.0" encoding="iso-8859-2"?>
<PROG NAME="ZDEMO_EXCEL22" VARCL="X" SUBC="1" CNAM="FEMIA" CDAT="20101220" UNAM="K2_SCHMOECK" UDAT="20120324" VERN="000041" RSTAT="T" RMAND="001" RLOAD="E" FIXPT="X" SDATE="20120324" STIME="111821" IDATE="20120324" ITIME="111821" UCCHECK="X">
<textPool>
<language SPRAS="E">
<textElement ID="R" ENTRY="abap2xlsx Demo: Export internal table" LENGTH="38 "/>
<textElement ID="S" KEY="P_PATH" ENTRY="D ." LENGTH="9 "/>
</language>
</textPool>
<source>*&amp;---------------------------------------------------------------------*
*&amp; Report ZDEMO_EXCEL22
*&amp;
*&amp;---------------------------------------------------------------------*
*&amp;
*&amp;
*&amp;---------------------------------------------------------------------*
REPORT zdemo_excel22.
DATA: lo_excel TYPE REF TO zcl_excel,
lo_worksheet TYPE REF TO zcl_excel_worksheet,
lo_style TYPE REF TO zcl_excel_style,
lo_style_date TYPE REF TO zcl_excel_style,
lo_style_editable TYPE REF TO zcl_excel_style,
lo_data_validation TYPE REF TO zcl_excel_data_validation.
DATA: lt_field_catalog TYPE zexcel_t_fieldcatalog,
ls_table_settings TYPE zexcel_s_table_settings,
ls_table_settings_out TYPE zexcel_s_table_settings.
DATA: lv_style_guid TYPE zexcel_cell_style.
DATA: lv_row TYPE char10.
FIELD-SYMBOLS: &lt;fs_field_catalog&gt; TYPE zexcel_s_fieldcatalog.
CONSTANTS: gc_save_file_name TYPE string VALUE &apos;22_itab_fieldcatalog.xlsx&apos;.
INCLUDE zdemo_excel_outputopt_incl.
START-OF-SELECTION.
&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;PN_MASSIVE&apos;).
DATA lt_test TYPE TABLE OF sflight.
SELECT * FROM sflight INTO TABLE lt_test. &quot;#EC CI_NOWHERE
&quot; sheet style (white background)
lo_style = lo_excel-&gt;add_new_style( ).
lo_style-&gt;fill-&gt;filltype = zcl_excel_style_fill=&gt;c_fill_solid.
lo_style-&gt;fill-&gt;fgcolor-rgb = zcl_excel_style_color=&gt;c_white.
lv_style_guid = lo_style-&gt;get_guid( ).
&quot; Get active sheet
lo_worksheet = lo_excel-&gt;get_active_worksheet( ).
lo_worksheet-&gt;zif_excel_sheet_properties~set_style( lv_style_guid ).
lo_worksheet-&gt;zif_excel_sheet_protection~protected = zif_excel_sheet_protection=&gt;c_protected.
lo_worksheet-&gt;zif_excel_sheet_protection~password = zcl_excel_common=&gt;encrypt_password( &apos;test&apos; ).
lo_worksheet-&gt;zif_excel_sheet_protection~sheet = zif_excel_sheet_protection=&gt;c_active.
lo_worksheet-&gt;zif_excel_sheet_protection~objects = zif_excel_sheet_protection=&gt;c_active.
lo_worksheet-&gt;zif_excel_sheet_protection~scenarios = zif_excel_sheet_protection=&gt;c_active.
&quot; Create cell style for display only fields
lo_style = lo_excel-&gt;add_new_style( ).
lo_style-&gt;fill-&gt;filltype = zcl_excel_style_fill=&gt;c_fill_solid.
lo_style-&gt;fill-&gt;fgcolor-rgb = zcl_excel_style_color=&gt;c_gray.
lo_style-&gt;number_format-&gt;format_code = zcl_excel_style_number_format=&gt;c_format_text.
&quot; Create cell style for display only date field
lo_style_date = lo_excel-&gt;add_new_style( ).
lo_style_date-&gt;fill-&gt;filltype = zcl_excel_style_fill=&gt;c_fill_solid.
lo_style_date-&gt;fill-&gt;fgcolor-rgb = zcl_excel_style_color=&gt;c_gray.
lo_style_date-&gt;number_format-&gt;format_code = zcl_excel_style_number_format=&gt;c_format_date_ddmmyyyy.
&quot; Create cell style for editable fields
lo_style_editable = lo_excel-&gt;add_new_style( ).
lo_style_editable-&gt;protection-&gt;locked = zcl_excel_style_protection=&gt;c_protection_unlocked.
lt_field_catalog = zcl_excel_common=&gt;get_fieldcatalog( ip_table = lt_test ).
LOOP AT lt_field_catalog ASSIGNING &lt;fs_field_catalog&gt;.
CASE &lt;fs_field_catalog&gt;-fieldname.
WHEN &apos;CARRID&apos;.
&lt;fs_field_catalog&gt;-position = 3.
&lt;fs_field_catalog&gt;-dynpfld = abap_true.
&lt;fs_field_catalog&gt;-style = lo_style-&gt;get_guid( ).
WHEN &apos;CONNID&apos;.
&lt;fs_field_catalog&gt;-position = 1.
&lt;fs_field_catalog&gt;-dynpfld = abap_true.
&lt;fs_field_catalog&gt;-style = lo_style-&gt;get_guid( ).
WHEN &apos;FLDATE&apos;.
&lt;fs_field_catalog&gt;-position = 2.
&lt;fs_field_catalog&gt;-dynpfld = abap_true.
&lt;fs_field_catalog&gt;-style = lo_style_date-&gt;get_guid( ).
WHEN &apos;PRICE&apos;.
&lt;fs_field_catalog&gt;-position = 4.
&lt;fs_field_catalog&gt;-dynpfld = abap_true.
&lt;fs_field_catalog&gt;-style = lo_style_editable-&gt;get_guid( ).
&lt;fs_field_catalog&gt;-totals_function = zcl_excel_table=&gt;totals_function_sum.
WHEN OTHERS.
&lt;fs_field_catalog&gt;-dynpfld = abap_false.
ENDCASE.
ENDLOOP.
ls_table_settings-table_style = zcl_excel_table=&gt;builtinstyle_medium2.
ls_table_settings-show_row_stripes = abap_true.
lo_worksheet-&gt;bind_table( EXPORTING
ip_table = lt_test
it_field_catalog = lt_field_catalog
is_table_settings = ls_table_settings
IMPORTING
es_table_settings = ls_table_settings_out ).
lo_worksheet-&gt;freeze_panes( ip_num_rows = 3 ). &quot;freeze column headers when scrolling
lo_data_validation = lo_worksheet-&gt;add_new_data_validation( ).
lo_data_validation-&gt;type = zcl_excel_data_validation=&gt;c_type_custom.
lv_row = ls_table_settings_out-top_left_row.
CONDENSE lv_row.
CONCATENATE &apos;ISNUMBER(&apos; ls_table_settings_out-top_left_column lv_row &apos;)&apos; INTO lo_data_validation-&gt;formula1.
lo_data_validation-&gt;cell_row = ls_table_settings_out-top_left_row.
lo_data_validation-&gt;cell_column = ls_table_settings_out-top_left_column.
lo_data_validation-&gt;cell_row_to = ls_table_settings_out-bottom_right_row.
lo_data_validation-&gt;cell_column_to = ls_table_settings_out-bottom_right_column.
*** Create output
lcl_output=&gt;output( lo_excel ).</source>
</PROG>