mirror of
https://github.com/abap2xlsx/abap2xlsx.git
synced 2025-05-05 13:46:17 +08:00
Demoreport 34 - static styles: This demonstrates the advantages of static styles better then the previous version by "building" a cell's style step by step as needed.
git-svn-id: https://subversion.assembla.com/svn/abap2xlsx/trunk@282 b7d68dce-7c3c-4a99-8ce0-9ea847f5d049
This commit is contained in:
parent
195e762394
commit
d72a920d2a
|
@ -1,8 +1,8 @@
|
||||||
<?xml version="1.0" encoding="utf-16"?>
|
<?xml version="1.0" encoding="iso-8859-2"?>
|
||||||
<PROG NAME="ZDEMO_EXCEL34" VARCL="X" SUBC="1" CNAM="FEMIA" CDAT="20120319" UNAM="FEMIA" UDAT="20120319" VERN="000013" RSTAT="T" RMAND="001" RLOAD="E" FIXPT="X" SDATE="20120319" STIME="222025" IDATE="20120319" ITIME="215102" UCCHECK="X">
|
<PROG NAME="ZDEMO_EXCEL34" VARCL="X" SUBC="1" CNAM="FEMIA" CDAT="20120319" UNAM="K2_SCHMOECK" UDAT="20120325" VERN="000017" RSTAT="T" RMAND="001" RLOAD="E" FIXPT="X" SDATE="20120325" STIME="233306" IDATE="20120325" ITIME="233306" UCCHECK="X">
|
||||||
<textPool>
|
<textPool>
|
||||||
<language SPRAS="E">
|
<language SPRAS="E">
|
||||||
<textElement ID="R" ENTRY="abap2xlsx Demo: static styles" LENGTH="33 "/>
|
<textElement ID="R" ENTRY="abap2xlsx Demo: Static Styles (Chess)" LENGTH="37 "/>
|
||||||
<textElement ID="S" KEY="P_PATH" ENTRY="D ." LENGTH="9 "/>
|
<textElement ID="S" KEY="P_PATH" ENTRY="D ." LENGTH="9 "/>
|
||||||
</language>
|
</language>
|
||||||
</textPool>
|
</textPool>
|
||||||
|
@ -16,147 +16,209 @@
|
||||||
|
|
||||||
REPORT zdemo_excel34.
|
REPORT zdemo_excel34.
|
||||||
|
|
||||||
|
CONSTANTS: width TYPE f VALUE '10.14'.
|
||||||
|
CONSTANTS: height TYPE f VALUE '57.75'.
|
||||||
|
|
||||||
|
DATA: current_row TYPE i,
|
||||||
|
col TYPE i,
|
||||||
|
col_alpha TYPE zexcel_cell_column_alpha,
|
||||||
|
row TYPE i,
|
||||||
|
row_board TYPE i,
|
||||||
|
colorflag TYPE i,
|
||||||
|
color TYPE zexcel_style_color_argb,
|
||||||
|
|
||||||
|
column_dimension TYPE REF TO zcl_excel_worksheet_columndime,
|
||||||
|
row_dimension TYPE REF TO zcl_excel_worksheet_rowdimensi,
|
||||||
|
|
||||||
|
writing1 TYPE string,
|
||||||
|
writing2 TYPE string.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
DATA: lo_excel TYPE REF TO zcl_excel,
|
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_worksheet TYPE REF TO zcl_excel_worksheet.
|
||||||
|
|
||||||
DATA: lv_file TYPE xstring,
|
CONSTANTS: gc_save_file_name TYPE string VALUE '34_Static Styles_Chess.xlsx'.
|
||||||
lv_bytecount TYPE i,
|
INCLUDE zdemo_excel_outputopt_incl.
|
||||||
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 '34_Static_Styles.xlsx'.
|
|
||||||
|
|
||||||
PARAMETERS: p_path TYPE zexcel_export_dir.
|
|
||||||
|
|
||||||
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_path.
|
|
||||||
lv_workdir = p_path.
|
|
||||||
cl_gui_frontend_services=>directory_browse( EXPORTING initial_folder = lv_workdir
|
|
||||||
CHANGING selected_folder = lv_workdir ).
|
|
||||||
p_path = lv_workdir.
|
|
||||||
|
|
||||||
INITIALIZATION.
|
|
||||||
cl_gui_frontend_services=>get_desktop_directory( CHANGING desktop_directory = lv_workdir ).
|
|
||||||
cl_gui_cfw=>flush( ).
|
|
||||||
p_path = lv_workdir.
|
|
||||||
|
|
||||||
sy-title = 'ZDEMO_EXCEL2;Issue 139: Static styles instead of dynamic styles'.
|
|
||||||
|
|
||||||
START-OF-SELECTION.
|
START-OF-SELECTION.
|
||||||
|
|
||||||
IF p_path IS INITIAL.
|
|
||||||
p_path = lv_workdir.
|
|
||||||
ENDIF.
|
|
||||||
cl_gui_frontend_services=>get_file_separator( CHANGING file_separator = lv_file_separator ).
|
|
||||||
CONCATENATE p_path lv_file_separator lv_default_file_name INTO lv_full_path.
|
|
||||||
|
|
||||||
" Creates active sheet
|
" Creates active sheet
|
||||||
CREATE OBJECT lo_excel.
|
CREATE OBJECT lo_excel.
|
||||||
|
|
||||||
|
|
||||||
" Get active sheet
|
" Get active sheet
|
||||||
lo_worksheet = lo_excel->get_active_worksheet( ).
|
lo_worksheet = lo_excel->get_active_worksheet( ).
|
||||||
lo_worksheet->set_title( ip_title = 'Styles' ).
|
lo_worksheet->set_title( ip_title = 'Spassky_vs_Bronstein' ).
|
||||||
lo_worksheet->set_cell( ip_column = 'B' ip_row = 2 ip_value = 'Hello world' ).
|
|
||||||
lo_worksheet->set_cell( ip_column = 'C' ip_row = 3 ip_value = 'Bold text' ).
|
* Header
|
||||||
lo_worksheet->change_cell_style( ip_column = 'C'
|
current_row = 1.
|
||||||
ip_row = 3
|
|
||||||
ip_font_bold = abap_true
|
ADD 1 TO current_row.
|
||||||
ip_font_italic = abap_true
|
lo_worksheet->set_cell( ip_row = current_row ip_column = 'B' ip_value = 'White' ).
|
||||||
ip_font_name = zcl_excel_style_font=>c_name_arial
|
lo_worksheet->set_cell( ip_row = current_row ip_column = 'C' ip_value = 'Spassky, Boris V -- wins in turn 23' ).
|
||||||
ip_font_scheme = zcl_excel_style_font=>c_scheme_none
|
|
||||||
ip_font_color_rgb = zcl_excel_style_color=>c_red
|
ADD 1 TO current_row.
|
||||||
).
|
lo_worksheet->set_cell( ip_row = current_row ip_column = 'B' ip_value = 'Black' ).
|
||||||
lo_worksheet->set_cell( ip_column = 'D' ip_row = 4 ip_value = 'Underlined text' ).
|
lo_worksheet->set_cell( ip_row = current_row ip_column = 'C' ip_value = 'Bronstein, David I' ).
|
||||||
lo_worksheet->change_cell_style( ip_column = 'D'
|
|
||||||
ip_row = 4
|
ADD 1 TO current_row.
|
||||||
ip_font_underline = abap_true
|
* Set size of column + Writing above chessboard
|
||||||
ip_font_underline_mode = zcl_excel_style_font=>c_underline_double
|
DO 8 TIMES.
|
||||||
ip_font_name = zcl_excel_style_font=>c_name_roman
|
|
||||||
ip_font_scheme = zcl_excel_style_font=>c_scheme_none
|
writing1 = zcl_excel_common=>convert_column2alpha( sy-index ).
|
||||||
ip_font_family = zcl_excel_style_font=>c_family_roman
|
writing2 = sy-index .
|
||||||
).
|
row = current_row + sy-index.
|
||||||
lo_worksheet->set_cell( ip_column = 'B' ip_row = 5 ip_value = 'Filled text' ).
|
|
||||||
lo_worksheet->change_cell_style( ip_column = 'B'
|
col = sy-index + 1.
|
||||||
ip_row = 5
|
col_alpha = zcl_excel_common=>convert_column2alpha( col ).
|
||||||
ip_fill_filltype = zcl_excel_style_fill=>c_fill_solid
|
|
||||||
ip_fill_fgcolor_theme = zcl_excel_style_color=>c_theme_accent6
|
* Set size of column
|
||||||
).
|
column_dimension = lo_worksheet->get_column_dimension( col_alpha ).
|
||||||
lo_worksheet->set_cell( ip_column = 'C' ip_row = 6 ip_value = 'Borders' ).
|
column_dimension->set_width( width ).
|
||||||
lo_worksheet->change_cell_style( ip_column = 'C'
|
|
||||||
ip_row = 6
|
* Set size of row
|
||||||
ip_borders_allbo_color_rgb = zcl_excel_style_color=>c_black
|
row_dimension = lo_worksheet->get_row_dimension( row ).
|
||||||
ip_borders_allborders_style = zcl_excel_style_border=>c_border_thin
|
row_dimension->set_row_height( height ).
|
||||||
ip_borders_diagonal_color_rgb = zcl_excel_style_color=>c_black
|
|
||||||
ip_borders_diagonal_style = zcl_excel_style_border=>c_border_thin
|
* Set writing on chessboard
|
||||||
ip_borders_diagonal_mode = zcl_excel_style_borders=>c_diagonal_both
|
lo_worksheet->set_cell( ip_row = row
|
||||||
).
|
ip_column = 'A'
|
||||||
lo_worksheet->set_cell( ip_column = 'D' ip_row = 7 ip_value = 'I''m not a button :)' ).
|
ip_value = writing2 ).
|
||||||
lo_worksheet->change_cell_style( ip_column = 'D'
|
lo_worksheet->change_cell_style( ip_column = 'A'
|
||||||
ip_row = 7
|
ip_row = row
|
||||||
ip_borders_down_color_rgb = zcl_excel_style_color=>c_black
|
ip_alignment_vertical = zcl_excel_style_alignment=>c_vertical_center ).
|
||||||
ip_borders_down_style = zcl_excel_style_border=>c_border_thin
|
lo_worksheet->set_cell( ip_row = row
|
||||||
ip_borders_right_color_rgb = zcl_excel_style_color=>c_black
|
ip_column = 'J'
|
||||||
ip_borders_right_style = zcl_excel_style_border=>c_border_thin
|
ip_value = writing2 ).
|
||||||
ip_borders_top_color_rgb = zcl_excel_style_color=>c_gray
|
lo_worksheet->change_cell_style( ip_column = 'J'
|
||||||
ip_borders_top_style = zcl_excel_style_border=>c_border_thin
|
ip_row = row
|
||||||
ip_borders_left_color_rgb = zcl_excel_style_color=>c_gray
|
ip_alignment_vertical = zcl_excel_style_alignment=>c_vertical_center ).
|
||||||
ip_borders_left_style = zcl_excel_style_border=>c_border_thin
|
|
||||||
).
|
row = current_row + 9.
|
||||||
lo_worksheet->set_cell( ip_column = 'B' ip_row = 9 ip_value = 'Modified color for Excel 2003' ).
|
lo_worksheet->set_cell( ip_row = current_row
|
||||||
lo_excel->legacy_palette->set_color( "replace built-in color from palette with out custom RGB turquoise
|
ip_column = col_alpha
|
||||||
ip_index = 16
|
ip_value = writing1 ).
|
||||||
ip_color = '0040E0D0' ).
|
lo_worksheet->change_cell_style( ip_column = col_alpha
|
||||||
lo_worksheet->change_cell_style( ip_column = 'B'
|
ip_row = current_row
|
||||||
ip_row = 9
|
ip_alignment_horizontal = zcl_excel_style_alignment=>c_horizontal_center ).
|
||||||
ip_fill_filltype = zcl_excel_style_fill=>c_fill_solid
|
lo_worksheet->set_cell( ip_row = row
|
||||||
ip_fill_fgcolor_indexed = 16
|
ip_column = col_alpha
|
||||||
).
|
ip_value = writing1 ).
|
||||||
" Fill the cell and apply one style
|
lo_worksheet->change_cell_style( ip_column = col_alpha
|
||||||
lo_worksheet->set_cell( ip_column = 'B' ip_row = 6 ip_value = 'Filled text' ).
|
ip_row = row
|
||||||
lo_worksheet->change_cell_style( ip_column = 'B'
|
ip_alignment_horizontal = zcl_excel_style_alignment=>c_horizontal_center ).
|
||||||
ip_row = 6
|
ENDDO.
|
||||||
ip_fill_filltype = zcl_excel_style_fill=>c_fill_solid
|
column_dimension = lo_worksheet->get_column_dimension( 'A' ).
|
||||||
ip_fill_fgcolor_rgb = zcl_excel_style_color=>c_green
|
column_dimension->set_auto_size( abap_true ).
|
||||||
ip_font_name = zcl_excel_style_font=>c_name_cambria
|
column_dimension = lo_worksheet->get_column_dimension( 'J' ).
|
||||||
ip_font_scheme = zcl_excel_style_font=>c_scheme_major
|
column_dimension->set_auto_size( abap_true ).
|
||||||
).
|
|
||||||
" Change the style
|
* Set win-position
|
||||||
* lo_worksheet->set_cell_style( ip_column = 'B' ip_row = 6 ).
|
CONSTANTS: c_pawn TYPE string VALUE 'Pawn'.
|
||||||
" Add Style to an empty cell to test Fix for Issue
|
CONSTANTS: c_rook TYPE string VALUE 'Rook'.
|
||||||
"#44 Exception ZCX_EXCEL thrown when style is set for an empty cell
|
CONSTANTS: c_knight TYPE string VALUE 'Knight'.
|
||||||
" https://code.sdn.sap.com/spaces/abap2xlsx/tickets/44-exception-zcx_excel-thrown-when-style-is-set-for-an-empty-cell
|
CONSTANTS: c_bishop TYPE string VALUE 'Bishop'.
|
||||||
* lo_worksheet->set_cell_style( ip_column = 'E' ip_row = 6 ).
|
CONSTANTS: c_queen TYPE string VALUE 'Queen'.
|
||||||
lo_worksheet->change_cell_style( ip_column = 'E'
|
CONSTANTS: c_king TYPE string VALUE 'King'.
|
||||||
ip_row = 6
|
|
||||||
ip_fill_filltype = zcl_excel_style_fill=>c_fill_solid
|
row = current_row + 1.
|
||||||
ip_fill_fgcolor_rgb = zcl_excel_style_color=>c_green
|
lo_worksheet->set_cell( ip_row = row ip_column = 'B' ip_value = c_rook ).
|
||||||
ip_font_name = zcl_excel_style_font=>c_name_cambria
|
lo_worksheet->set_cell( ip_row = row ip_column = 'F' ip_value = c_rook ).
|
||||||
ip_font_scheme = zcl_excel_style_font=>c_scheme_major
|
lo_worksheet->set_cell( ip_row = row ip_column = 'G' ip_value = c_knight ).
|
||||||
).
|
row = current_row + 2.
|
||||||
|
lo_worksheet->set_cell( ip_row = row ip_column = 'B' ip_value = c_pawn ).
|
||||||
|
lo_worksheet->set_cell( ip_row = row ip_column = 'C' ip_value = c_pawn ).
|
||||||
|
lo_worksheet->set_cell( ip_row = row ip_column = 'D' ip_value = c_pawn ).
|
||||||
|
lo_worksheet->set_cell( ip_row = row ip_column = 'F' ip_value = c_queen ).
|
||||||
|
lo_worksheet->set_cell( ip_row = row ip_column = 'H' ip_value = c_pawn ).
|
||||||
|
lo_worksheet->set_cell( ip_row = row ip_column = 'I' ip_value = c_king ).
|
||||||
|
row = current_row + 3.
|
||||||
|
lo_worksheet->set_cell( ip_row = row ip_column = 'I' ip_value = c_pawn ).
|
||||||
|
row = current_row + 4.
|
||||||
|
lo_worksheet->set_cell( ip_row = row ip_column = 'D' ip_value = c_pawn ).
|
||||||
|
lo_worksheet->set_cell( ip_row = row ip_column = 'F' ip_value = c_knight ).
|
||||||
|
row = current_row + 5.
|
||||||
|
lo_worksheet->set_cell( ip_row = row ip_column = 'E' ip_value = c_pawn ).
|
||||||
|
lo_worksheet->set_cell( ip_row = row ip_column = 'F' ip_value = c_queen ).
|
||||||
|
row = current_row + 6.
|
||||||
|
lo_worksheet->set_cell( ip_row = row ip_column = 'C' ip_value = c_bishop ).
|
||||||
|
row = current_row + 7.
|
||||||
|
lo_worksheet->set_cell( ip_row = row ip_column = 'B' ip_value = c_pawn ).
|
||||||
|
lo_worksheet->set_cell( ip_row = row ip_column = 'C' ip_value = c_pawn ).
|
||||||
|
lo_worksheet->set_cell( ip_row = row ip_column = 'H' ip_value = c_pawn ).
|
||||||
|
lo_worksheet->set_cell( ip_row = row ip_column = 'I' ip_value = c_pawn ).
|
||||||
|
row = current_row + 8.
|
||||||
|
lo_worksheet->set_cell( ip_row = row ip_column = 'G' ip_value = c_rook ).
|
||||||
|
lo_worksheet->set_cell( ip_row = row ip_column = 'H' ip_value = c_king ).
|
||||||
|
|
||||||
|
* Set Chessboard
|
||||||
|
DO 8 TIMES.
|
||||||
|
IF sy-index <= 3. " Black
|
||||||
|
color = zcl_excel_style_color=>c_black.
|
||||||
|
ELSE.
|
||||||
|
color = zcl_excel_style_color=>c_white.
|
||||||
|
ENDIF.
|
||||||
|
row_board = sy-index.
|
||||||
|
row = current_row + sy-index.
|
||||||
|
DO 8 TIMES.
|
||||||
|
col = sy-index + 1.
|
||||||
|
col_alpha = zcl_excel_common=>convert_column2alpha( col ).
|
||||||
|
TRY.
|
||||||
|
* Borders around outer limits
|
||||||
|
IF row_board = 1.
|
||||||
|
lo_worksheet->change_cell_style( ip_column = col_alpha
|
||||||
|
ip_row = row
|
||||||
|
ip_borders_top_style = zcl_excel_style_border=>c_border_thick
|
||||||
|
ip_borders_top_color_rgb = zcl_excel_style_color=>c_black ).
|
||||||
|
ENDIF.
|
||||||
|
IF row_board = 8.
|
||||||
|
lo_worksheet->change_cell_style( ip_column = col_alpha
|
||||||
|
ip_row = row
|
||||||
|
ip_borders_down_style = zcl_excel_style_border=>c_border_thick
|
||||||
|
ip_borders_down_color_rgb = zcl_excel_style_color=>c_black ).
|
||||||
|
ENDIF.
|
||||||
|
IF col = 2.
|
||||||
|
lo_worksheet->change_cell_style( ip_column = col_alpha
|
||||||
|
ip_row = row
|
||||||
|
ip_borders_left_style = zcl_excel_style_border=>c_border_thick
|
||||||
|
ip_borders_left_color_rgb = zcl_excel_style_color=>c_black ).
|
||||||
|
ENDIF.
|
||||||
|
IF col = 9.
|
||||||
|
lo_worksheet->change_cell_style( ip_column = col_alpha
|
||||||
|
ip_row = row
|
||||||
|
ip_borders_right_style = zcl_excel_style_border=>c_border_thick
|
||||||
|
ip_borders_right_color_rgb = zcl_excel_style_color=>c_black ).
|
||||||
|
ENDIF.
|
||||||
|
* Style for writing
|
||||||
|
lo_worksheet->change_cell_style( ip_column = col_alpha
|
||||||
|
ip_row = row
|
||||||
|
ip_font_color_rgb = color
|
||||||
|
ip_font_bold = 'X'
|
||||||
|
ip_font_size = 16
|
||||||
|
ip_alignment_horizontal = zcl_excel_style_alignment=>c_horizontal_center
|
||||||
|
ip_alignment_vertical = zcl_excel_style_alignment=>c_vertical_center
|
||||||
|
ip_fill_filltype = zcl_excel_style_fill=>c_fill_solid ).
|
||||||
|
* Color of field
|
||||||
|
colorflag = ( row + col ) MOD 2.
|
||||||
|
IF colorflag = 0.
|
||||||
|
lo_worksheet->change_cell_style( ip_column = col_alpha
|
||||||
|
ip_row = row
|
||||||
|
ip_fill_fgcolor_rgb = 'FFB5866A' ).
|
||||||
|
ELSE.
|
||||||
|
lo_worksheet->change_cell_style( ip_column = col_alpha
|
||||||
|
ip_row = row
|
||||||
|
ip_fill_fgcolor_rgb = 'FFF5DEBF' ).
|
||||||
|
ENDIF.
|
||||||
|
|
||||||
|
|
||||||
CREATE OBJECT lo_excel_writer TYPE zcl_excel_writer_2007.
|
|
||||||
lv_file = lo_excel_writer->write_file( lo_excel ).
|
|
||||||
|
|
||||||
" Convert to binary
|
CATCH zcx_excel .
|
||||||
CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
|
ENDTRY.
|
||||||
EXPORTING
|
|
||||||
buffer = lv_file
|
|
||||||
IMPORTING
|
|
||||||
output_length = lv_bytecount
|
|
||||||
TABLES
|
|
||||||
binary_tab = lt_file_tab.
|
|
||||||
* " This method is only available on AS ABAP > 6.40
|
|
||||||
* lt_file_tab = cl_bcs_convert=>xstring_to_solix( iv_xstring = lv_file ).
|
|
||||||
* lv_bytecount = xstrlen( lv_file ).
|
|
||||||
|
|
||||||
" Save the file
|
ENDDO.
|
||||||
cl_gui_frontend_services=>gui_download( EXPORTING bin_filesize = lv_bytecount
|
ENDDO.
|
||||||
filename = lv_full_path
|
|
||||||
filetype = 'BIN'
|
|
||||||
CHANGING data_tab = lt_file_tab ).</source>
|
*** Create output
|
||||||
|
lcl_output=>output( lo_excel ).</source>
|
||||||
</PROG>
|
</PROG>
|
||||||
|
|
Loading…
Reference in New Issue
Block a user