(原创)带模板的DOI输出EXCEL
Posted 喜欢奶牛的MZ
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了(原创)带模板的DOI输出EXCEL相关的知识,希望对你有一定的参考价值。
与OLE一样,模板的作用就是将一些类似标题和表头行这种值固定,格式固定的单元格填充好数据和设置好格式后存储为模板上传到系统中。然后需要时根据上传模板的在SAP系统中的URL打开模板,再进行下一步的操作。
DOI上传模板的事务码是OAOR,下面阐述上传的具体步骤:
1.输入T-CODE:OAOR,进入界面后填入类名称,分类类型,对象代码后点击执行。
2.右键“表模板”->输入文件
3.打开自己保存好的模板文件,设置文件描述。
4.展开HRFPM_EXCEL_STANDARD文件夹,发现文件已上传完毕
模板文件上传之后,我们需要在程序中获取它的URL,主要是使用类CL_BDS_DOCUMENT_SET 的GET_WITH_URL 方法,具体用法见下面的DEMO示例。
DEMO示例:
输出目标:
模板:
创建的屏幕100:
源代码:
*&---------------------------------------------------------------------* *& Report Z15540_DOI1 *&---------------------------------------------------------------------* *& *&---------------------------------------------------------------------* REPORT Z15540_DOI2. TYPE-POOLS:SOI. "数据定义 DATA:BEGIN OF GS_DATA, WERKS TYPE MARD-WERKS, LGORT TYPE MARD-LGORT, MATNR TYPE MARD-MATNR, MAKTX TYPE MAKT-MAKTX, LABST TYPE MARD-LABST, MEINS TYPE MARA-MEINS, END OF GS_DATA. DATA GT_DATA LIKE TABLE OF GS_DATA. "DOI相关定义 DATA:LCL_CONTAINER TYPE REF TO CL_GUI_CONTAINER, "容器引用对象 LCL_CONTROL TYPE REF TO I_OI_CONTAINER_CONTROL, "容器控制器引用对象 LCL_DOCUMENT TYPE REF TO I_OI_DOCUMENT_PROXY, "对应EXCEL文档层次引用对象 LCL_SHEET TYPE REF TO I_OI_SPREADSHEET. "对应SHEET层次操作的接口引用对象 DATA:GV_URL TYPE BDS_URI."存放将要打开模板的URL START-OF-SELECTION. CALL SCREEN 100. *&---------------------------------------------------------------------* *& Form FRM_CONTAINER *&---------------------------------------------------------------------* *& text *&---------------------------------------------------------------------* *& --> p1 text *& <-- p2 text *&---------------------------------------------------------------------* FORM FRM_CONTAINER . "将CL_GUI_CONTAINER的静态属性SCREEN0付给LCL_CONTAINER LCL_CONTAINER = CL_GUI_CONTAINER=>SCREEN0. ENDFORM. *&---------------------------------------------------------------------* *& Form FRM_CONTAINER_CONTROL *&---------------------------------------------------------------------* *& text *&---------------------------------------------------------------------* *& --> p1 text *& <-- p2 text *&---------------------------------------------------------------------* FORM FRM_CONTAINER_CONTROL . "实例化容器控制器 CALL METHOD C_OI_CONTAINER_CONTROL_CREATOR=>GET_CONTAINER_CONTROL IMPORTING CONTROL = LCL_CONTROL. "绑定容器与容器控制器,并初始化控制器设定 CALL METHOD LCL_CONTROL->INIT_CONTROL EXPORTING * DYNPRO_NR = SY-DYNNR * GUI_CONTAINER = \' \' INPLACE_ENABLED = \'X\' "EXCEL可嵌入容器 * INPLACE_MODE = 0 * INPLACE_RESIZE_DOCUMENTS = \' \' INPLACE_SCROLL_DOCUMENTS = \'X\' "可滚动 * INPLACE_SHOW_TOOLBARS = \'X\' "是否展示工具栏以上 * NO_FLUSH = \'X\' * PARENT_ID = cl_gui_cfw=>dynpro_0 R3_APPLICATION_NAME = \'DOI TEST\' REGISTER_ON_CLOSE_EVENT = \'X\' "注册关闭事件 REGISTER_ON_CUSTOM_EVENT = \'X\' "注册定制事件 * REP_ID = SY-REPID * SHELL_STYLE = 1384185856 PARENT = LCL_CONTAINER * NAME = * AUTOALIGN = \'x\' * IMPORTING * ERROR = * RETCODE = EXCEPTIONS JAVABEANNOTSUPPORTED = 1 OTHERS = 2. IF SY-SUBRC <> 0. * Implement suitable error handling here ENDIF. ENDFORM. *&---------------------------------------------------------------------* *& Form FRM_CREATE_EXCEL *&---------------------------------------------------------------------* *& text *&---------------------------------------------------------------------* *& --> p1 text *& <-- p2 text *&---------------------------------------------------------------------* FORM FRM_CREATE_EXCEL . "实例化LCL_DOCUMENT CALL METHOD LCL_CONTROL->GET_DOCUMENT_PROXY EXPORTING * DOCUMENT_FORMAT = \'NATIVE\' DOCUMENT_TYPE = \'EXCEL.SHEET\' "应用类型 NO_FLUSH = \'X\' * REGISTER_CONTAINER = \' \' IMPORTING DOCUMENT_PROXY = LCL_DOCUMENT * ERROR = * RETCODE = . "打开XCEL文件 CALL METHOD LCL_DOCUMENT->OPEN_DOCUMENT EXPORTING * DOCUMENT_TITLE = \' \' DOCUMENT_URL = gv_url * NO_FLUSH = \' \' OPEN_INPLACE = \'X\' * OPEN_READONLY = \' \' * PROTECT_DOCUMENT = \' \' * ONSAVE_MACRO = \' \' * STARTUP_MACRO = \'\' * USER_INFO = * IMPORTING * ERROR = * RETCODE = . ENDFORM. *&---------------------------------------------------------------------* *& Form FRM_SET_DATA *&---------------------------------------------------------------------* *& text *&---------------------------------------------------------------------* *& --> p1 text *& <-- p2 text *&---------------------------------------------------------------------* FORM FRM_SET_DATA . DATA LV_ROW TYPE I."填充数据的行数 DATA:LS_RANGE TYPE SOI_RANGE_ITEM, LT_RANGE TYPE TABLE OF SOI_RANGE_ITEM, "为SET_RANGE_DATA方法标明填充数据的范围名及其行列数 LS_CONTENT TYPE SOI_GENERIC_ITEM, LT_CONTENT TYPE TABLE OF SOI_GENERIC_ITEM, "为SET_RANGE_DATA方法提供填充数据 LS_FORMATTABLE TYPE SOI_FORMAT_ITEM, "设置范围格式 LT_FORMATTABLE TYPE TABLE OF SOI_FORMAT_ITEM. "设置范围格式 DATA LV_COUNT_ROW TYPE I."当前行 DATA LV_LABST TYPE C LENGTH 18. FIELD-SYMBOLS:<FS_DATA_FILED>, <FS_CONTENT_FIELD>. SELECT MARD~WERKS, MARD~LGORT, MARD~MATNR, MAKT~MAKTX, MARD~LABST, MARA~MEINS INTO TABLE @GT_DATA UP TO 200 ROWS FROM MARD INNER JOIN MARA ON MARD~MATNR = MARA~MATNR LEFT OUTER JOIN MAKT ON MARD~MATNR = MAKT~MATNR WHERE MAKT~SPRAS = @SY-LANGU. DESCRIBE TABLE GT_DATA LINES LV_ROW."填充数据行数 DATA A1 TYPE I. CALL METHOD LCL_DOCUMENT->HAS_SPREADSHEET_INTERFACE EXPORTING NO_FLUSH = \'X\' IMPORTING * ERROR = IS_AVAILABLE = A1 * RETCODE = . "实例化SHEET对象 CALL METHOD LCL_DOCUMENT->GET_SPREADSHEET_INTERFACE EXPORTING NO_FLUSH = \'X\' IMPORTING * ERROR = SHEET_INTERFACE = LCL_SHEET * RETCODE = . "选中SHEET1 CALL METHOD LCL_SHEET->SELECT_SHEET EXPORTING NAME = \'Sheet1\' NO_FLUSH = \'X\' * IMPORTING * ERROR = * RETCODE = . *CALL METHOD LCL_SHEET->GET_ACTIVE_SHEET * EXPORTING * NO_FLUSH = \'X\' ** IMPORTING ** SHEETNAME = ** ERROR = ** RETCODE = * . "创建一个左上角为第一行第一列,共计6列200行,名叫RANGE1的范围对象 CALL METHOD LCL_SHEET->INSERT_RANGE_DIM EXPORTING NO_FLUSH = \'X\' NAME = \'RANGE1\' LEFT = 1 TOP = 4 ROWS = LV_ROW COLUMNS = 6 * UPDATING = -1 * SHEETNAME = \'\' * IMPORTING * ERROR = * RETCODE = . LT_RANGE = VALUE #( ( NAME = \'RANGE1\' ROWS = LV_ROW COLUMNS = \'6\' ) ). LOOP AT GT_DATA INTO GS_DATA. LV_COUNT_ROW = SY-TABIX. DO 6 TIMES. ASSIGN COMPONENT SY-INDEX OF STRUCTURE GS_DATA TO <FS_DATA_FILED>. LS_CONTENT-ROW = LV_COUNT_ROW. LS_CONTENT-COLUMN = SY-INDEX. IF SY-INDEX = 5. LV_LABST = <FS_DATA_FILED>. CONDENSE LV_LABST NO-GAPS. LS_CONTENT-VALUE = LV_LABST. ELSE. LS_CONTENT-VALUE = <FS_DATA_FILED>. ENDIF. APPEND LS_CONTENT TO LT_CONTENT. CLEAR LS_CONTENT. ENDDO. CLEAR GS_DATA. ENDLOOP. "填充数据 CALL METHOD LCL_SHEET->SET_RANGES_DATA EXPORTING NO_FLUSH = \'X\' RANGES = LT_RANGE CONTENTS = LT_CONTENT * UPDATING = -1 * RANGESDEF = * IMPORTING * ERROR = * RETCODE = . "设置范围字体格式 LT_FORMATTABLE = VALUE #( ( NAME = \'RANGE1\' SIZE = 11 ALIGN = 1 ) )."RANGE1字体大小11 水平居中 CALL METHOD LCL_SHEET->SET_RANGES_FORMAT EXPORTING FORMATTABLE = LT_FORMATTABLE NO_FLUSH = \'X\' * UPDATING = -1 * IMPORTING * ERROR = * RETCODE = . "设置范围边框格式 CALL METHOD LCL_SHEET->SET_FRAME EXPORTING RANGENAME = \'RANGE1\' TYP = \'127\' COLOR = 1 * NO_FLUSH = \' \' * IMPORTING * ERROR = * RETCODE = . "设置列自动优化 CALL METHOD LCL_SHEET->FIT_WIDEST EXPORTING NO_FLUSH = \'X\' NAME = SPACE * IMPORTING * ERROR = * RETCODE = . "更改SHEET名称 CALL METHOD LCL_SHEET->SET_SHEET_NAME EXPORTING NEWNAME = \'可用库存清单\' OLDNAME = \'Sheet1\' NO_FLUSH = \'X\' * IMPORTING * ERROR = * RETCODE = . * "保存文件(该方法会自动默认保存到c盘用户文件下的文档文件夹中) * CALL METHOD LCL_DOCUMENT->SAVE_AS * EXPORTING * FILE_NAME = \'可用库存清单\' * NO_FLUSH = \'X\' ** PROMPT_USER = \' \' ** IMPORTING ** ERROR = ** RETCODE = * . ENDFORM. *&---------------------------------------------------------------------* *& Module STATUS_0100 OUTPUT *&---------------------------------------------------------------------* *& *&---------------------------------------------------------------------* MODULE STATUS_0100 OUTPUT. SET PF-STATUS \'STANDARD\'. SET TITLEBAR \'DOI TEST\'. "获取模板的URL PERFORM FRM_GET_MBURL. "实例化容器 PERFORM FRM_CONTAINER. "创建容器控制器 PERFORM FRM_CONTAINER_CONTROL. "创建EXCEL文档 PERFORM FRM_CREATE_EXCEL. "填充数据 PERFORM FRM_SET_DATA. ENDMODULE. *&---------------------------------------------------------------------* *& Module USER_COMMAND_0100 INPUT *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* MODULE USER_COMMAND_0100 INPUT. "退出时释放实例。 IF LCL_DOCUMENT IS NOT INITIAL. CALL METHOD LCL_DOCUMENT->CLOSE_DOCUMENT. ENDIF. IF LCL_CONTAINER IS NOT INITIAL. CALL METHOD LCL_CONTAINER->FREE. ENDIF. IF LCL_CONTROL IS NOT INITIAL. CALL METHOD LCL_CONTROL->DESTROY_CONTROL. FREE LCL_CONTROL. ENDIF. LEAVE PROGRAM. ENDMODULE. *&---------------------------------------------------------------------* *& Form FRM_GET_MBURL *&---------------------------------------------------------------------* *& text *&---------------------------------------------------------------------* *& --> p1 text *& <-- p2 text *&---------------------------------------------------------------------* FORM FRM_GET_MBURL. DATA:LV_CLASSNAME TYPE SBDST_CLASSNAME VALUE \'HRFPM_EXCEL_STANDARD\', LV_CLASSTYPE TYPE SBDST_CLASSTYPE VALUE \'OT\', LV_OBJECT_KEY TYPE SBDST_OBJECT_KEY VALUE \'DOIMB\', LT_URL TYPE SBDST_URI, LS_URL LIKE LINE OF LT_URL. CALL METHOD CL_BDS_DOCUMENT_SET=>GET_WITH_URL EXPORTING * LOGICAL_SYSTEM = CLASSNAME = LV_CLASSNAME CLASSTYPE = LV_CLASSTYPE * CLIENT = OBJECT_KEY = LV_OBJECT_KEY * URL_LIFETIME = * SELECTED_INDEX = CHANGING URIS = LT_URL * SIGNATURE = * COMPONENTS = EXCEPTIONS NOTHING_FOUND = 1 ERROR_KPRO = 2 INTERNAL_ERROR = 3 PARAMETER_ERROR = 4 NOT_AUTHORIZED = 5 NOT_ALLOWED = 6 OTHERS = 7. IF SY-SUBRC = 0. READ TABLE LT_URL INTO LS_URL INDEX 1. GV_URL = LS_URL-URI. ENDIF. ENDFORM.
以上是关于(原创)带模板的DOI输出EXCEL的主要内容,如果未能解决你的问题,请参考以下文章
SAP 下载EXCEL DOI使用OLE功能(DOI获取OLE对象)