本文主要是介绍讲IDOC生成的XML文件转化为excel文件的方法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
官方教程:http://help.sap.com/saphelp_erp2004/helpdata//EN/e3/7d4719ca581441b6841f1054ff1326/frameset.htm
关于转化的命令和基本格式都在里面
Command Overview
Command | Description |
<tt:apply ...> | Calls a subtemplate |
<tt:assign ...> | Assigns a value to data |
<tt:attribute ...> | Defines a non-literal attribute |
<tt:call ...> | Calls an ST program |
<tt:clear ...> | Initializes data |
<tt:cond ...> | Defines a conditioned transformation |
<tt:cond-var ...> | Defines a condition for variables |
<tt:context ...> | Defines a context for local data roots in subtemplates |
<tt:copy ...> | Transforms any data objects to asXML |
<tt:d-cond ...> | Defines a conditioned transformation for deserialization |
<tt:deserialize ...> | Defines the direction of the transformation |
<tt:empty ...> | Defines an empty pattern |
<tt:group ...> | Defines a group of conditioned transformations |
<tt:include ...> | Includes an ST program |
<... tt:lax=?...? ...> | Controls the name comparison of literal XML elements |
<tt:loop ...> | Transforms internal tables |
<tt:namespace ...> | Declares a namespace |
<tt:parameter ...> | Declares a parameter |
<tt:read ...> | Reads a variable from XML |
<tt:ref ...> | Sets the current node in the own context |
<... tt:ref=?...? ...> | Sets the current node for a literal XML element |
<tt:root ...> | Declares a data root |
<tt:serialize ...> | Defines the direction of the transformation |
<tt:s-cond ...> | Defines a conditioned transformation for the serialization |
<tt:skip ...> | Skips XML elements during deserialization |
<tt:switch ...> | Defines a case distinction between conditioned transformations |
<tt:switch-var ...> | Defines a case distinction for variables |
<tt:template ...> | Defines a main template or subtemplates |
<tt:text ...> | Explicitly identifies a literal text |
<tt:transform ...> | Root element of an ST program |
<tt:value ...> | Transforms elementary data objects |
<... tt:value-ref =?...? ...> | Short form for transformation of elementary data objects inliteral XML elements |
<tt:variable ...> | Declares a variable |
<tt:with-parameter ...> | Passes values to called ST programs and subtemplates |
<tt:with-root ...> | Passes data nodes to called ST programs and subtemplates |
<tt:write ...> | Writes the value of a variable to XML |
下面写一个具体的实例
程序实现把转换好的文件保存为xml
ZTEST_GLB_TRANS
*&---------------------------------------------------------------------*
*& Report ZTEST_GLB_TRANS
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT ztest_glb_trans.
TYPES: BEGIN OF xml_line,
data(100) TYPE c,
END OF xml_line.
DATA:
lv_xml TYPE string,
it_xml TYPE TABLE OF xml_line.
DATA: BEGIN OF l_header,
title1(100) TYPE c,
title2(100) TYPE c,
cus_name(100) TYPE c,
cus_val(100) TYPE c,
con_date(100) TYPE c,
con_val(100) TYPE c,
con_term(100) TYPE c,
con_tval(100) TYPE c,
quo_date(100) TYPE c,
quo_comm(100) TYPE c,
rep_date(100) TYPE c,
quo_no(100) TYPE c,
qty(100) TYPE c,
cond(100) type c,
type(100) type c,
des(100) type c,
amou(100) type c,
Unit(100) type c,
END OF l_header.
DATA td_header LIKE TABLE OF l_header.
DATA: BEGIN OF l_item,
item_type type c, "Package = ‘P’; Offer = ‘O’; Good = ‘G’; Subtotal = ‘S’
mat_des(100) TYPE c,
mat_num(100) type c,
qty(100) TYPE c,
cond(100) type c,
con_des(100) type c,
amou(100) type c,
Unit(100) type c,
END OF l_item.
DATA td_item LIKE TABLE OF l_item.
PERFORM get_data.
* call XML
CALL TRANSFORMATION ZMPS_PRICING_VALIDATE_XML
SOURCE it_header = td_header
it_item_data = td_item
RESULT XML lv_xml.
CALL FUNCTION 'SCMS_STRING_TO_FTEXT'
EXPORTING
text = lv_xml
TABLES
ftext_tab = it_xml.
***download
CALL FUNCTION 'GUI_DOWNLOAD'
EXPORTING
filename = 'C:\Test01.xls'
filetype = 'BIN'
TABLES
data_tab = it_xml.
WRITE:lv_xml.
*&---------------------------------------------------------------------*
*& Form GET_DATA
*&---------------------------------------------------------------------*
FORM get_data .
l_header-title1 = 'Managed Print Services'.
l_header-title2 = 'Pricing Validation Report'.
l_header-cus_name = 'Customer'.
l_header-cus_val = 'ABC Ltd'.
l_header-con_date = 'Contract Start Date'.
l_header-con_val = '8/1/2011'.
l_header-con_term = 'Contract Term (months)'.
l_header-con_tval = '48'.
l_header-quo_date = 'Quote Date: 07/01/2011'.
l_header-quo_comm = 'Valid for 45 days'.
l_header-rep_date = 'Report Date 07/01/2011'.
l_header-quo_no = 'Quote Ref: 123-456'.
l_header-qty = 'Qty'.
l_header-cond = 'Condition'.
l_header-type = 'type'.
l_header-des = 'Discription'.
l_header-amou = 'Amounts'.
l_header-Unit = 'Unit'.
APPEND l_header TO td_header.
CLEAR l_item.
l_item-mat_des = 'Package'.
l_item-item_type = 'P'.
APPEND l_item TO td_item.
CLEAR l_item.
l_item-mat_des = 'HP Hardware support'.
l_item-item_type = 'O'.
APPEND l_item TO td_item.
CLEAR l_item.
l_item-mat_des = 'HP LaserJet 4345MFP Printer'.
l_item-mat_num = 'Q3942A'.
l_item-qty = '15'.
l_item-cond = 'ZA73'.
l_item-con_des = 'Gross Monthly Base Price (Amount Based)'.
l_item-amou = '1200'.
l_item-unit = 'Dolllar'.
APPEND l_item TO td_item.
CLEAR l_item.
l_item-cond = 'YA9F'.
l_item-con_des = 'Item Discount on Base Price (Amount Based)'.
l_item-amou = '200'.
l_item-unit = 'Dolllar'.
APPEND l_item TO td_item.
CLEAR l_item.
l_item-cond = 'ZM97'.
l_item-con_des = 'Uplift on Unit Color Click Price'.
l_item-amou = '0'.
l_item-unit = 'Dollar Amt Per 1000'.
APPEND l_item TO td_item.
CLEAR l_item.
l_item-con_des = 'Sub Totals'.
l_item-amou = '1200'.
l_item-item_type = 'S'.
APPEND l_item TO td_item.
CLEAR l_item.
l_item-con_des = 'Tax'.
l_item-amou = '200'.
l_item-item_type = 'S'.
APPEND l_item TO td_item.
clear l_item.
ENDFORM. " GET_DATA
输入tcode;strans。
ZMPS_PRICING_VALIDATE_XML
<?sap.transform simple?>
<tt:transform xmlns:tt="http://www.sap.com/transformation-templates">
<tt:root name="it_header"/>
<tt:root name="it_item_data"/>
<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"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">
</ExcelWorkbook>
<Styles>
<Style ss:ID="s01">
<Font ss:FontName="Calibri" x:Family="Swiss" ss:Size="10" ss:Color="#000000"
ss:Italic="1"/>
</Style>
<Style ss:ID="s02">
<Alignment ss:Horizontal="Right" ss:Vertical="Bottom"/>
</Style>
<Style ss:ID="s03">
<Alignment ss:Horizontal="Center" ss:Vertical="Bottom"/>
<Borders/>
<Font ss:FontName="Calibri" x:Family="Swiss" ss:Size="14" ss:Color="#000000"
ss:Bold="1" ss:Italic="1"/>
<Interior/>
<NumberFormat/>
<Protection/>
</Style>
<Style ss:ID="s04">
<Alignment ss:Horizontal="Center" ss:Vertical="Bottom"/>
<Borders/>
<Font ss:FontName="Calibri" x:Family="Swiss" ss:Size="14" ss:Color="#000000"
ss:Bold="1"/>
<Interior/>
<NumberFormat/>
<Protection/>
</Style>
<Style ss:ID="s05">
<Alignment ss:Horizontal="Center" ss:Vertical="Bottom"/>
</Style>
<Style ss:ID="s06">
<Font ss:FontName="Calibri" x:Family="Swiss" ss:Size="11" ss:Color="#000000"
ss:Bold="1"/>
</Style>
<Style ss:ID="s07">
<Font ss:FontName="Calibri" x:Family="Swiss" ss:Size="12" ss:Color="#000000"
ss:Bold="1"/>
</Style>
<Style ss:ID="s08">
<Borders>
<Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="2"/>
</Borders>
</Style>
<Style ss:ID="s09">
<Borders>
<Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="2"/>
</Borders>
</Style>
<Style ss:ID="s10">
<Borders>
<Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="2"/>
<Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="2"/>
</Borders>
</Style>
<Style ss:ID="s11">
<Alignment ss:Horizontal="Right" ss:Vertical="Bottom"/>
<Font ss:FontName="Calibri" x:Family="Swiss"/>
<Borders>
<Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"/>
</Borders>
</Style>
<Style ss:ID="s12">
<Alignment ss:Horizontal="Center" ss:Vertical="Bottom"/>
<Borders>
<Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"/>
</Borders>
</Style>
<Style ss:ID="s13">
<Borders>
<Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"/>
</Borders>
</Style>
</Styles>
<Worksheet ss:Name="Pricing validation">
<Table ss:ExpandedColumnCount="50" x:FullColumns="1" x:FullRows="1">
<Column ss:Index="2" ss:Width="180"/>
<Column ss:Index="10" ss:Width="200"/>
<Column ss:Index="15" ss:Width="90"/>
<tt:loop name="header" ref=".it_header">
<Row>
<Cell ss:Index="9" ss:StyleID="s03">
<Data ss:Type="String">
<tt:value ref="$header.title1"/> <!--Managed Print Services-->
</Data>
</Cell>
<Cell ss:Index="16" ss:StyleID="s08">
</Cell>
</Row>
<Row>
<Cell ss:Index="9" ss:StyleID="s04">
<Data ss:Type="String">
<tt:value ref="$header.title2"/> <!--Pricing Validation Report-->
</Data>
</Cell>
<Cell ss:Index="16" ss:StyleID="s08">
</Cell>
</Row>
<Row>
<Cell ss:Index="2">
<Data ss:Type="String">
<tt:value ref="$header.cus_name"/> <!--Customer-->
</Data>
</Cell>
<Cell ss:Index="3">
<Data ss:Type="String">
<tt:value ref="$header.cus_val"/> <!--Customer value-->
</Data>
</Cell>
<Cell ss:Index="16" ss:StyleID="s08">
</Cell>
</Row>
<Row>
<Cell ss:Index="2">
<Data ss:Type="String">
<tt:value ref="$header.con_date"/> <!--Contract Start Date-->
</Data>
</Cell>
<Cell ss:Index="3">
<Data ss:Type="String">
<tt:value ref="$header.con_val"/> <!--Contract Start Date value-->
</Data>
</Cell>
<Cell ss:Index="16" ss:StyleID="s08">
</Cell>
</Row>
<Row>
<Cell ss:Index="2">
<Data ss:Type="String">
<tt:value ref="$header.con_term"/> <!--Contract Term (months)-->
</Data>
</Cell>
<Cell ss:Index="3">
<Data ss:Type="String">
<tt:value ref="$header.con_tval"/> <!--Contract Term (months) value-->
</Data>
</Cell>
<Cell ss:Index="14">
<Data ss:Type="String">
<tt:value ref="$header.rep_date"/> <!--Report Date + ref value-->
</Data>
</Cell>
<Cell ss:Index="16" ss:StyleID="s08">
</Cell>
</Row>
<Row>
<Cell ss:Index="2">
<Data ss:Type="String">
<tt:value ref="$header.quo_date"/> <!--Quote Date: + ref value-->
</Data>
</Cell>
<Cell ss:Index="3">
<Data ss:Type="String">
<tt:value ref="$header.quo_comm"/> <!--Valid for 45 days-->
</Data>
</Cell>
<Cell ss:Index="14">
<Data ss:Type="String">
<tt:value ref="$header.quo_no"/> <!--Quote Ref: + ref value-->
</Data>
</Cell>
<Cell ss:Index="16" ss:StyleID="s08">
</Cell>
</Row>
<Row>
<Cell>
<Data ss:Type="String"/>
</Cell>
<Cell ss:Index="16" ss:StyleID="s08">
</Cell>
</Row>
<Row>
<Cell ss:Index="8" ss:StyleID="s05">
<Data ss:Type="String">
<tt:value ref="$header.cond"/> <!--item header Condition-->
</Data>
</Cell>
<Cell ss:Index="16" ss:StyleID="s08">
</Cell>
</Row>
<Row>
<Cell ss:Index="6" ss:StyleID="s11">
<Data ss:Type="String">
<tt:value ref="$header.qty"/> <!--item header Qty-->
</Data>
</Cell>
<Cell ss:Index="7" ss:StyleID="s13">
</Cell>
<Cell ss:Index="8" ss:StyleID="s12">
<Data ss:Type="String">
<tt:value ref="$header.type"/> <!--item header type-->
</Data>
</Cell>
<Cell ss:Index="9" ss:StyleID="s13">
</Cell>
<Cell ss:Index="10" ss:StyleID="s13">
<Data ss:Type="String">
<tt:value ref="$header.des"/> <!--item header description-->
</Data>
</Cell>
<Cell ss:Index="11" ss:StyleID="s13">
</Cell>
<Cell ss:Index="12" ss:StyleID="s13">
</Cell>
<Cell ss:Index="13" ss:StyleID="s13">
<Data ss:Type="String">
<tt:value ref="$header.amou"/> <!--item header amout-->
</Data>
</Cell>
<Cell ss:Index="14" ss:StyleID="s13">
</Cell>
<Cell ss:Index="15" ss:StyleID="s11">
<Data ss:Type="String">
<tt:value ref="$header.Unit"/> <!--item header unit-->
</Data>
</Cell>
<Cell ss:Index="16" ss:StyleID="s08">
</Cell>
</Row>
</tt:loop>
<!--here is item data-->
<tt:loop name="item" ref=".it_item_data">
<Row>
<tt:switch>
<tt:cond data="$item.item_type='P'"> <!--when type is 'P'-->
<Cell ss:Index="2" ss:StyleID="s07">
<Data ss:Type="String">
<tt:value ref="$item.mat_des"/> <!--material package-->
</Data>
</Cell>
</tt:cond>
<tt:cond data="$item.item_type='O'"> <!--when type is 'O'-->
<Cell ss:Index="2" ss:StyleID="s06">
<Data ss:Type="String">
<tt:value ref="$item.mat_des"/> <!--material offer-->
</Data>
</Cell>
</tt:cond>
<tt:cond> <!--when type is others-->
<Cell ss:Index="2" >
<Data ss:Type="String">
<tt:value ref="$item.mat_des"/> <!--material goods-->
</Data>
</Cell>
</tt:cond>
</tt:switch>
<Cell ss:Index="3">
</Cell>
<tt:switch>
<tt:cond data="$item.item_type='P'"> <!--when type is 'P'-->
<Cell ss:Index="4" ss:StyleID="s07">
<Data ss:Type="String">
<tt:value ref="$item.mat_num"/> <!--package number-->
</Data>
</Cell>
</tt:cond>
<tt:cond data="$item.item_type='O'"> <!--when type is 'O'-->
<Cell ss:Index="4" ss:StyleID="s06">
<Data ss:Type="String">
<tt:value ref="$item.mat_num"/> <!--offer number-->
</Data>
</Cell>
</tt:cond>
<tt:cond> <!--when type is other-->
<Cell ss:Index="4">
<Data ss:Type="String">
<tt:value ref="$item.mat_num"/> <!--good number-->
</Data>
</Cell>
</tt:cond>
</tt:switch>
<Cell ss:Index="5">
</Cell>
<Cell ss:Index="6" ss:StyleID="s02">
<Data ss:Type="String">
<tt:value ref="$item.qty"/> <!--goods quatity-->
</Data>
</Cell>
<Cell ss:Index="7">
</Cell>
<Cell ss:Index="8" ss:StyleID="s05">
<Data ss:Type="String">
<tt:value ref="$item.cond"/> <!--goods condition type-->
</Data>
</Cell>
<Cell ss:Index="9">
</Cell>
<tt:switch>
<tt:cond data="$item.item_type='S'"> <!--when type is 'S'-->
<Cell ss:Index="10" ss:StyleID="s02">
<Data ss:Type="String">
<tt:value ref="$item.con_des"/> <!--goods condition description-->
</Data>
</Cell>
</tt:cond>
<tt:cond>
<Cell ss:Index="10"> <!--when type is not 'S'-->
<Data ss:Type="String">
<tt:value ref="$item.con_des"/> <!--subtotal/tax-->
</Data>
</Cell>
</tt:cond>
</tt:switch>
<Cell ss:Index="11">
</Cell>
<Cell ss:Index="12">
</Cell>
<Cell ss:Index="13" ss:StyleID="s02">
<Data ss:Type="String">
<tt:value ref="$item.amou"/> <!--goods amout-->
</Data>
</Cell>
<Cell ss:Index="14">
</Cell>
<Cell ss:Index="15" ss:StyleID="s02">
<Data ss:Type="String">
<tt:value ref="$item.unit"/> <!--goods unit-->
</Data>
</Cell>
<Cell ss:Index="16" ss:StyleID="s08">
</Cell>
</Row>
</tt:loop>
<Row>
<Cell ss:Index="16" ss:StyleID="s08">
</Cell>
</Row>
<Row>
<Cell ss:Index="16" ss:StyleID="s08">
</Cell>
</Row>
<Row>
<Cell ss:Index="1" ss:StyleID="s09">
</Cell>
<Cell ss:Index="2" ss:StyleID="s09">
</Cell>
<Cell ss:Index="3" ss:StyleID="s09">
</Cell>
<Cell ss:Index="4" ss:StyleID="s09">
</Cell>
<Cell ss:Index="5" ss:StyleID="s09">
</Cell>
<Cell ss:Index="6" ss:StyleID="s09">
</Cell>
<Cell ss:Index="7" ss:StyleID="s09">
</Cell>
<Cell ss:Index="8" ss:StyleID="s09">
</Cell>
<Cell ss:Index="9" ss:StyleID="s09">
</Cell>
<Cell ss:Index="10" ss:StyleID="s09">
</Cell>
<Cell ss:Index="11" ss:StyleID="s09">
</Cell>
<Cell ss:Index="12" ss:StyleID="s09">
</Cell>
<Cell ss:Index="13" ss:StyleID="s09">
</Cell>
<Cell ss:Index="14" ss:StyleID="s09">
</Cell>
<Cell ss:Index="15" ss:StyleID="s09">
</Cell>
<Cell ss:Index="16" ss:StyleID="s10">
</Cell>
</Row>
</Table>
<WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
<Zoom>81</Zoom>
<Selected/>
<DoNotDisplayGridlines/>
<FreezePanes/>
<FrozenNoSplit/>
<SplitHorizontal>9</SplitHorizontal>
<TopRowBottomPane>9</TopRowBottomPane>
<ActivePane>2</ActivePane>
<Panes>
<Pane>
<Number>3</Number>
</Pane>
<Pane>
<Number>2</Number>
<RangeSelection>R6C1:R6C21</RangeSelection>
</Pane>
</Panes>
<ProtectObjects>False</ProtectObjects>
<ProtectScenarios>False</ProtectScenarios>
</WorksheetOptions>
</Worksheet>
</Workbook>
</tt:template>
</tt:transform>
最后显示EXCEL显示效果如图所示
这篇关于讲IDOC生成的XML文件转化为excel文件的方法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!