abap2xlsx/ZA2X/PROG/ZDEMO_EXCEL17.slnk
2010-12-28 10:10:49 +00:00

102 lines
4.9 KiB
XML

<?xml version="1.0" encoding="utf-16"?>
<PROG NAME="ZDEMO_EXCEL17" VARCL="X" SUBC="1" CNAM="FEMIA" CDAT="20100921" UNAM="BCUSER" UDAT="20101228" VERN="000027" RSTAT="T" RMAND="001" RLOAD="E" FIXPT="X" SDATE="20101228" STIME="110557" IDATE="20101228" ITIME="110557" UCCHECK="X">
<textPool>
<language SPRAS="E">
<textElement ID="R" ENTRY="abap2xlsx Demo: Sheet Protection" LENGTH="32 "/>
<textElement ID="S" KEY="P_PATH" ENTRY="D ." LENGTH="24 "/>
<textElement ID="S" KEY="P_PWD" ENTRY="D ." LENGTH="9 "/>
</language>
</textPool>
<programDocumentation/>
<source>*&amp;---------------------------------------------------------------------*
*&amp; Report ZDEMO_EXCEL17
*&amp;
*&amp;---------------------------------------------------------------------*
*&amp;
*&amp;
*&amp;---------------------------------------------------------------------*
REPORT zdemo_excel17.
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_protection TYPE REF TO zcl_excel_style,
lv_style_protection_guid TYPE zexcel_cell_style.
DATA: lv_file TYPE xstring,
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.
CONSTANTS: lv_default_file_name TYPE string VALUE &apos;17_SheetProtection.xlsx&apos;.
PARAMETERS: p_path TYPE zexcel_export_dir,
p_pwd TYPE zexcel_aes_password LOWER CASE DEFAULT &apos;secret&apos;.
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.
CREATE OBJECT lo_excel.
&quot; Get active sheet
lo_worksheet = lo_excel-&gt;get_active_worksheet( ).
lo_worksheet-&gt;zif_excel_sheet_protection~protected = zif_excel_sheet_protection=&gt;c_protected.
* lo_worksheet-&gt;zif_excel_sheet_protection~password = &apos;DAA7&apos;. &quot;it is the encoded word &quot;secret&quot;
lo_worksheet-&gt;zif_excel_sheet_protection~password = zcl_excel_common=&gt;encrypt_password( p_pwd ).
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;Center
lo_style_protection = lo_excel-&gt;add_new_style( ).
lo_style_protection-&gt;protection-&gt;locked = zcl_excel_style_protection=&gt;c_protection_unlocked.
lv_style_protection_guid = lo_style_protection-&gt;get_guid( ).
lo_worksheet-&gt;set_cell( ip_row = 4 ip_column = &apos;C&apos; ip_value = &apos;This cell is unlocked&apos; ip_style = lv_style_protection_guid ).
lo_worksheet-&gt;set_cell( ip_row = 5 ip_column = &apos;C&apos; ip_value = &apos;This cell is locked as all the others empty cell&apos; ).
lo_worksheet-&gt;set_cell( ip_row = 6 ip_column = &apos;C&apos; ip_value = &apos;This cell is unlocked&apos; ip_style = lv_style_protection_guid ).
lo_worksheet-&gt;set_cell( ip_row = 7 ip_column = &apos;C&apos; ip_value = &apos;This cell is unlocked&apos; ip_style = lv_style_protection_guid ).
lo_worksheet-&gt;set_cell( ip_row = 8 ip_column = &apos;C&apos; ip_value = &apos;This cell is locked as all the others empty cell&apos; ).
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>