如何在后台运行模式生成Excel格式文件

如果要生成纯正的Excel格式,一般可以采用普通OLE的方法,或者DOI(本质也是OLE)。但是这个方法有个前提,就是:必须在程序前台运行,并且前台的机器必须装了Office软件。
如何在后台生成Excel格式的文件呢?
本文就是解决这个问题。
说明:因为Excel支持XML格式的存储,所以我们使用Transformation program来产生Excel格式的XML文件,所以用这个方法产生的XLS文档还不是纯正的Excel二进制格式的文档,不过已经可以在大多数需要Excel格式的场合使用了,而且可以很方便的添加各种格式,比如字体大小、单元格颜色,,等等
需要注意的几点:
1、兼容一般的OpenOffice
2、Office2000之前的版本可能会打不开
3、如果包含图片、图表等,导出可能有困难
<!--StartFragment --> REPORTz_barry_test.

DATA:BEGINOFitabOCCURS0,
matnrLIKEmakt-matnr,
maktxLIKEmakt-maktx,
ENDOFitab.

DATA:xmlstrTYPEstring,
xml_tableTYPESTANDARDTABLEOFstring,
wa_xmlLIKELINEOFxml_table.

DATA:dname(120)TYPEc.

START-OF-SELECTION.

SELECTmatnrmaktxINTOTABLEitabFROMmakt
UPTO20ROWS
WHEREspras='1'.

CALLTRANSFORMATIONztestexcel
SOURCEtable=itab[]
RESULTXMLxmlstr.

REPLACEFIRSTOCCURRENCEOF'encoding="utf-16"'INxmlstrWITH'encoding="gbk"'.
APPENDxmlstrTOxml_table.

CALLFUNCTION'GUI_DOWNLOAD'
EXPORTING
filename='c:\xmlexcel.xls'
codepage='8400'
filetype='ASC'
TABLES
data_tab=xml_table.

*dname='/usr/sap/TZK/DVEBMGS00/work/xmlexcel.xls'.
*OPENDATASETdnameFOROUTPUTINTEXTMODEENCODINGDEFAULT.
*IFsy-subrcNE0.EXIT.ENDIF.
*LOOPATxml_tableINTOwa_xml.
*TRANSFERwa_xmlTOdname.
*ENDLOOP.
*CLOSEDATASETdname.
ztestexcel内容:
<?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>
<Worksheet ss:Name="Sheet1">
<Table ss:ExpandedColumnCount="3" ss:ExpandedRowCount="25" x:FullColumns="1" x:FullRows="1">
<Column ss:Width="120"/>
<Column ss:Width="220"/>
<tt:loop ref=".table">
<Row>
<Cell>
<Data ss:Type="String">
<tt:value ref="MATNR"/>
</Data>
</Cell>
<Cell>
<Data ss:Type="String">
<tt:value ref="MAKTX"/>
</Data>
</Cell>
</Row>
</tt:loop>
</Table>
</Worksheet>
</Workbook>
</tt:template>
</tt:transform>
注:如果要生成UTF-8的XML文件,可以更改CALL TRANSFORMATION的参数

你可能感兴趣的:(c,xml,Excel,Microsoft,Office)