DOI输出EXCEL的例子.docx
- 文档编号:6142139
- 上传时间:2023-01-04
- 格式:DOCX
- 页数:9
- 大小:260.99KB
DOI输出EXCEL的例子.docx
《DOI输出EXCEL的例子.docx》由会员分享,可在线阅读,更多相关《DOI输出EXCEL的例子.docx(9页珍藏版)》请在冰豆网上搜索。
DOI输出EXCEL的例子
DOI输出EXCEL的例子
DOI(DesktopOfficeIntegration)是SAI提供的与OFFICE集成的技术,他的功能非常的强大,甚至可以使用EXCEL公式和VBA扩展功能。
有关DOI的详细介绍请参看SAPLibrary的指定章节。
有详细的结构定义和方法说明。
用DOI实现用EXCEL输出报表的功能是比较有用的。
我们今天就以此为例讲解一下DOI的使用。
也就是把一个制作好的EXCEL的模板文件通过事务代码OAOR导入到系统中备用。
然后写程序用DOI技术读取该模板,向模板中填写数据。
最后输出出来。
因为可以支持公式和VBA扩展功能,所以可以实现自动计算、统计以及EXCEL图标功能。
(1)用OAOR导入EXCEL文件
(2)然后就可以编程用DOI实现报表的输出:
为了输出EXCEL,需要创建一个屏幕,并在屏幕上放置一个容器,详细代码如下:
*&---------------------------------------------------------------------*
*& Report ZZKP052
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT ZLJC_TEST NO STANDARD PAGE HEADING .
DATA:
document_name(30) VALUE 'TESTDOI',
active_sheet(50) VALUE 'Sheet1'.
CONSTANTS inplace VALUE 'X'.
DATA:
container TYPE REF TO cl_gui_custom_container,
control TYPE REF TO i_oi_container_control,
document TYPE REF TO i_oi_document_proxy,
spreadsheet TYPE REF TO i_oi_spreadsheet,
error TYPE REF TO i_oi_error,
errors TYPE REF TO i_oi_error OCCURS 0 WITH HEADER LINE.
DATA:
rangeitem TYPE soi_range_item.
DATA:
ranges TYPE soi_range_list.
DATA:
excel_input TYPE soi_generic_table.
DATA:
excel_input_wa TYPE soi_generic_item.
DATA:
initialized
(1), retcode TYPE soi_ret_string.
DATA:
item_url(256), already_done, newname(40).
DATA document_type(80).
DATA:
excel(80) VALUE 'Excel.Sheet'.
DATA:
line_count TYPE i,
column_count TYPE i.
DATA:
ok_code LIKE sy-ucomm.
PARAMETERS flag.
START-OF-SELECTION.
CALL SCREEN 100.
*&——————————————————————-
*& Module OUTPUT_TO_EXCEL OUTPUT
*&——————————————————————-
MODULE status_0100 OUTPUT.
SET PF-STATUS '100'.
* 创建EXCEL模板
PERFORM create_basic_objects USING '' '' '' '' document_name.
* 输出EXCEL
PERFORM output_to_excel.
ENDMODULE. "' OUTPUT_TO_EXCEL OUTPUT
*———————————————————————*
* FORM create_basic_objects *
*———————————————————————*
FORM create_basic_objects USING p_app_name
p_classname
p_classtype
p_obj_key
p_docname.
DATA l_app_name(200).
DATA:
bds_instance TYPE REF TO cl_bds_document_set.
DATA:
doc_signature TYPE sbdst_signature,
wa_doc_signature LIKE LINE OF doc_signature,
doc_components TYPE sbdst_components,
doc_uris TYPE sbdst_uri,
wa_doc_uris LIKE LINE OF doc_uris.
DATA:
doc_classname TYPE sbdst_classname VALUE 'PICTURES',
doc_classtype TYPE sbdst_classtype VALUE 'OT',
doc_object_key TYPE sbdst_object_key VALUE 'EXCEL'.
CHECK initialized IS INITIAL.
* 处理参数
IF p_app_name IS INITIAL.
l_app_name = 'R/3 Reporter'.
ELSE.
l_app_name = p_app_name.
ENDIF.
IF NOT ( p_classname IS INITIAL OR p_classtype IS INITIAL
OR p_obj_key IS INITIAL ).
doc_classname = p_classname.
doc_classtype = p_classtype.
doc_object_key = p_obj_key.
ENDIF.
* 创建容器控制器实例
CALL METHOD c_oi_container_control_creator=>get_container_control
IMPORTING
control = control
error = error.
CALL METHOD error->raise_message
EXPORTING
type = 'E'.
* 创建屏幕上的容器对象
CREATE OBJECT container
EXPORTING
container_name = 'CONTAINER'.
* 初始化容器控制器
CALL METHOD control->init_control
EXPORTING
r3_application_name = l_app_name
inplace_enabled = inplace
inplace_scroll_documents = 'X'
parent = container
register_on_close_event = 'X'
register_on_custom_event = 'X'
no_flush = 'X'
IMPORTING
error = errors.
APPEND errors.
CLEAR item_url.
wa_doc_signature-prop_name = 'DESCRIPTION'.
document_type = excel.
wa_doc_signature-prop_value = p_docname.
APPEND wa_doc_signature TO doc_signature.
* BDS对象实例化
CREATE OBJECT bds_instance.
* 读取BDS内容(模板)
CALL METHOD bds_instance->get_info
EXPORTING
classname = doc_classname
classtype = doc_classtype
object_key = doc_object_key
CHANGING
components = doc_components
signature = doc_signature.
* 读取BDS的URL
CALL METHOD bds_instance->get_with_url
EXPORTING
classname = doc_classname
classtype = doc_classtype
object_key = doc_object_key
CHANGING
uris = doc_uris
signature = doc_signature.
FREE bds_instance.
READ TABLE doc_uris INTO wa_doc_uris INDEX 1.
item_url = wa_doc_uris-uri.
* ask the SAP DOI container for a i_oi_document_proxy for Excel
* 容器控制器获得一个EXCEL文档代理
CALL METHOD control->get_document_proxy
EXPORTING
document_type = 'Excel.Sheet'
no_flush = 'X'
IMPORTING
document_proxy = document
error = errors.
APPEND errors.
* open a document saved in business document service.
* 容器控制器中打开指定BDS返回的文档
CALL METHOD document->open_document
EXPORTING
open_inplace = inplace
document_url = item_url.
DATA:
has TYPE i.
CALL METHOD document->has_spreadsheet_interface
EXPORTING
no_flush = ''
IMPORTING
is_available = has
error = errors.
APPEND errors.
CALL METHOD document->get_spreadsheet_interface
EXPORTING
no_flush = ' '
IMPORTING
sheet_interface = spreadsheet
error = errors.
APPEND errors.
* Activate sheet
CALL METHOD spreadsheet->select_sheet
EXPORTING
name = active_sheet
no_flush = ''
IMPORTING
error = errors.
APPEND errors.
*错误处理
LOOP AT errors.
CALL METHOD errors->raise_message
EXPORTING
type = 'S'.
ENDLOOP.
FREE errors.
initialized = 'X'.
ENDFORM. "' CREATE_BASIC_OBJECTS
*———————————————————————*
* FORM output_to_excel *
*———————————————————————*
FORM output_to_excel.
DATA:
tmpdate(10) TYPE c.
column_count = 2.
line_count = 2.
PERFORM fill_cell USING line_count
column_count
'TEST1'.
line_count = line_count + 1.
PERFORM fill_cell USING line_count
column_count
'TEST2'.
column_count = column_count + 1.
PERFORM fill_cell USING line_count
column_count
'ljc'.
* call METHOD spreadsheet->PROTECT
* EXPORTING
* protect = 'X'.
ENDFORM. "output_to_excel
*———————————————————————*
* FORM fill_cell *
*———————————————————————*
FORM fill_cell USING i j val.
DATA:
columns_number TYPE i,
rows_number TYPE i.
columns_number = 1.
rows_number = 1.
CALL METHOD spreadsheet->insert_range_dim
EXPORTING
name = 'cell'
no_flush = 'X'
top = i
left = j
rows = rows_number
columns = columns_number
IMPORTING
error = errors.
APPEND errors.
REFRESH:
ranges, excel_input.
rangeitem-name = 'cell'.
rangeitem-columns = 1.
rangeitem-rows = 1.
APPEND rangeitem TO ranges.
excel_input_wa-column = 1.
excel_input_wa-row = 1.
excel_input_wa-value = val.
APPEND excel_input_wa TO excel_input.
* set data
CALL METHOD spreadsheet->set_ranges_data
EXPORTING
ranges = ranges
contents = excel_input
no_flush = 'X'
IMPORTING
error = errors.
APPEND errors.
CALL METHOD spreadsheet->fit_widest
EXPORTING
name = space
no_flush = 'X'.
REFRESH:
ranges, excel_input.
ENDFORM. "fill_cell
*&———————————————————————*
*& Module EXIT INPUT
*&———————————————————————*
MODULE user_command_0100 INPUT.
IF NOT document IS INITIAL.
CALL METHOD document->close_document.
FREE document.
ENDIF.
IF NOT control IS INITIAL.
CALL METHOD control->destroy_control.
FREE control.
ENDIF.
SET SCREEN 0 .
ENDMODULE. " EXIT INPUT
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- DOI 输出 EXCEL 例子
![提示](https://static.bdocx.com/images/bang_tan.gif)