abap2xlsx/ZA2X/PROG/ZDEMO_EXCEL2.slnk

118 lines
6.0 KiB
XML

<?xml version="1.0" encoding="utf-16"?>
<PROG NAME="ZDEMO_EXCEL2" VARCL="X" SUBC="1" CNAM="FEMIA" CDAT="20100711" UNAM="FEMIA" UDAT="20100905" VERN="000017" RSTAT="T" RMAND="001" RLOAD="E" FIXPT="X" SDATE="20100905" STIME="163545" IDATE="20100905" ITIME="163545" UCCHECK="X">
<textPool>
<language SPRAS="E">
<textElement ID="R" ENTRY="Test Styles for ABAP2XLSX" LENGTH="25 "/>
</language>
</textPool>
<source>*&amp;---------------------------------------------------------------------*
*&amp; Report ZIFE_TEST_EXCEL
*&amp; Test Styles for ABAP2XLSX
*&amp;---------------------------------------------------------------------*
*&amp;
*&amp;
*&amp;---------------------------------------------------------------------*
REPORT zdemo_excel2.
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_bold TYPE REF TO zcl_excel_style,
lo_style_underline TYPE REF TO zcl_excel_style,
lo_style_filled TYPE REF TO zcl_excel_style,
lo_style_border TYPE REF TO zcl_excel_style,
lo_style_button TYPE REF TO zcl_excel_style,
lo_border_dark TYPE REF TO zcl_excel_style_border,
lo_border_light TYPE REF TO zcl_excel_style_border.
DATA: lv_style_bold_guid TYPE zexcel_cell_style,
lv_style_underline_guid TYPE zexcel_cell_style,
lv_style_filled_guid TYPE zexcel_cell_style,
lv_style_filled_green_guid TYPE zexcel_cell_style,
lv_style_border_guid TYPE zexcel_cell_style,
lv_style_button_guid TYPE zexcel_cell_style.
DATA: lv_file TYPE xstring,
lv_bytecount TYPE i,
lt_file_tab TYPE STANDARD TABLE OF solisti1.
&quot; Creates active sheet
CREATE OBJECT lo_excel.
&quot; Create border object
CREATE OBJECT lo_border_dark.
lo_border_dark-&gt;border_color = zcl_excel_style_color=&gt;c_black.
lo_border_dark-&gt;border_style = zcl_excel_style_border=&gt;c_border_thin.
CREATE OBJECT lo_border_light.
lo_border_light-&gt;border_color = zcl_excel_style_color=&gt;c_gray.
lo_border_light-&gt;border_style = zcl_excel_style_border=&gt;c_border_thin.
&quot; Create a bold / italic style
lo_style_bold = lo_excel-&gt;add_new_style( ).
lo_style_bold-&gt;font-&gt;bold = abap_true.
lo_style_bold-&gt;font-&gt;italic = abap_true.
lo_style_bold-&gt;font-&gt;color = zcl_excel_style_color=&gt;c_red.
lv_style_bold_guid = lo_style_bold-&gt;get_guid( ).
&quot; Create an underline double style
lo_style_underline = lo_excel-&gt;add_new_style( ).
lo_style_underline-&gt;font-&gt;underline = abap_true.
lo_style_underline-&gt;font-&gt;underline_mode = zcl_excel_style_font=&gt;c_underline_double.
lv_style_underline_guid = lo_style_underline-&gt;get_guid( ).
&quot; Create filled style yellow
lo_style_filled = lo_excel-&gt;add_new_style( ).
lo_style_filled-&gt;fill-&gt;filltype = zcl_excel_style_fill=&gt;c_fill_solid.
lo_style_filled-&gt;fill-&gt;fgcolor = zcl_excel_style_color=&gt;c_yellow.
lv_style_filled_guid = lo_style_filled-&gt;get_guid( ).
&quot; Create border with button effects
lo_style_button = lo_excel-&gt;add_new_style( ).
lo_style_button-&gt;borders-&gt;right = lo_border_dark.
lo_style_button-&gt;borders-&gt;down = lo_border_dark.
lo_style_button-&gt;borders-&gt;left = lo_border_light.
lo_style_button-&gt;borders-&gt;top = lo_border_light.
lv_style_button_guid = lo_style_button-&gt;get_guid( ).
&quot;Create style with border
lo_style_border = lo_excel-&gt;add_new_style( ).
lo_style_border-&gt;borders-&gt;allborders = lo_border_dark.
lv_style_border_guid = lo_style_border-&gt;get_guid( ).
&quot; Create filled style green
lo_style_filled = lo_excel-&gt;add_new_style( ).
lo_style_filled-&gt;fill-&gt;filltype = zcl_excel_style_fill=&gt;c_fill_solid.
lo_style_filled-&gt;fill-&gt;fgcolor = zcl_excel_style_color=&gt;c_green.
lv_style_filled_green_guid = lo_style_filled-&gt;get_guid( ).
&quot; Get active sheet
lo_worksheet = lo_excel-&gt;get_active_worksheet( ).
lo_worksheet-&gt;title = &apos;Styles&apos;.
lo_worksheet-&gt;set_cell( ip_column = &apos;B&apos; ip_row = 2 ip_value = &apos;Hello world&apos; ).
lo_worksheet-&gt;set_cell( ip_column = &apos;C&apos; ip_row = 3 ip_value = &apos;Bold text&apos; ip_style = lv_style_bold_guid ).
lo_worksheet-&gt;set_cell( ip_column = &apos;D&apos; ip_row = 4 ip_value = &apos;Underlined text&apos; ip_style = lv_style_underline_guid ).
lo_worksheet-&gt;set_cell( ip_column = &apos;B&apos; ip_row = 5 ip_value = &apos;Filled text&apos; ip_style = lv_style_filled_guid ).
lo_worksheet-&gt;set_cell( ip_column = &apos;C&apos; ip_row = 6 ip_value = &apos;Borders&apos; ip_style = lv_style_border_guid ).
lo_worksheet-&gt;set_cell( ip_column = &apos;D&apos; ip_row = 7 ip_value = &apos;I&apos;&apos;m not a button :)&apos; ip_style = lv_style_button_guid ).
&quot; Fill the cell and apply one style
lo_worksheet-&gt;set_cell( ip_column = &apos;B&apos; ip_row = 6 ip_value = &apos;Filled text&apos; ip_style = lv_style_filled_guid ).
&quot; Change the style
lo_worksheet-&gt;set_cell_style( ip_column = &apos;B&apos; ip_row = 6 ip_style = lv_style_filled_green_guid ).
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; Save the file
CALL FUNCTION &apos;GUI_DOWNLOAD&apos;
EXPORTING
bin_filesize = lv_bytecount
filename = &apos;C:\Styles.xlsx&apos;
filetype = &apos;BIN&apos;
TABLES
data_tab = lt_file_tab.</source>
</PROG>