ITPub博客

首页 > Linux操作系统 > Linux操作系统 > ABAP中如何使用OLE

ABAP中如何使用OLE

原创 Linux操作系统 作者:chouer523 时间:2019-05-14 20:36:09 0 删除 编辑

*******************************************************
声明:原创文章--版权所有---转载请保留信息
MSN:
njchouer@hotmail.com
作者:chouer
******************************************************

昨天花了好长时间终于把报表中的数据导到Excel的时候,使用了固定的格式。其实如果不是为了汇到Excel中,输出的报表完全可以用Smartform进行数据输出,这样看上去也很舒服。


report rsdemo01 no standard page heading.
include ole2incl.
* handles for OLE objects
data: gs_excel type ole2_object,
gs_wbooklist type ole2_object,
gs_application type ole2_object,
gs_wbook type ole2_object,
gs_activesheet type ole2_object,
gs_sheets type ole2_object,
gs_newsheet type ole2_object,
gs_cell1 type ole2_object,
gs_cell2 type ole2_object,
gs_cells type ole2_object,
gs_font type ole2_object.

tables: spfli.
data h type i.
* table of flights
data: it_spfli like spfli occurs 10 with header line.
data: gv_sheet_name(20) type c.
data: x1 type p, x2 type p, y1 type p, y2 type p.
data: value(100),value1(100).
data: name(20),size(2).
data: bold type p, underline type p, alignment type p.
*&---------------------------------------------------------------------*
*& Event START-OF-SELECTION
*&---------------------------------------------------------------------*
value = '这个是测试时候的标题,真累'.
size = '20'.
alignment = 3.
start-of-selection.
* read flights
select * from spfli into table it_spfli up to 10 rows.

**end-of-selection
end-of-selection.
* start Excel
create object gs_excel 'EXCEL.APPLICATION'.
set property of gs_excel 'Visible' = 1.
get property of gs_excel 'Workbooks' = gs_wbooklist.

* get list of workbooks, initially empty
call method of gs_excel 'Workbooks' = gs_wbooklist.
* add a new workbook
call method of gs_wbooklist 'Add' = gs_wbook.

*write header
perform fill_cell using 1 1 1 name.
perform format_header using gs_cells value name size bold alignment
underline.
move 1 to x1.
move 1 to y1.
move 1 to x2.
move 10 to y2.
perform merged using x1 y1 x2 y2.
clear x1. clear x2. clear y1. clear y2.

move 2 to x1.
move 1 to y1.
move 2 to x2.
move 2 to y2.
perform fill_cell using 2 1 1 sy-datum.
perform merged using x1 y1 x2 y2.


* output column headings to active Excel sheet
perform fill_cell using 3 1 1 'Flug'(001).
perform fill_cell using 3 2 1 'Nr'(002).
perform fill_cell using 3 3 1 'Von'(003).
perform fill_cell using 3 4 1 'Nach'(004).
perform fill_cell using 3 5 1 'Zeit'(005).
loop at it_spfli.
* copy flights to active EXCEL sheet
h = sy-tabix + 3.
perform fill_cell using h 1 0 it_spfli-carrid.
perform fill_cell using h 2 0 it_spfli-connid.
perform fill_cell using h 3 0 it_spfli-cityfrom.
perform fill_cell using h 4 0 it_spfli-cityto.
perform fill_cell using h 5 0 it_spfli-deptime.
endloop.
* disconnect from Excel
free object gs_excel.

*---------------------------------------------------------------------*
* FORM FILL_CELL *
*---------------------------------------------------------------------*
* sets cell at coordinates i,j to value val boldtype bold *
*---------------------------------------------------------------------*
form fill_cell using i j bold val.
call method of gs_excel 'Cells' = gs_cells exporting #1 = i #2 = j.
set property of gs_cells 'Value' = val .
get property of gs_cells 'Font' = gs_font.
set property of gs_font 'Bold' = bold .
endform.

*------------------------------
*该函数用来合并excel单元格
*------------------------------
form merged using x1 y1 x2 y2.
*--Selecting cell area to be merged.
call method of gs_excel 'Cells' = gs_cell1
exporting
#1 = x1
#2 = y1.
call method of gs_excel 'Cells' = gs_cell2
exporting
#1 = x2
#2 = y2.
call method of gs_excel 'Range' = gs_cells
exporting
#1 = gs_cell1
#2 = gs_cell2.
call method of gs_cells 'Select'.
*--Merging
call method of gs_cells 'Merge' .
endform. "merged
*&---------------------------------------------------------------------*
*& Form format_header
*&---------------------------------------------------------------------*
* 格式化头
*----------------------------------------------------------------------*
form format_header using gs_cell1 value name size bold alignment
underline.
condense value.
condense name.
condense size.
get property of gs_cell1 'Font' = gs_font .
set property of gs_font 'Name' = name.
set property of gs_font 'Size' = size.
set property of gs_font 'Bold' = bold. "Not bold
set property of gs_font 'Italic' = '1'. "Not Italic
set property of gs_font 'Underline' = underline. "Not underlined
set property of gs_cell1 'horizontalAlignment' = alignment.
" && 水平方向 2左对齐,3居中,4右对齐
*SET PROPERTY OF gs_cell1 'VerticalAlignment' = 3.
"&& 垂直方向 1靠上 ,2居中,3靠下
set property of gs_cell1 'NumberFormatLocal' =
'@'.
"&& 设置数据格式
set property of gs_cell1 'Value' = value.
endform. " format_header

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/332542/viewspace-53566/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-03-28

  • 博文量
    74
  • 访问量
    47767