一个简单小例子,将excel模板用STRANS生成XML格式,abap调用使用。
步骤如下:
我的数据源
相应的excel模板为:
将excel模板另存为XML文档,记事本打开,查看。
用t-code STRANS生成XML:
将excel模板生成的xml文档上传,其实我自己通常是保存一个模板,只要修改一下红色代码的地方,改为你sap数据的element,如下:
<?sap.transform simple?>
<?mso-application progid=”Excel.Sheet”?>
<tt:transform xmlns:tt=”http://www.sap.com/transformation-templates”>
<tt:root name=”table”/>
<tt:template>
<Workbook xmlns=”urn:schemas-microsoft-com:office:spreadsheet”
xmlns:o=”urn:schemas-microsoft-com:office:office”
xmlns:x=”urn:schemas-microsoft-com:office:excel”
xmlns:ss=”urn:schemas-microsoft-com:office:spreadsheet”
xmlns:html=”http://www.w3.org/TR/REC-html40″>
<ExcelWorkbook xmlns=”urn:schemas-microsoft-com:office:excel”>
</ExcelWorkbook>
<Styles>
<Style ss:ID=”Default” ss:Name=”Normal”>
<Alignment ss:Vertical=”Bottom”/>
<Borders/>
<Font ss:FontName=”Arial” x:Family=”Swiss”/>
<Interior/>
<NumberFormat/>
<Protection/>
</Style>
<Style ss:ID=”s62″>
<Alignment ss:Horizontal=”Left” ss:Vertical=”Bottom”/>
</Style>
<Style ss:ID=”s63″>
<Borders>
<Border ss:Position=”Bottom” ss:LineStyle=”Continuous” ss:Weight=”1″/>
<Border ss:Position=”Left” ss:LineStyle=”Continuous” ss:Weight=”1″/>
<Border ss:Position=”Right” ss:LineStyle=”Continuous” ss:Weight=”1″/>
<Border ss:Position=”Top” ss:LineStyle=”Continuous” ss:Weight=”1″/>
</Borders>
<Interior ss:Color=”#00CCFF” ss:Pattern=”Solid”/>
</Style>
</Styles>
<Worksheet ss:Name=”Sheet1″>
<Table x:FullColumns=”1″ x:FullRows=”1″>
<tt:loop ref=”.table”>
<Row>
<Cell><Data ss:Type=”String”><tt:value ref=”STUID”/></Data></Cell>
<Cell><Data ss:Type=”String”><tt:value ref=”STUNM”/></Data></Cell>
<Cell><Data ss:Type=”String”><tt:value ref=”STUSX”/></Data></Cell>
<Cell><Data ss:Type=”String”><tt:value ref=”STUTP”/></Data></Cell>
<Cell><Data ss:Type=”String”><tt:value ref=”STUAD”/></Data></Cell>
</Row>
</tt:loop>
</Table>
</Worksheet>
</Workbook>
</tt:template>
</tt:transform>
接下来程序调用:
程序界面,获得文件路径:
最后结果如下:
调用的程序代码如下:
*&———————————————————————*
*& Report YY_ELCT
*&
*&———————————————————————*
*&
*&
*&———————————————————————*
REPORT YY_ELCT.
types:begin of ty_out,
stuid type string,
stunm type string,
stusx type string,
stutp type string,
stuad type string,
end of ty_out.
data:gt_out type table of ty_out,
gw_out type ty_out.
data:gt_student type table of yyt_student,
gw_student type yyt_student.
data:g_xmlstr type string,
gt_xml type standard table of string,
gw_xml like line of gt_xml.
data g_filename type string.
selection-screen begin of block blk1 with frame title text-001.
parameters: p_down like rlgrap-filename default ‘D:\’.
selection-screen end of block blk1.
initialization.
*&———————————————————————*
* AT SELECTION-SCREEN
*&———————————————————————*
at selection-screen on value-request for p_down.
call function ‘WS_FILENAME_GET’
exporting
def_path = p_down
title = ‘Choose your file’(120)
mode = ‘S’
importing
filename = p_down
exceptions
inv_winsys = 1
no_batch = 2
selection_cancel = 3
selection_error = 4
others = 5.
check sy-subrc = 0 and not p_down is initial.
start-of-selection.
“get data
select * into table gt_student
from yyt_student.
“down file
clear gw_out.
gw_out-stuid = ‘Student Id’.
gw_out-stunm = ‘Student Name’.
gw_out-stusx = ‘Student Sex’.
gw_out-stutp = ‘Student Telphone’.
gw_out-stuad = ‘Student Address’.
append gw_out to gt_out.
clear gw_out.
loop at gt_student into gw_student.
move-corresponding gw_student to gw_out.
append gw_out to gt_out.
clear gw_out.
endloop.
call transformation ystu_excel
source table = gt_out
result xml g_xmlstr.
replace first occurrence of ‘encoding=”utf-16″‘ in g_xmlstr with ‘encoding=”gbk”‘.
append g_xmlstr to gt_xml.
g_filename = p_down.
call function ‘GUI_DOWNLOAD’
exporting
filename = g_filename
codepage = ‘8400′
filetype = ‘ASC’
tables
data_tab = gt_xml.
if sy-subrc eq 0.
message ‘Success’ type ‘S’.
else.
message ‘Download file fail’ type ‘I’.
endif.