class ZCL_EXCEL_CONVERTER definition public create public . *"* public components of class ZCL_EXCEL_CONVERTER *"* do not include other source files here!!! public section. type-pools ABAP . class-methods CLASS_CONSTRUCTOR . methods CONVERT importing !IO_ALV type ref to OBJECT optional !IT_TABLE type STANDARD TABLE !I_ROW_INT type I default 1 !I_COLUMN_INT type I default 1 !I_TABLE type FLAG optional !I_STYLE_TABLE type ZEXCEL_TABLE_STYLE optional !IO_WORKSHEET type ref to ZCL_EXCEL_WORKSHEET optional changing !CO_EXCEL type ref to ZCL_EXCEL optional raising ZCX_EXCEL . methods GET_FILE exporting !E_BYTECOUNT type I !ET_FILE type SOLIX_TAB !E_FILE type XSTRING . methods OPEN_FILE . methods WRITE_FILE importing !I_PATH type STRING optional . *"* protected components of class ZCL_EXCEL_CONVERTER *"* do not include other source files here!!! protected section. types: BEGIN OF t_relationship, id TYPE string, type TYPE string, target TYPE string, END OF t_relationship . types: BEGIN OF t_fileversion, appname TYPE string, lastedited TYPE string, lowestedited TYPE string, rupbuild TYPE string, codename TYPE string, END OF t_fileversion . types: BEGIN OF t_sheet, name TYPE string, sheetid TYPE string, id TYPE string, END OF t_sheet . types: BEGIN OF t_workbookpr, codename TYPE string, defaultthemeversion TYPE string, END OF t_workbookpr . types: BEGIN OF t_sheetpr, codename TYPE string, END OF t_sheetpr . data W_ROW_INT type ZEXCEL_CELL_ROW value 1. "#EC NOTEXT . data W_COL_INT type ZEXCEL_CELL_COLUMN value 1. "#EC NOTEXT . *"* private components of class ZCL_EXCEL_CONVERTER *"* do not include other source files here!!! private section. data wo_excel type ref to zcl_excel . data wo_worksheet type ref to zcl_excel_worksheet . data wo_data type ref to data . data wt_fieldcatalog type zexcel_t_converter_fcat . data ws_layout type zexcel_s_converter_layo . class-data wt_objects type tt_alv_types . data w_fcount type numc3 . methods bind_table importing !i_style_table type zexcel_table_style . methods bind_cells raising zcx_excel . methods create_formular_total importing !i_row_int type zexcel_cell_row !i_column type zexcel_cell_column_alpha !i_totals_function type zexcel_table_totals_function returning value(r_formula) type string . methods create_style_hdr importing !i_alignment type zexcel_alignment optional returning value(r_style) type zexcel_cell_style . methods create_style_normal importing !i_alignment type zexcel_alignment optional !i_inttype type inttype optional returning value(r_style) type zexcel_cell_style . methods create_style_stripped importing !i_alignment type zexcel_alignment optional !i_inttype type inttype optional returning value(r_style) type zexcel_cell_style . methods create_style_total importing !i_alignment type zexcel_alignment optional !i_inttype type inttype optional returning value(r_style) type zexcel_cell_style . methods create_path returning value(r_path) type string . methods create_table importing !it_table type standard table . methods create_worksheet importing !i_table type flag default 'X' !i_style_table type zexcel_table_style . methods execute_converter importing !io_object type ref to object !it_table type standard table raising zcx_excel . methods set_fieldcatalog . *"* local class implementation for public class *"* use this source file for the implementation part of *"* local helper classes *"* use this source file for any type declarations (class *"* definitions, interfaces or data types) you need for method *"* implementation or private method's signature types: begin of ts_alv_types, seoclass type SEOCLSNAME, clsname type seoclsname, end of ts_alv_types, tt_alv_types type hashed table of ts_alv_types with unique key seoclass. *"* use this source file for any macro definitions you need *"* in the implementation part of the class ABAP SLIS SOI method BIND_CELLS. data: lo_tabdescr type ref to cl_abap_structdescr, lo_data type ref to data, l_row_header type zexcel_cell_row value '2', l_col_header type zexcel_cell_column_alpha value 'B', l_row_int type zexcel_cell_row, l_col_int type zexcel_cell_column, l_col_alpha type zexcel_cell_column_alpha, l_cell_value type zexcel_cell_value, l_hdr type zexcel_cell_style, l_stripped type zexcel_cell_style, l_normal type zexcel_cell_style, l_total type zexcel_cell_style, l_s_color type abap_bool, i_num_columns type i, lo_col_dim type ref to zcl_excel_worksheet_columndime, l_formula type zexcel_cell_formula. field-symbols: <fs_stab> type any, <fs_tab> type any table, <fs_sfcat> type zexcel_s_converter_fcat, <fs_fldval> type any, <fs_cell_value> type zexcel_cell_value. assign wo_data->* to <fs_tab> . l_col_int = w_col_int. l_row_int = w_row_int. * First lets check if we fiedl catalog ready or we need conversion from table. if wt_fieldcatalog is initial. set_fieldcatalog( ). endif. clear ws_layout. * It is better to loop column by column loop at wt_fieldcatalog assigning <fs_sfcat>. * Default stype with alignment and format l_hdr = create_style_hdr( i_alignment = <fs_sfcat>-alignment ). if ws_layout-is_stripped = abap_true. l_stripped = create_style_stripped( i_alignment = <fs_sfcat>-alignment i_inttype = <fs_sfcat>-inttype ). endif. l_normal = create_style_normal( i_alignment = <fs_sfcat>-alignment i_inttype = <fs_sfcat>-inttype ). l_total = create_style_total( i_alignment = <fs_sfcat>-alignment i_inttype = <fs_sfcat>-inttype ). * Freeze panes if <fs_sfcat>-fix_column = abap_true. add 1 to i_num_columns. endif. l_s_color = abap_true. l_col_alpha = zcl_excel_common=>convert_column2alpha( l_col_int ). " First of all write column header l_cell_value = <fs_sfcat>-scrtext_m. wo_worksheet->set_cell( ip_column = l_col_alpha ip_row = l_row_int ip_value = l_cell_value ip_abap_type = cl_abap_typedescr=>typekind_string ip_style = l_hdr ). add 1 to l_row_int. loop at <fs_tab> assigning <fs_stab>. * Now the cell values assign component <fs_sfcat>-columnname of structure <fs_stab> to <fs_fldval>. if ws_layout-is_stripped = abap_true and l_s_color = abap_true. wo_worksheet->set_cell( ip_column = l_col_alpha ip_row = l_row_int ip_value = <fs_fldval> ip_abap_type = <fs_sfcat>-inttype ip_style = l_stripped ). clear l_s_color. else. wo_worksheet->set_cell( ip_column = l_col_alpha ip_row = l_row_int ip_value = <fs_fldval> ip_abap_type = <fs_sfcat>-inttype ip_style = l_normal ). l_s_color = abap_true. endif. add 1 to l_row_int. endloop. * Now let's check for optimized if <fs_sfcat>-is_optimized = abap_true. lo_col_dim = wo_worksheet->get_column_dimension( ip_column = l_col_alpha ). lo_col_dim->set_auto_size( ip_auto_size = abap_true ) . endif. * Now let's check for visible if <fs_sfcat>-is_visible = abap_false. lo_col_dim = wo_worksheet->get_column_dimension( ip_column = l_col_alpha ). lo_col_dim->set_visible( ip_visible = abap_false ) . endif. * Now let's check for total if <fs_sfcat>-totals_function is not initial. l_row_int = l_row_int - 1. l_formula = create_formular_total( i_row_int = l_row_int i_column = l_col_alpha i_totals_function = <fs_sfcat>-totals_function ). add 1 to l_row_int. wo_worksheet->set_cell( ip_column = l_col_alpha ip_row = l_row_int ip_formula = l_formula ip_style = l_total ). endif. l_row_int = w_row_int. add 1 to l_col_int. endloop. * Check for freeze panes if ws_layout-is_fixed = abap_true. if i_num_columns = 0. i_num_columns = w_col_int. endif. wo_worksheet->freeze_panes( exporting ip_num_columns = i_num_columns ip_num_rows = w_row_int ) . endif. endmethod. method BIND_TABLE. data: lt_field_catalog type zexcel_t_fieldcatalog, ls_field_catalog type zexcel_s_fieldcatalog, ls_fcat type zexcel_s_converter_fcat, ls_settings type zexcel_s_table_settings, l_line type i. field-symbols: <fs_tab> type any table. assign wo_data->* to <fs_tab> . if wt_fieldcatalog is initial. set_fieldcatalog( ) . endif. ls_settings-table_style = i_style_table. ls_settings-top_left_column = zcl_excel_common=>convert_column2alpha( ip_column = w_col_int ). ls_settings-top_left_row = w_row_int. ls_settings-show_row_stripes = ws_layout-is_stripped. describe table wt_fieldcatalog lines l_line. l_line = l_line + 1 + w_col_int. ls_settings-bottom_right_column = zcl_excel_common=>convert_column2alpha( ip_column = l_line ). describe table <fs_tab> lines l_line. ls_settings-bottom_right_row = l_line + 1 + w_row_int. sort wt_fieldcatalog by position. loop at wt_fieldcatalog into ls_fcat. move-corresponding ls_fcat to ls_field_catalog. ls_field_catalog-dynpfld = abap_true. insert ls_field_catalog into table lt_field_catalog. endloop. wo_worksheet->bind_table( exporting ip_table = <fs_tab> it_field_catalog = lt_field_catalog is_table_settings = ls_settings importing es_table_settings = ls_settings ). endmethod. method CLASS_CONSTRUCTOR. data: ls_objects type ts_alv_types. * Object CL_GUI_ALV_GRID ls_objects-seoclass = 'CL_GUI_ALV_GRID'. ls_objects-clsname = 'ZCL_EXCEL_CONVERTER_ALV_GRID'. insert ls_objects into table wt_objects. * Object CL_SALV_TABLE ls_objects-seoclass = 'CL_SALV_TABLE'. ls_objects-clsname = 'ZCL_EXCEL_CONVERTER_SALV_TABLE'. insert ls_objects into table wt_objects. endmethod. method CONVERT. try. execute_converter( io_object = io_alv it_table = it_table ) . endtry. if io_worksheet is supplied and io_worksheet is bound. wo_worksheet = io_worksheet. endif. if co_excel is supplied. if co_excel is not bound. create object co_excel. co_excel->zif_excel_book_properties~creator = sy-uname. endif. wo_excel = co_excel. endif. * Move table to data object and clean it up if wt_fieldcatalog is not initial. create_table( it_table = it_table ). else. get reference of it_table into wo_data. endif. if wo_excel is not bound. create object wo_excel. wo_excel->zif_excel_book_properties~creator = sy-uname. endif. if wo_worksheet is not bound. " Get active sheet wo_worksheet = wo_excel->get_active_worksheet( ). wo_worksheet->set_title( ip_title = 'Sheet1'(001) ). endif. w_row_int = i_row_int. w_col_int = i_column_int. create_worksheet( i_table = i_table i_style_table = i_style_table ) . endmethod. method CREATE_FORMULAR_TOTAL. data: l_row_alpha type string, l_row_e_alpha type string. l_row_alpha = w_row_int. l_row_e_alpha = i_row_int. concatenate i_totals_function '(' i_column l_row_alpha ':' i_column l_row_e_alpha ')' into r_formula. endmethod. method CREATE_PATH. data: l_sep type c . clear r_path. " Save the file cl_gui_frontend_services=>get_sapgui_workdir( changing sapworkdir = r_path * exceptions * get_sapworkdir_failed = 1 * cntl_error = 2 * error_no_gui = 3 * not_supported_by_gui = 4 ). if sy-subrc <> 0. * MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO * WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4. endif. add 1 to w_fcount. *-obtain file separator character--------------------------------------- call method cl_gui_frontend_services=>get_file_separator changing file_separator = l_sep exceptions cntl_error = 1 error_no_gui = 2 not_supported_by_gui = 3 others = 4. if sy-subrc <> 0. l_sep = ''. endif. concatenate r_path l_sep 'Excel_' w_fcount '.xlsx' into r_path. endmethod. method CREATE_STYLE_HDR. data: lo_style type ref to zcl_excel_style. lo_style = wo_excel->add_new_style( ). lo_style->font->bold = abap_true. lo_style->font->color-rgb = zcl_excel_style_color=>c_white. lo_style->fill->filltype = zcl_excel_style_fill=>c_fill_solid. lo_style->fill->fgcolor-rgb = 'FF4F81BD'. if i_alignment is supplied and i_alignment is not initial. lo_style->alignment->horizontal = i_alignment. endif. r_style = lo_style->get_guid( ) . endmethod. method CREATE_STYLE_NORMAL. data: lo_style type ref to zcl_excel_style. lo_style = wo_excel->add_new_style( ). if i_alignment is supplied and i_alignment is not initial. lo_style->alignment->horizontal = i_alignment. endif. if i_inttype is supplied and i_inttype is not initial. case i_inttype. when cl_abap_typedescr=>typekind_date. lo_style->number_format->format_code = wo_worksheet->get_default_excel_date_format( ). when cl_abap_typedescr=>typekind_time. lo_style->number_format->format_code = zcl_excel_style_number_format=>c_format_date_time6. endcase. endif. r_style = lo_style->get_guid( ) . endmethod. method CREATE_STYLE_STRIPPED. data: lo_style type ref to zcl_excel_style. lo_style = wo_excel->add_new_style( ). lo_style->fill->filltype = zcl_excel_style_fill=>c_fill_solid. lo_style->fill->fgcolor-rgb = 'FFDBE5F1'. if i_alignment is supplied and i_alignment is not initial. lo_style->alignment->horizontal = i_alignment. endif. if i_inttype is supplied and i_inttype is not initial. case i_inttype. when cl_abap_typedescr=>typekind_date. lo_style->number_format->format_code = wo_worksheet->get_default_excel_date_format( ). when cl_abap_typedescr=>typekind_time. lo_style->number_format->format_code = zcl_excel_style_number_format=>c_format_date_time6. endcase. endif. r_style = lo_style->get_guid( ) . endmethod. method CREATE_STYLE_TOTAL. data: lo_style type ref to zcl_excel_style. lo_style = wo_excel->add_new_style( ). create object lo_style->borders->top. lo_style->borders->top->border_style = zcl_excel_style_border=>c_border_thin. lo_style->borders->top->border_color-rgb = zcl_excel_style_color=>c_black. create object lo_style->borders->right. lo_style->borders->right->border_style = zcl_excel_style_border=>c_border_none. lo_style->borders->right->border_color-rgb = zcl_excel_style_color=>c_black. create object lo_style->borders->down. lo_style->borders->down->border_style = zcl_excel_style_border=>c_border_double. lo_style->borders->down->border_color-rgb = zcl_excel_style_color=>c_black. create object lo_style->borders->left. lo_style->borders->left->border_style = zcl_excel_style_border=>c_border_none. lo_style->borders->left->border_color-rgb = zcl_excel_style_color=>c_black. if i_alignment is supplied and i_alignment is not initial. lo_style->alignment->horizontal = i_alignment. endif. if i_inttype is supplied and i_inttype is not initial. case i_inttype. when cl_abap_typedescr=>typekind_date. lo_style->number_format->format_code = wo_worksheet->get_default_excel_date_format( ). when cl_abap_typedescr=>typekind_time. lo_style->number_format->format_code = zcl_excel_style_number_format=>c_format_date_time6. endcase. endif. r_style = lo_style->get_guid( ) . endmethod. method CREATE_TABLE. data: lo_data type ref to data. data: lo_addit type ref to cl_abap_elemdescr, lt_components_tab type cl_abap_structdescr=>component_table, ls_components type abap_componentdescr, lo_table type ref to cl_abap_tabledescr, lo_struc type ref to cl_abap_structdescr. field-symbols: <fs_scat> type zexcel_s_converter_fcat, <fs_stab> type any, <fs_ttab> type standard table, <fs> type any. sort wt_fieldcatalog by position. read table it_table assigning <fs_stab> index 1. if sy-subrc eq 0 . loop at wt_fieldcatalog assigning <fs_scat>. assign component <fs_scat>-columnname of structure <fs_stab> to <fs>. if sy-subrc = 0. ls_components-name = <fs_scat>-columnname. lo_addit ?= cl_abap_typedescr=>describe_by_data( <fs> ). ls_components-type = lo_addit . insert ls_components into table lt_components_tab. endif. endloop. "create new line type lo_struc = cl_abap_structdescr=>create( lt_components_tab ). lo_table = cl_abap_tabledescr=>create( lo_struc ). create data wo_data type handle lo_table. create data lo_data type handle lo_struc. assign wo_data->* to <fs_ttab>. assign lo_data->* to <fs_stab>. loop at it_table assigning <fs>. clear <fs_stab>. move-corresponding <fs> to <fs_stab>. append <fs_stab> to <fs_ttab>. endloop. endif. endmethod. method CREATE_WORKSHEET. if wo_data is bound and wo_worksheet is bound. if i_table = abap_true. bind_table( i_style_table = i_style_table ) . else. bind_cells( ) . endif. endif. wo_worksheet->zif_excel_sheet_properties~zoomscale = 100. wo_worksheet->zif_excel_sheet_properties~zoomscale_normal = 100. endmethod. method EXECUTE_CONVERTER. data: lt_fieldcatalog type zexcel_t_fieldcatalog, ls_fieldcatalog type zexcel_s_converter_fcat, lo_if type ref to zif_excel_converter, ls_types type ts_alv_types, lo_addit type ref to cl_abap_classdescr. if io_object is bound. try. lo_addit ?= cl_abap_typedescr=>describe_by_object_ref( io_object ). catch cx_sy_move_cast_error. raise exception type zcx_excel. endtry. ls_types-seoclass = lo_addit->get_relative_name( ). read table wt_objects into ls_types with table key seoclass = ls_types-seoclass. if sy-subrc = 0. create object lo_if type (ls_types-clsname). try. lo_if->create_fieldcatalog( exporting io_object = io_object it_table = it_table importing es_layout = ws_layout et_fieldcatalog = wt_fieldcatalog ). endtry. else. raise exception type zcx_excel. endif. else. refresh wt_fieldcatalog. endif. endmethod. method GET_FILE. data: lo_excel_writer type ref to zif_excel_writer, lo_excel type ref to zcl_excel. data: ls_seoclass type seoclass. if wo_excel is bound. create object lo_excel_writer type zcl_excel_writer_2007. e_file = lo_excel_writer->write_file( wo_excel ). select single * into ls_seoclass from seoclass where clsname = 'CL_BCS_CONVERT'. if sy-subrc = 0. call method (ls_seoclass-clsname)=>xstring_to_solix exporting iv_xstring = e_file receiving et_solix = et_file. e_bytecount = xstrlen( e_file ). else. " Convert to binary call function 'SCMS_XSTRING_TO_BINARY' exporting buffer = e_file importing output_length = e_bytecount tables binary_tab = et_file. endif. endif. endmethod. method OPEN_FILE. data: l_bytecount type i, lt_file type solix_tab, l_dir type string, l_sep type c. field-symbols: <fs_data> type any table. assign wo_data->* to <fs_data>. * catch zcx_excel . *endtry. if wo_excel is bound. get_file( importing e_bytecount = l_bytecount et_file = lt_file ) . l_dir = create_path( ) . cl_gui_frontend_services=>gui_download( exporting bin_filesize = l_bytecount filename = l_dir filetype = 'BIN' changing data_tab = lt_file ). cl_gui_frontend_services=>execute( exporting document = l_dir * application = * parameter = * default_directory = * maximized = * minimized = * synchronous = * operation = 'OPEN' exceptions cntl_error = 1 error_no_gui = 2 bad_parameter = 3 file_not_found = 4 path_not_found = 5 file_extension_unknown = 6 error_execute_failed = 7 synchronous_failed = 8 not_supported_by_gui = 9 ). if sy-subrc <> 0. message id sy-msgid type sy-msgty number sy-msgno with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4. endif. endif. endmethod. method SET_FIELDCATALOG. data: lt_fieldcat type zexcel_t_fieldcatalog, ls_fieldcat type zexcel_s_fieldcatalog, ls_fcat type zexcel_s_converter_fcat. field-symbols: <fs_tab> type any table. assign wo_data->* to <fs_tab> . lt_fieldcat = zcl_excel_common=>get_fieldcatalog( ip_table = <fs_tab> ). loop at lt_fieldcat into ls_fieldcat. move-corresponding ls_fieldcat to ls_fcat. insert ls_fcat into table wt_fieldcatalog. endloop. endmethod. method WRITE_FILE. data: l_bytecount type i, lt_file type solix_tab, l_dir type string. field-symbols: <fs_data> type any table. assign wo_data->* to <fs_data>. * catch zcx_excel . *endtry. if wo_excel is bound. get_file( importing e_bytecount = l_bytecount et_file = lt_file ) . if i_path is initial. l_dir = create_path( ) . else. l_dir = i_path. endif. cl_gui_frontend_services=>gui_download( exporting bin_filesize = l_bytecount filename = l_dir filetype = 'BIN' changing data_tab = lt_file ). endif. endmethod.