1)在excel做好表格的格式;
2)在ABAP程序输出数据到指定路径的文件
(1)定义使用excel容器
include ole2incl.
data:
excel type ole2_object, " Excel object
book type ole2_object, " list of workbooks
sheet type ole2_object, " workbook
cell type ole2_object, " cell
rows type i. "excel行
(2)建立SAP与EXCEL的接口
create object
excel 'Excel.Application'.
call method of
excel 'Workbooks' = book.
call method of
book 'Open'
exporting #1 = 'D:/XXX.XLS'. "存储EXCEL格式表的路径及文件名
call method of
excel 'Worksheets' = sheet exporting #1 = 1.
call method of
sheet 'Activate'.
(3)输出数据
rows = 3. "从EXCEL表格的第三行开始写入数据(前两行是Title)
loop at
itab.
call method of
excel 'CELLS' = cell exporting #1 = rows #2 = 1. "写入到第rows行第一列
set property of
cell 'VALUE' = itab-fld1. "写到excel第rows行第一列的内表列值
...
rows = rows + 1.
endloop.
set property of
excel 'Visible' = 1. "打开excel
free object
excel.
使用这种方法,可以让用户很灵活的按自己需要的打印格式打印报表,就和使用excel一样。
如果导出到execel后需要强制分页:
(1)增加定义
CONSTANTS: xlpagebreakmanual TYPE I VALUE-4135.
(2)加入要强制分行的行语句即可:
call method of
excel 'Rows' = rows exporting #1 = 4 .
set property of
rows 'PageBreak' = xlpagebreakmanual.