Code: Alles auswählen.
METHOD TAB_2_XLSX.
*Importing IMP_DATA TYPE REF TO DATA
*Exporting EXP_XSTRING TYPE XSTRING XSTRING
*Exporting EXP_STRING TYPE STRING STRING
*Exporting RETC TYPE SYSUBRC Return Value of ABAP Statements
** IT_FIELDCATALOG Static Attribute Private Type LVC_T_FCAT
** WS_FCAT Static Attribute Private Type LVC_S_FCAT
*------------------------------------------------------------------------------*
* Änderungen created 14.09.2011 11:40:20 -------------------------------*
*------------------------------------------------------------------------------*
* K User Datum Grund *
*------------------------------------------------------------------------------*
*xxxxx wxxxxx yyyymmdd .......................................... *
*------------------------------------------------------------------------------*
FIELD-SYMBOLS <fs_table> TYPE STANDARD TABLE.
DATA va_ch TYPE c LENGTH 1.
DATA va_strlen TYPE i.
DATA va_offs TYPE i.
FIELD-SYMBOLS <fs_rec> TYPE ANY.
FIELD-SYMBOLS <x> TYPE x.
DATA ref_result_data TYPE REF TO cl_salv_ex_result_data_table.
DATA va_version TYPE string.
DATA wa_choice TYPE if_salv_bs_xml=>s_type_xml_choice.
DATA va_tabkind TYPE c length 1.
CLEAR: retc.
* continue if import pointer is bound.
IF imp_data IS NOT BOUND.
retc = 1.
RETURN.
ENDIF.
* access the data.
TRY.
* check data only int tables allowed
ASSIGN imp_data->* TO <fs_rec>.
DESCRIBE FIELD <fs_rec> TYPE va_tabkind.
IF va_tabkind NE 'h' AND va_tabkind NE 'H'.
retc = 97.
RETURN.
ENDIF.
ASSIGN imp_data->* TO <fs_table>.
IF sy-subrc <> 0.
retc = 2.
RETURN.
ENDIF.
CATCH cx_root.
retc = 98.
RETURN.
ENDTRY.
REFRESH it_fieldcatalog.
col_pos = 0.
* build fieldcatalog from data.
get_elements( EXPORTING element = <fs_table>
name = ' ' ).
IF it_fieldcatalog IS INITIAL. retc = 3. RETURN. ENDIF.
IF <fs_table> IS INITIAL. retc = 4. RETURN. ENDIF.
va_version = if_salv_bs_xml=>version_26.
wa_choice-version = if_salv_bs_xml=>version_26.
wa_choice-xml_type = if_salv_bs_xml=>C_TYPE_XLSX.
wa_choice-gui_type = if_salv_bs_xml=>c_gui_type_gui.
ref_result_data = cl_salv_ex_util=>factory_result_data_table(
* T_SELECTED_ROWS = lt_lvc_row
* T_SELECTED_COLUMNS = lt_sel_cols
* T_SELECTED_CELLS = lt_sel_cells
r_data = imp_data
* S_LAYOUT = gs_layo
t_fieldcatalog = it_fieldcatalog
* T_SORT = gt_sort
* T_FILTER = gt_filter
* T_HYPERLINKS = me->mt_hyperlinks
* S_CURRENT_CELL = ls_cur_cell
* HYPERLINK_ENTRY_COLUMN = ls_hyper_entry
* DROPDOWN_ENTRY_COLUMN = ls_dropdown_entry
* T_DROPDOWN_VALUES = lt_drdn )
* r_top_of_list = lr_form_tol
* r_end_of_list = lr_form_eol
).
cl_salv_bs_tt_util=>if_salv_bs_tt_util~transform(
EXPORTING xml_type = wa_choice-xml_type
xml_version = va_version
r_result_data = ref_result_data
xml_flavour = if_salv_bs_c_tt=>c_tt_xml_flavour_export
gui_type = wa_choice-gui_type
IMPORTING xml = exp_xstring ).
* build "readable" string
IF exp_string IS SUPPLIED.
CLEAR exp_string.
ASSIGN va_ch TO <x> CASTING TYPE x.
va_strlen = XSTRLEN( exp_xstring ).
va_offs = 0.
DO va_strlen TIMES.
<x> = exp_xstring+va_offs(1).
va_offs = va_offs + 1.
CONCATENATE exp_string va_ch INTO exp_string IN CHARACTER MODE.
ENDDO.
ENDIF.
ENDMETHOD.
Code: Alles auswählen.
METHOD get_elements.
* Importing ELEMENT TYPE ANY
* Importing NAME TYPE FIELDNAME DEFAULT ' ' Field Name
*------------------------------------------------------------------------------*
* Änderungen created 22.09.2011 11:07:25 -------------------------------*
*------------------------------------------------------------------------------*
* K User Datum Grund *
*------------------------------------------------------------------------------*
*xxxxx wxxxxx yyyymmdd .......................................... *
*------------------------------------------------------------------------------*
DATA ref_line TYPE REF TO data.
DATA ref_struc TYPE REF TO cl_abap_structdescr.
DATA ref_elem TYPE REF TO cl_abap_elemdescr.
* DATA it_comps TYPE abap_compdescr_tab.
DATA wa_dfies TYPE dfies.
DATA va_type_kind TYPE abap_typekind.
DATA va_name TYPE string.
FIELD-SYMBOLS <fs_comp> TYPE abap_compdescr.
FIELD-SYMBOLS <fs_any> TYPE ANY.
FIELD-SYMBOLS <fs_table> TYPE STANDARD TABLE.
FIELD-SYMBOLS <fs_line> TYPE ANY.
va_type_kind = cl_abap_datadescr=>get_data_type_kind( p_data = element ).
* if internal table get line (create or read) -> call method get_elements with "data line"
IF va_type_kind = 'h'.
ASSIGN element TO <fs_table>.
READ TABLE <fs_table> ASSIGNING <fs_line> INDEX 1.
if sy-subrc ne 0.
TRY.
CREATE DATA ref_line LIKE LINE OF <fs_table>.
CATCH cx_root.
RETURN.
ENDTRY.
ASSIGN ref_line->* TO <fs_line>.
endif.
IF sy-subrc EQ 0.
get_elements( EXPORTING element = <fs_line>
name = ' ' ).
ENDIF.
RETURN.
ENDIF.
* not Tabs/Intf/Class/Ref to ..., - only struct and elements allowed
IF va_type_kind CA 'rgyl*+~?!hm'. EXIT. ENDIF.
* structures
IF va_type_kind CA 'uv'.
ref_struc ?= cl_abap_datadescr=>describe_by_data( p_data = element ).
LOOP AT ref_struc->components ASSIGNING <fs_comp>.
ASSIGN COMPONENT <fs_comp>-name OF STRUCTURE element TO <fs_any>.
IF sy-subrc EQ 0.
get_elements( EXPORTING element = <fs_any>
name = <fs_comp>-name ).
ENDIF.
ENDLOOP.
ELSE.
* element -> fieldcatalog
ref_elem ?= cl_abap_datadescr=>describe_by_data( p_data = element ).
va_name = ref_elem->get_relative_name( ).
IF va_name IS INITIAL.
va_name = name.
ENDIF.
* DDIC-item ? get ddic information
IF ref_elem->is_ddic_type( ) = 'X'.
TRY.
wa_dfies = ref_elem->get_ddic_field( ).
CATCH cx_root.
CLEAR wa_dfies.
ENDTRY.
ENDIF.
ADD 1 TO col_pos.
CLEAR ws_fcat.
ws_fcat-col_pos = col_pos.
ws_fcat-inttype = ref_elem->type_kind.
ws_fcat-intlen = ref_elem->length.
ws_fcat-outputlen = ref_elem->output_length.
ws_fcat-decimals_o = ref_elem->decimals.
ws_fcat-fieldname = name.
ws_fcat-seltext = name.
ws_fcat-scrtext_l = name.
ws_fcat-scrtext_m = name.
ws_fcat-scrtext_s = name.
wa_dfies-fieldname = name.
IF wa_dfies-leng IS NOT INITIAL.
IF wa_dfies-fieldname IS INITIAL.
wa_dfies-fieldname = va_name.
ENDIF.
MOVE-CORRESPONDING wa_dfies TO ws_fcat.
ENDIF.
* -> fieldcatalog
APPEND ws_fcat TO it_fieldcatalog.
ENDIF.
ENDMETHOD.
Code: Alles auswählen.
METHOD STRUC_DATA_XLSX.
* Importing IMP_SRVFILENAME TYPE STRING Appl Server filename
* Importing IMP_DATA TYPE REF TO DATA
* Exporting RETC TYPE SYSUBRC Return Value of ABAP Statements
*------------------------------------------------------------------------------*
* Änderungen ------------------------------------------------------------*
*------------------------------------------------------------------------------*
*------------------------------------------------------------------------------*
* K User Datum Grund *
*------------------------------------------------------------------------------*
*xxxxx wxxxxx yyyymmdd .......................................... *
*------------------------------------------------------------------------------*
DATA: va_xstring TYPE xstring.
CLEAR va_xstring.
retc = 0.
tab_2_xlsx( EXPORTING imp_data = imp_data
IMPORTING exp_xstring = va_xstring
retc = retc ).
IF retc NE 0.
RETURN.
ENDIF.
IF va_xstring IS INITIAL.
retc = 4.
RETURN.
ENDIF.
* create outputfile
TRY.
OPEN DATASET imp_srvfilename FOR OUTPUT IN BINARY MODE .
TRANSFER va_xstring TO imp_srvfilename.
CLOSE DATASET imp_srvfilename.
CATCH cx_root.
CLOSE DATASET imp_srvfilename.
retc = 5.
ENDTRY.
ENDMETHOD.
Code: Alles auswählen.
DATA it_mara TYPE TABLE OF mara.
DATA dref TYPE REF TO data.
DATA retc type sysubrc.
SELECT * FROM mara UP TO 200 ROWS
INTO TABLE it_mara.
GET REFERENCE OF it_mara INTO dref.
<zcl_irgendwas>=>STRUC_DATA_XLSX( EXPORTING IMP_SRVFILENAME = '\\SERVERPATHT\Mara_Test.xlsx'
imp_data = dref
IMPORTING retc = retc ).