Beides etwas unbefriedigend, aber ich werde mich wohl mit Möglichkeit 1 zufrieden geben müssen.Alexander D. hat geschrieben:hallo Ralf,
hier sind zwei mögliche Ansätze zu dem Thema:
http://ww.apentia-forum.de/viewtopic.php?f=2&t=17922
http://www.benx.de/benx%28bD1lbiZjPTAwM ... id=1459759
Code: Alles auswählen.
*&---------------------------------------------------------------------*
*& Include ZCL_NEW_CSVGEN *
*&---------------------------------------------------------------------*
*----------------------------------------------------------------------
*
* CLASS ZCL_NEW_CSVGEN DEFINITION
*----------------------------------------------------------------------
*
*
*----------------------------------------------------------------------
*
class zcl_new_csvgen definition.
type-pools : abap,
sydes.
public section.
types : i_self type ref to zcl_new_csvgen,
ty_shortterm type ref to zcl_new_shortterm.
types : begin of ty_csv,
data type di_data,
end of ty_csv.
types : ty_table type standard table of ty_csv.
class-methods create returning value(ref) type i_self.
methods : head importing e_structur type any
exporting i_head type di_data.
methods : csv importing e_anytab type any table
exporting i_csvtab type ty_table.
methods free.
private section.
types : ty_str256(256) type c.
types : begin of ty_in_out,
input type ty_str256,
f_dec type i,
counter type i,
shortterm type ty_shortterm,
end of ty_in_out.
data : ta_in_out type hashed table of ty_in_out
with unique key input f_dec.
types : begin of ty_lang,
row type i,
len type i,
end of ty_lang.
data : ta_lang type standard table of ty_lang.
data : my_error type sysubrc,
my_counter type i.
methods : kompakt importing p_table type any table.
methods : wandel importing p_input type any
returning value(my_string) type ty_str256.
endclass. "ZCL_NEW_CSVGEN DEFINITION
*----------------------------------------------------------------------*
* CLASS ZCL_NEW_CSVGEN IMPLEMANTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
*
class zcl_new_csvgen implementation.
method create.
create object ref.
endmethod. "create
method wandel.
data : f_times type i,
f_type type c,
f_dec type i,
f_len type i,
l_puffer type ty_str256.
data : wa_in_out type ty_in_out.
field-symbols : <fs_in_out> type ty_in_out.
my_counter = my_counter + 1.
if my_counter >= 100000.
my_counter = 0.
delete ta_in_out where counter = 0.
endif.
clear my_string.
check not p_input is initial.
describe field p_input type f_type.
case f_type.
when 'T'.
read table ta_in_out with table key input = p_input
f_dec = 0
assigning <fs_in_out>.
if sy-subrc = 0.
call method <fs_in_out>-shortterm->get_data
importing
i_data = my_string.
<fs_in_out>-counter = <fs_in_out>-counter + 1.
else.
search p_input for ':'.
if sy-subrc <> 0.
concatenate p_input+0(2) ':' p_input+2(2) ':' p_input+4(2)
into my_string.
else.
my_string = p_input.
endif.
clear wa_in_out.
wa_in_out-input = p_input.
wa_in_out-f_dec = 0.
wa_in_out-counter = 0.
wa_in_out-shortterm = zcl_new_shortterm=>create( ).
call method wa_in_out-shortterm->set_data
exporting
e_data = my_string
importing
i_error = my_error.
if my_error = 0.
insert wa_in_out into table ta_in_out.
endif.
endif.
when 'D'.
read table ta_in_out with table key input = p_input
f_dec = 0
assigning <fs_in_out>.
if sy-subrc = 0.
call method <fs_in_out>-shortterm->get_data
importing
i_data = my_string.
<fs_in_out>-counter = <fs_in_out>-counter + 1.
else.
search p_input for '.'.
if sy-subrc <> 0.
concatenate p_input+6(2) '.' p_input+4(2) '.' p_input+0(4)
into my_string.
else.
my_string = p_input.
endif.
clear wa_in_out.
wa_in_out-input = p_input.
wa_in_out-f_dec = 0.
wa_in_out-counter = 0.
wa_in_out-shortterm = zcl_new_shortterm=>create( ).
call method wa_in_out-shortterm->set_data
exporting
e_data = my_string
importing
i_error = my_error.
if my_error = 0.
insert wa_in_out into table ta_in_out.
endif.
endif.
when 'F'.
unpack p_input to my_string.
describe field p_input decimals f_dec.
read table ta_in_out with table key input = my_string
f_dec = f_dec
assigning <fs_in_out>.
if sy-subrc = 0.
call method <fs_in_out>-shortterm->get_data
importing
i_data = my_string.
<fs_in_out>-counter = <fs_in_out>-counter + 1.
else.
l_puffer = my_string.
shift my_string left deleting leading '0'.
f_len = strlen( my_string ).
if f_len < f_dec.
f_times = f_dec - f_len.
do f_times times.
concatenate '0' my_string into my_string.
enddo.
f_len = f_dec.
endif.
if f_dec > 0.
f_times = f_len - f_dec.
if f_times = 0.
concatenate '0,' my_string into my_string.
else.
concatenate my_string+0(f_times) ','
my_string+f_times(f_dec)
into my_string.
endif.
endif.
if p_input < 0.
concatenate '-' my_string into my_string.
endif.
clear wa_in_out.
wa_in_out-input = l_puffer.
wa_in_out-f_dec = f_dec.
wa_in_out-counter = 0.
wa_in_out-shortterm = zcl_new_shortterm=>create( ).
call method wa_in_out-shortterm->set_data
exporting
e_data = my_string
importing
i_error = my_error.
if my_error = 0.
insert wa_in_out into table ta_in_out.
endif.
endif.
when 'P'.
unpack p_input to my_string.
describe field p_input decimals f_dec.
read table ta_in_out with table key input = p_input
f_dec = f_dec
assigning <fs_in_out>.
if sy-subrc = 0.
call method <fs_in_out>-shortterm->get_data
importing
i_data = my_string.
<fs_in_out>-counter = <fs_in_out>-counter + 1.
else.
l_puffer = my_string.
shift my_string left deleting leading '0'.
f_len = strlen( my_string ).
if f_len < f_dec.
f_times = f_dec - f_len.
do f_times times.
concatenate '0' my_string into my_string.
enddo.
f_len = f_dec.
endif.
if f_dec > 0.
f_times = f_len - f_dec.
if f_times = 0.
concatenate '0,' my_string into my_string.
else.
concatenate my_string+0(f_times) ','
my_string+f_times(f_dec)
into my_string.
endif.
endif.
if p_input < 0.
concatenate '-' my_string into my_string.
endif.
clear wa_in_out.
wa_in_out-input = l_puffer.
wa_in_out-f_dec = f_dec.
wa_in_out-counter = 0.
wa_in_out-shortterm = zcl_new_shortterm=>create( ).
call method wa_in_out-shortterm->set_data
exporting
e_data = my_string
importing
i_error = my_error.
if my_error = 0.
insert wa_in_out into table ta_in_out.
endif.
endif.
when others.
read table ta_in_out with table key input = p_input
f_dec = 0
assigning <fs_in_out>.
if sy-subrc = 0.
call method <fs_in_out>-shortterm->get_data
importing
i_data = my_string.
<fs_in_out>-counter = <fs_in_out>-counter + 1.
else.
my_string = p_input.
if f_type = 'C'
or f_type = 'G'.
find ';' in my_string.
if sy-subrc = 0.
concatenate '"' my_string '"' into my_string.
endif.
endif.
condense my_string.
endif.
clear wa_in_out.
wa_in_out-f_dec = 0.
wa_in_out-counter = 0.
wa_in_out-input = p_input.
wa_in_out-shortterm = zcl_new_shortterm=>create( ).
call method wa_in_out-shortterm->set_data
exporting
e_data = my_string
importing
i_error = my_error.
if my_error = 0.
insert wa_in_out into table ta_in_out.
endif.
endcase.
endmethod. "wandel
method kompakt.
data : wa_lang type ty_lang.
" l_type(1) TYPE c.
field-symbols : <fs_co> type any,
<fs_wa> type any,
<fs_la> type ty_lang.
data : my_string type ty_str256,
my_erster type boolean,
olen type i.
" offset TYPE i.
free ta_lang.
my_erster = abap_false.
loop at p_table assigning <fs_wa>.
do.
assign component sy-index
of structure <fs_wa>
to <fs_co>.
if sy-subrc <> 0.
exit.
endif.
my_string = me->wandel( <fs_co> ).
olen = strlen( my_string ).
if my_erster = abap_false.
wa_lang-row = sy-index.
wa_lang-len = olen.
append wa_lang to ta_lang .
else.
read table ta_lang index sy-index assigning <fs_la>.
if olen > <fs_la>-len.
<fs_la>-len = olen.
endif.
endif.
enddo.
my_erster = abap_true.
endloop.
endmethod. "kompakt
method head.
types : feld_name type sydes_nameinfo.
data : td type sydes_desc,
f_names_tab type standard table of feld_name,
f_names type feld_name,
" f_name(15) TYPE c,
l_longname(61) type c,
l_index type i.
data : ref_fieldname type fieldname,
ref_tabname type tabname,
rollname type rollname,
l_lang type i,
scrtext_m type scrtext_m.
field-symbols : <fs_field> type any.
clear i_head.
do.
clear : scrtext_m,
rollname.
assign component sy-index of structure e_structur to <fs_field>.
if sy-subrc <> 0.
exit.
endif.
describe field <fs_field> into td.
f_names_tab[] = td-names[].
l_index = 2.
read table f_names_tab index l_index into f_names.
if sy-subrc = 0.
l_longname = f_names-name.
endif.
if f_names-continue = '*'.
do.
l_index = l_index + 1.
read table f_names_tab index l_index into f_names.
concatenate l_longname f_names-name into l_longname.
if f_names-continue <> '*'.
exit.
endif.
enddo.
endif.
find '-' in l_longname.
if sy-subrc = 0.
split l_longname at '-' into ref_tabname ref_fieldname.
select single rollname "#EC *
into rollname
from dd03l
where tabname = ref_tabname
and fieldname = ref_fieldname
and as4local = 'A'.
if sy-subrc <> 0.
l_lang = strlen( ref_fieldname ).
l_lang = l_lang - 1.
if l_lang > 1.
ref_fieldname+l_lang(1) = '%'.
select single rollname "#EC *
into rollname
from dd03l
where tabname = ref_tabname
and fieldname like ref_fieldname
and as4local = 'A'.
if sy-subrc <> 0.
rollname = '???'.
endif.
endif.
endif.
else.
rollname = l_longname.
endif.
if rollname <> ''.
select single scrtext_m "#EC *
into scrtext_m
from dd04t
where rollname = rollname
and ddlanguage = sy-langu
and as4local = 'A'.
if sy-subrc <> 0.
scrtext_m = '???'.
endif.
else.
scrtext_m = '???'.
endif.
concatenate i_head scrtext_m ';' into i_head.
enddo.
endmethod. "head
method csv.
field-symbols : <fs_co> type any,
<fs_wa> type any,
<fs_la> type ty_lang.
data : my_string type ty_str256,
wa_csv type ty_csv,
olen type i,
offset type i.
call method me->kompakt
exporting
p_table = e_anytab.
loop at e_anytab assigning <fs_wa>.
offset = 0.
clear wa_csv-data.
do.
assign component sy-index of structure <fs_wa> to <fs_co>.
if sy-subrc <> 0.
olen = strlen( wa_csv ).
olen = olen - 1.
wa_csv = wa_csv+0(olen).
append wa_csv to i_csvtab.
exit.
endif.
my_string = me->wandel( <fs_co> ).
write my_string to wa_csv-data+offset.
read table ta_lang index sy-index assigning <fs_la>.
offset = offset + <fs_la>-len.
write ';' to wa_csv-data+offset.
offset = offset + 1.
enddo.
endloop.
endmethod. "csv
method free.
field-symbols : <fs_in_out> type ty_in_out.
loop at ta_in_out assigning <fs_in_out>.
call method <fs_in_out>-shortterm->free.
endloop.
free : ta_in_out,
ta_lang.
call method cl_abap_memory_utilities=>do_garbage_collection.
endmethod. "free
endclass. "ZCL_NEW_CSVGEN IMPLEMENTATION
Code: Alles auswählen.
*&---------------------------------------------------------------------*
*& Form DOWNLOAD
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
form download .
types : begin of ty_short,
class type klasse_d, " Klassennummer
abtei type abteilung, " Anwendungssicht
atnam type atnam, " Merkmalname
end of ty_short.
data ta_short type standard table of ty_short.
field-symbols <fs_short> type ty_short.
types : begin of ty_csv,
data type di_data.
types : end of ty_csv.
data : ta_csv type standard table of ty_csv.
data : head type di_data,
l_error type sysubrc.
data : file_dir type string value 'c:\temp\',
file_name type string value '',
file_ext type string value 'csv'.
data : o_new_csv type ref to zcl_new_csvgen,
o_new_down type ref to zcl_new_asciidown.
field-symbols : <fs_line> type ty_csv.
o_new_csv = zcl_new_csvgen=>create( ).
loop at ta_table assigning <fs_table>.
append initial line to ta_short assigning <fs_short>.
<fs_short>-class = <fs_table>-class.
<fs_short>-abtei = <fs_table>-abtei.
<fs_short>-atnam = <fs_table>-atnam.
endloop.
o_new_csv = zcl_new_csvgen=>create( ).
read table ta_short index 1 assigning <fs_short>.
call method o_new_csv->head
exporting
e_structur = <fs_short>
importing
i_head = head.
call method o_new_csv->csv
exporting
e_anytab = ta_short
importing
i_csvtab = ta_csv.
call method o_new_csv->free.
clear o_new_csv.
o_new_down = zcl_new_asciidown=>create( ).
call method o_new_down->set_dos_modus.
* CALL METHOD o_new_down->asc_addline
* EXPORTING
* p_line = head
* IMPORTING
* p_error = l_error.
loop at ta_csv assigning <fs_line>.
call method o_new_down->asc_addline
exporting
p_line = <fs_line>-data
importing
p_error = l_error.
endloop.
call method o_new_down->asc_downlocal
exporting
p_dir = file_dir
p_filenam = file_name
p_fileext = file_ext
importing
p_error = l_error.
call method o_new_down->free.
clear o_new_down.
endform. " DOWNLOAD