abap webdynpro 导入excel并显示

sdn上有关于将excel如何通过abap webdynpro导入并显示的示例,但是找着去做之后会发现导入并显示出的是乱码。其实本身示例给出的就是按照txt的格式来做的,这其实是由于sap本身对于把excel得来的xstring重新转成string类型的过程无法正确匹配罢了,至今sap没有给出一个很好的解决办法,按照示例代码:

METHOD onactionon_upload . TYPES :
BEGIN OF str_itab,
name(10) TYPE c,
age(10) TYPE c,
END OF str_itab. DATA : t_table1 TYPE STANDARD TABLE OF str_itab,
i_data TYPE STANDARD TABLE OF string,
lo_nd_sflight TYPE REF TO if_wd_context_node,
lo_el_sflight TYPE REF TO if_wd_context_element,
l_string TYPE string,
fs_table TYPE str_itab,
l_xstring TYPE xstring,
fields TYPE string_table,
lv_field TYPE string.
DATA : t_table TYPE if_main=>elements_data_tab,
data_table TYPE if_main=>elements_data_tab.
* get single attribute wd_context->get_attribute(
EXPORTING
name = `DATASOURCE`
IMPORTING
value = l_xstring ).
data: conv type ref to cl_abap_conv_in_ce.
call method cl_abap_conv_in_ce=>create
exporting
input = l_xstring
encoding = 'UTF-8'
replacement = '?'
ignore_cerr = abap_true
receiving
conv = conv .
conv->read( importing data = l_string ).

[color=orange] CALL FUNCTION 'HR_KR_XSTRING_TO_STRING'
EXPORTING
in_xstring = l_xstring
IMPORTING
out_string = l_string. 废除[/color]
SPLIT l_string AT cl_abap_char_utilities=>newline INTO TABLE i_data.
* Bind With table Element.
LOOP AT i_data INTO l_string.
SPLIT l_string AT cl_abap_char_utilities=>horizontal_tab INTO TABLE fields.
READ TABLE fields INTO lv_field INDEX 1.
fs_table-name = lv_field.
READ TABLE fields INTO lv_field INDEX 2.
fs_table-age = lv_field.
APPEND fs_table TO t_table1.
ENDLOOP.
lo_nd_sflight = wd_context->get_child_node( 'DATA_TAB' ).
lo_nd_sflight->bind_table( t_table1 ).
ENDMETHOD.


DATASOURCE为xstring或string都可以。
废除部分其实无法正确将xstring转化为string。

在测试的时候需要将excel文件先保存为unicode的txt文件,然后再将txt文件保存为utf8格式导入方可。否则非utf8文本文件导入时会将文本所有内容在每一单元格显示。

你可能感兴趣的:(ABAP,Web,Dynpro)