abap2xlsx/ZA2X/PROG/ZDEMO_EXCEL22.slnk
2011-05-30 09:22:54 +00:00

176 lines
8.2 KiB
XML

<?xml version="1.0" encoding="utf-16"?>
<PROG NAME="ZDEMO_EXCEL22" VARCL="X" SUBC="1" CNAM="FEMIA" CDAT="20101220" UNAM="FEMIA" UDAT="20110528" VERN="000037" RSTAT="T" RMAND="001" RLOAD="E" FIXPT="X" SDATE="20110530" STIME="095303" IDATE="20110528" ITIME="181955" UCCHECK="X">
<textPool>
<language SPRAS="E">
<textElement ID="R" ENTRY="abap2xlsx Demo: Export internal table" LENGTH="37 "/>
<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_excel_writer TYPE REF TO zif_excel_writer,
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_file TYPE xstring,
lv_style_guid TYPE zexcel_cell_style,
lv_bytecount TYPE i,
lt_file_tab TYPE solix_tab.
DATA: lv_full_path TYPE string,
lv_workdir TYPE string,
lv_file_separator TYPE c,
lv_row TYPE char10.
FIELD-SYMBOLS: &lt;fs_field_catalog&gt; TYPE zexcel_s_fieldcatalog.
CONSTANTS: lv_default_file_name TYPE string VALUE &apos;22_itab_fieldcatalog.xlsx&apos;.
PARAMETERS: p_path TYPE zexcel_export_dir.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_path.
lv_workdir = p_path.
cl_gui_frontend_services=&gt;directory_browse( EXPORTING initial_folder = lv_workdir
CHANGING selected_folder = lv_workdir ).
p_path = lv_workdir.
INITIALIZATION.
cl_gui_frontend_services=&gt;get_sapgui_workdir( CHANGING sapworkdir = lv_workdir ).
cl_gui_cfw=&gt;flush( ).
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 lv_full_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;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 OBJECT lo_excel_writer TYPE zcl_excel_writer_2007.
lv_file = lo_excel_writer-&gt;write_file( lo_excel ).
&quot; Convert to binary
CALL FUNCTION &apos;SCMS_XSTRING_TO_BINARY&apos;
EXPORTING
buffer = lv_file
IMPORTING
output_length = lv_bytecount
TABLES
binary_tab = lt_file_tab.
* &quot; This method is only available on AS ABAP &gt; 6.40
* 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 = lv_full_path
filetype = &apos;BIN&apos;
CHANGING data_tab = lt_file_tab ).</source>
</PROG>