ABAP学习(8):操做EXCEL

ABAP操做EXCEL

一、EXCEL导入内表

方式1:服务器

    调用function,’F4_FILENAME’获取文件名。经过屏幕元素Prameter的帮助事件,弹出文件选择框,获取选择文件名。app

    调用function,’TEXT_CONVERT_XLS_TO_SAP’,将选择excel数据放入内表。frontend

示例:ide

TYPE-POOLS truxs. DATA:it_raw TYPE truxs_t_text_data. "定义一个内表来存储数据,内表的列数和要传得数据的列数要相同,其按照列来匹配传值

DATA: BEGIN OF gt_data OCCURS 0, col1 TYPE char10, col2 TYPE char10, END OF gt_data. PARAMETERS:p_file TYPE rlgrap-filename. "调用F4_FILENAME,点击输入框后小方块弹出文件选择框

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file. CLEAR p_file. CALL FUNCTION 'F4_FILENAME'

    IMPORTING file_name = p_file."返回文件名

 

*Excel传值

START-OF-SELECTION. PERFORM exceltotab. FORM exceltotab . CALL FUNCTION 'TEXT_CONVERT_XLS_TO_SAP'

  EXPORTING

* I_FIELD_SEPERATOR = ' ' "分隔符?
 i_line_header = 'X'         "是否去掉首行
 i_tab_raw_data = it_raw     "WORK TABLE
 i_filename = p_file TABLES i_tab_converted_data = gt_data[] "ACTUAL DATA

  EXCEPTIONS conversion_failed = 1 OTHERS = 2. IF sy-subrc <> 0. MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4. ENDIF. ENDFORM.                    "exceltotab
View Code

 

方式2测试

直接弹出文件选择弹窗,获取文件路径:字体

data: ts_files type filetable, rc type i. refresh ts_files. DATA:filename(1024) type C. "调用方法,弹出文件选择框,获取选中文件路径
 call method cl_gui_frontend_services=>file_open_dialog exporting window_title = 'Select File' default_extension = '*.xls' multiselection = ' '    " Only one file
 changing file_table = ts_files rc = rc exceptions file_open_dialog_failed = 1 cntl_error = 2 error_no_gui = 3 not_supported_by_gui = 4 others = 5. read table ts_files into filename index 1.

调用function ‘ALSM_EXCEL_TO_INTERNAL_TABLE’,将excel表内容读入ABAP内表。ui

"将excel读取到内表'ALSM_EXCEL_TO_INTERNAL_TABLE'

DATA :excelTab like alsmex_tabline OCCURS 10 WITH HEADER LINE. Data :fileTest like rlgrap-filename VALUE 'D:\myexcel.xlsx'. call function 'ALSM_EXCEL_TO_INTERNAL_TABLE' exporting filename = fileTest i_begin_col = 1 i_begin_row = 1 i_end_col = 20 i_end_row = 20000 tables intern = excelTab exceptions inconsistent_parameters = 1 upload_ole = 2 others = 3.

 

自定义function:‘ALSM_EXCEL_TO_INTERNAL_TABLE’url

 

Structure:ALSMEX_TABLINE,其中VALUE字段类型为CHAR50,若是上传字段长度超过CHAR50,那么上传数据就会不全,因此咱们须要自定义Function,修改ALSMEX_TABLINE的VALUE字段。spa

自定义步骤:excel

1.复制Sturcture:ALSMEX_TABLINE,自定义名字TEST_ALSMEX_TABLINE修改VALUE的数据类型,定义为需求长度;

2.复制function:ALSM_EXCEL_TO_INTERNAL_TABLE,复制以前先建立目标Function group,否则会复制失败;

 

 

3.在新建Function Group建立include: LZTEST_EXCELF01,将ALSMEX下,includes:LALSMEXF01内容复制到新建include。

 

 

 4.将ALSMEX下,includes:LALSMEXTOP内容复制到新建Function Group的includes:LZTEST_EXCELTOP中,而后检查,激活。

示例:

* value of excel-cell
TYPES: ty_d_itabvalue             TYPE alsmex_tabline-value, * internal table containing the excel data
       ty_t_itab                  TYPE alsmex_tabline   OCCURS 0,

5.修改TEST_EXCEL_TO_INTERNAL_TABLE,将INTERN参数对应数据类型修改成自定义的TEST_ALSMEX_TABLINE,将LZTEST_EXCELTOP中的ty_d_itabvalue和ty_t_itab对应的数据类型也修改成自定义类型。

 

6.程序中调用function便可。

示例:

"将excel读取到内表'ALSM_EXCEL_TO_INTERNAL_TABLE'
  DATA :exceltab like alsmex_tabline OCCURS 10 WITH HEADER LINE. Data :filetest like rlgrap-filename VALUE 'D:test.xlsx'. * CALL FUNCTION 'ALSM_EXCEL_TO_INTERNAL_TABLE'
  CALL FUNCTION 'TEST_EXCEL_TO_INTERNAL_TABLE'
    EXPORTING filename = fileTest i_begin_col = 1 i_begin_row = 1 i_end_col = 20 i_end_row = 20000
    TABLES intern = exceltab EXCEPTIONS inconsistent_parameters = 1 upload_ole = 2 others = 3.

 

二、OLE2操做EXCEL

使用ABAP程序操做EXCEL,将内表数据写出到EXCEL

OLE2方式操做EXCEL,OLE是将全部EXCEL中组成结构封装成一个ole2的类。经过ole2对象方法来操做Excel。

一、类对象:lo_application:一个Excel;

经过create object语句建立一个excel对象,

CREATE OBJECT lo_application 'Excel.Application'.  "建立excel对象

设置EXCEL的属性,是否可见:

SET PROPERTY OF lo_application 'Visible' = 1.  "设置excel可见

SET PROPERTY OF lo_application 'SheetsInNewWorkbook' = 1.

 

二、类对象:lo_workbooks :工做簿集合;

经过建立的excel对象,调用Workbooks方法,建立他的工做薄集,

CALL METHOD OF lo_application 'Workbooks'  = lo_workbooks. "调用lo_application的'Workbooks'建立工做簿集

 

三、类对象:lo_workbook:工做簿;

经过建立的工做薄集对象,调用add方法,建立他的工做薄,

CALL METHOD OF lo_workbooks   'Add'        = lo_workbook.  "调用lo_workbooks 的方法'Add' ,建立工做薄

 

四、类对象:lo_worksheets:工做表,sheet集合;

    经过EXCEL对象的Sheets方法,建立sheet集合对象

"建立一个sheet集合

CALL METHOD OF lo_application 'Sheets' = lo_worksheets . 经过sheet集合对象,调用add方法,能够建立多个sheet对象。建立出的sheet对象调用Activate方法,能够激活。 "建立新的work sheet. "默认建立的最新的sheet,处于active状态

CALL METHOD OF lo_worksheets 'Add' = new_worksheet. *CALL METHOD OF new_worksheet 'Activate'. "激活当前新的sheet

 

五、类对象:lo_worksheet:工做表,sheet;

    经过EXCEL对象的Activesheet属性,能够获取到当前活动的sheet,这个sheet就是建立初始时,默认存在的sheet。

GET PROPERTY OF lo_application 'ACTIVESHEET' = lo_worksheet.  "获取当前活动sheet,初始建立时,只有一个sheet,且active

     设置sheet的名称,设置sheet的属性name:

SET PROPERTY OF lo_worksheet 'Name' = 'first sheet'. "设置sheet名

 

六、类对象:lo_cell:excel单元格;

    经过sheet对象,调用方法Cells方法,在当前sheet中建立单元格对象。

"建立单元个

CALL METHOD OF lo_worksheet 'Cells' = lo_cell EXPORTING # = 1  "Row
 # = 1. "Column

    设置单元格对象属性value,为单元格赋值:

SET PROPERTY OF lo_cell 'value' = ''.   "设置单元个值

   设置单元格对象属性NumberFormat,设置单元格数据格式:

"设置单元格值格式,支持excel中的格式

*SET PROPERTY OF lo_cell 'NumberFormat' = '0.00'.

SET PROPERTY OF lo_cell 'NumberFormat' = 'm/d/yy'.

  设置单元格自动换列

"特别长文字,自动换列,看到所有文字

 SET PROPERTY OF lo_cell 'WrapText' = 1.

  设置水平对齐方式

"设置水平对齐方式

"center = -4108

"left = -4131

"right = -4152

"top = -4160

SET PROPERTY OF lo_cell 'HorizontalAlignment' = -4108.

类对象:lo_font:字体设置对象,调用单元格对象的font方法,获取font对象,经过font对象设置单元格字体。

"设置单元格字体

CALL METHOD OF lo_cell 'font' = lo_font. "EXCEL支持的字体名

*SET PROPERTY OF lo_font 'Name' = 'Agency FB'. "字体名

SET PROPERTY OF lo_font 'Name' = '微软正黑体'.   "字体名

SET PROPERTY OF lo_font 'Size' = 15.        "大小

"颜色数字规则?

SET PROPERTY OF lo_font 'Color' = -12123441. "颜色

SET PROPERTY OF lo_font 'TintAndShade' = 0. "明亮度-1~1,1彻底透明

SET PROPERTY OF lo_font 'Bold' = 0.  "是否粗体字 0 :false 1:true

SET PROPERTY OF lo_font 'Italic' = 1. "是否斜体 0:false 1:true

SET PROPERTY OF lo_font 'Underline' = 2. "xlUnderlineStyleSingle = 2,下划线

类对象lo_interior,单元格背景;

示例:

经过单元格对象获取lo_interior对象,设置Color或者ColorIndex属性,设置背景颜色,两种方式均可。对应颜色值不清楚?

"设置单元格背景

DATA:lo_interior TYPE ole2_object. CALL METHOD OF lo_cell 'interior' = lo_interior. SET PROPERTY OF lo_interior 'Color' = 12123441. SET PROPERTY OF lo_interior 'ColorIndex' = 43.

 

八、类对象:lo_range:多个单元格集合;

选择多个单元格集合。

示例:

选择单元格(1,1)到(3,3)这部分区域:

"选择Ranges of cells

CALL METHOD OF lo_worksheet 'Cells' = lo_cellstart EXPORTING # = 1 "row
 # = 1. "column

CALL METHOD OF lo_worksheet 'Cells' = lo_cellend EXPORTING # = 3 "row
 # = 3. "column

"选中一个区域Cell

CALL METHOD OF lo_worksheet 'RANGE' = lo_range EXPORTING # = lo_cellstart # = lo_cellend.

也能够选中一行或者一列单元格:

类对象:lo_column:excel行

"选择列

CALL METHOD OF lo_worksheet 'Columns' = lo_column EXPORTING # = 1.

类对象:lo_row:excel列

"选择行

CALL METHOD OF lo_worksheet 'Row' = lo_row EXPORTING # = 2.

若是要操做选择部分区域须要lo_selection对象

类对象:lo_selection,选中操做对象

"获取选中区域selection

CALL METHOD OF lo_row 'Select'. CALL METHOD OF lo_application 'selection' = lo_selection.

 

九、类对象:lo_validation:验证操做validation对象;

示例:测试失败?

"添加验证

"选中(4,4)

CALL METHOD OF lo_worksheet 'Cells' = lo_cell EXPORTING # = 4 # = 4. "获取选中区域selection

CALL METHOD OF lo_cell 'Select'. CALL METHOD OF lo_application 'selection' = lo_selection. "获取验证对象lo_validation

CALL METHOD OF lo_selection 'Validation' = lo_validation. "添加验证规则

CALL METHOD OF lo_validation 'Add'

  EXPORTING #1 = 4 "验证类型Type 4 = xlValidateDate,验证日期
 #2 = 1 "验证弹窗类型AlertStype 1 = xlValidAlertStop
 #3 = 1 "操做类型,Operator 1 = xlBetween,在之间
 #4 = '1/1/2000'   "Formula1
 #5 = '1/1/2010'.  "Formula2

"设置错误信息

SET PROPERTY OF lo_validation 'ErrorMessage' = 'Enter a valid date'.

 

十、使用复制粘贴方式将内表数数据写入EXCEL

示例:

"复制粘贴方式将数据写入EXCEL

CALL METHOD OF lo_worksheet 'Activate'.   "激活当前新的sheet

 

DATA:test_spfli LIKE TABLE OF spfli WITH HEADER LINE. TYPES: ty_data(1500) TYPE c. DATA: lt_data TYPE ty_data OCCURS 0 WITH HEADER LINE. DATA:lv_cont TYPE I. FIELD-SYMBOLS: <field>  TYPE ANY. SELECT * FROM spfli INTO TABLE test_spfli. * Prepare the data before copy to clipboard;

LOOP AT test_spfli. lv_cont = 1. * Write for example 5 columns per row.

  DO 5 TIMES. ASSIGN COMPONENT lv_cont OF STRUCTURE test_spfli TO <field>. "使用#将数据连接起来,数据格式#xxx#xxxx#xxxx

    CONCATENATE lt_data <field> INTO lt_data SEPARATED BY cl_abap_char_utilities=>horizontal_tab. ADD 1 TO lv_cont. ENDDO. "去掉开始#号

  SHIFT lt_data BY 1 PLACES LEFT. APPEND lt_data. CLEAR lt_data. ENDLOOP. * Copy to clipboard into ABAP

CALL FUNCTION 'CONTROL_FLUSH'

  EXCEPTIONS OTHERS = 3. CALL FUNCTION 'CLPB_EXPORT'

  TABLES data_tab = lt_data EXCEPTIONS clpb_error = 1 OTHERS = 2. * Select the cell A1

CALL METHOD OF lo_worksheet 'Cells'      = lo_cell EXPORTING #1           = 1  "Row
 #2           = 1. "Column

 

* Paste clipboard from cell A1

CALL METHOD OF lo_cell 'SELECT'. CALL METHOD OF lo_worksheet 'PASTE'. "调用lo_workbook 'SaveAs' 方法保存excel

CALL METHOD OF lo_workbook 'SaveAs'

  EXPORTING # = lv_complete_path. if sy-subrc = 0. MESSAGE 'File downloaded successfully' TYPE 'S'. ELSE. MESSAGE 'Error downloading the file' TYPE 'E'. ENDIF.

完整示例:

TYPE-POOLS: soi,ole2. DATA: lo_application TYPE  ole2_object,  "表明excel对象
 lo_workbook TYPE  ole2_object,  "表明excel工做薄
 lo_workbooks TYPE  ole2_object,  "表明excel工做薄集合
 lo_range TYPE  ole2_object,  "表明多个单元格集合
 lo_worksheet TYPE ole2_object,  "表明excel工做表sheet
 lo_worksheets TYPE ole2_object,  "表明excel工做表sheet集合
 lo_column TYPE  ole2_object,  "表明excel列
 lo_row TYPE  ole2_object,  "表明excel行
 lo_cell TYPE  ole2_object,  "表明excel单元格
 lo_font TYPE  ole2_object.  "表明字体

 

DATA: lo_cellstart      TYPE ole2_object, lo_cellend TYPE ole2_object, lo_selection TYPE ole2_object, lo_validation TYPE ole2_object. DATA: lv_selected_folder TYPE string, lv_complete_path TYPE char256, lv_titulo TYPE string .  "弹窗描述

 

DATA: new_worksheet TYPE ole2_object. "新sheet

 

"调用文件路径选择框,默认路径C:\

CALL METHOD cl_gui_frontend_services=>directory_browse EXPORTING window_title = lv_titulo   "弹窗最上方,能够添加描述信息,不是弹窗标题
 initial_folder = 'C:\' "初始化选择路径 CHANGING selected_folder = lv_selected_folder "返回选择路径 EXCEPTIONS cntl_error = 0 error_no_gui = 1 OTHERS = 2. "检查是否获取到路径 CHECK NOT lv_selected_folder IS INITIAL. DATA:str_len TYPE I. DATA:temp_sign(1) TYPE C. str_len = STRLEN( lv_selected_folder ) - 1. temp_sign = lv_selected_folder+str_len(1). if temp_sign = '\'. "肯定保存路径 CONCATENATE lv_selected_folder 'Test' INTO lv_complete_path. else. "肯定保存路径 CONCATENATE lv_selected_folder '\Test' INTO lv_complete_path. ENDIF. "建立excel对象 CREATE OBJECT lo_application 'Excel.Application'. "建立excel对象 SET PROPERTY OF lo_application 'Visible' = 1. "设置excel可见 SET PROPERTY OF lo_application 'SheetsInNewWorkbook' = 1. "建立workbook CALL METHOD OF lo_application 'Workbooks' = lo_workbooks. "调用lo_application的'Workbooks'建立工做簿集 CALL METHOD OF lo_workbooks 'Add' = lo_workbook. "调用lo_workbooks 的方法'Add' ,建立工做薄 GET PROPERTY OF lo_application 'ACTIVESHEET' = lo_worksheet. "获取当前活动sheet,初始建立时,只有一个sheet,且active SET PROPERTY OF lo_worksheet 'Name' = 'first sheet'. "设置sheet名 "建立一个sheet集合 CALL METHOD OF lo_application 'Sheets' = lo_worksheets . "建立新的work sheet. "默认建立的最新的sheet,处于active状态 CALL METHOD OF lo_worksheets 'Add' = new_worksheet. *CALL METHOD OF new_worksheet 'Activate'. "激活当前新的sheet SET PROPERTY OF new_worksheet 'Name' = 'second sheet'. "设置sheet名 "lo_application默认sheet,设置值 "建立单元个 CALL METHOD OF lo_worksheet 'Cells' = lo_cell EXPORTING # = 1 "Row # = 1. "Column SET PROPERTY OF lo_cell 'value' = ''. "设置单元个值 "设置单元格字体 CALL METHOD OF lo_cell 'font' = lo_font. "EXCEL支持的字体名 *SET PROPERTY OF lo_font 'Name' = 'Agency FB'. "字体名 SET PROPERTY OF lo_font 'Name' = '微软正黑体'. "字体名 SET PROPERTY OF lo_font 'Size' = 15. "大小 "建立单元个 CALL METHOD OF lo_worksheet 'Cells' = lo_cell EXPORTING # = 1 "Row # = 2. "Column SET PROPERTY OF lo_cell 'value' = '12/03/2010'. "设置单元个值 "设置单元格值格式,支持excel中的格式 *SET PROPERTY OF lo_cell 'NumberFormat' = '0.00'. SET PROPERTY OF lo_cell 'NumberFormat' = 'm/d/yy'. "设置单元格字体 CALL METHOD OF lo_cell 'font' = lo_font. "颜色数字规则? SET PROPERTY OF lo_font 'Color' = -12123441. "颜色 SET PROPERTY OF lo_font 'TintAndShade' = 0. "明亮度-1~1,1彻底透明 SET PROPERTY OF lo_font 'Bold' = 0. "是否粗体字 0 :false 1:true SET PROPERTY OF lo_font 'Italic' = 1. "是否斜体 0:false 1:true SET PROPERTY OF lo_font 'Underline' = 2. "xlUnderlineStyleSingle = 2,下划线 "设置单元格背景 DATA:lo_interior TYPE ole2_object. CALL METHOD OF lo_cell 'interior' = lo_interior. SET PROPERTY OF lo_interior 'Color' = 12123441. "设置单元格边框 DATA:lo_borders TYPE ole2_object. "获取边框对象,输入参数‘7’:左边框xlEdgeLeft;‘8’:上边框xlEdgeTop;‘9’:下边框xlEdgeBottom;‘10’:右边框xlEdgeRight CALL METHOD OF lo_cell 'borders' = lo_borders EXPORTING # = 7. SET PROPERTY OF lo_borders 'LineStyle' = 1."xlContinuous='1' SET PROPERTY OF lo_borders 'Weight' = 4."xlThick = 4 "在对应new sheet设置单元格值 "建立单元个 CALL METHOD OF new_worksheet 'Cells' = lo_cell EXPORTING # = 1 "Row # = 1. "Column SET PROPERTY OF lo_cell 'value' = 'hello2'. "设置单元个值 "建立单元个 CALL METHOD OF new_worksheet 'Cells' = lo_cell EXPORTING # = 1 "Row # = 2. "Column SET PROPERTY OF lo_cell 'value' = 'welcome2'. "设置单元个值 *"选择Ranges of cells *CALL METHOD OF lo_worksheet 'Cells' = lo_cellstart * EXPORTING * # = 1 "row * # = 1. "column * *CALL METHOD OF lo_worksheet 'Cells' = lo_cellend * EXPORTING * # = 3 "row * # = 3. "column * *"选中一个区域Cell *CALL METHOD OF lo_worksheet 'RANGE' = lo_range * EXPORTING * # = lo_cellstart * # = lo_cellend. * "选择列 *CALL METHOD OF lo_worksheet 'Columns' = lo_column * EXPORTING * # = 1. *"选择行 *CALL METHOD OF lo_worksheet 'Row' = lo_row * EXPORTING * # = 2. * *"获取选中区域selection *CALL METHOD OF lo_row 'Select'. *CALL METHOD OF lo_application 'selection' = lo_selection. *"添加验证 *"选中(4,4) *CALL METHOD OF lo_worksheet 'Cells' = lo_cell * EXPORTING * # = 4 * # = 4. *"获取选中区域selection *CALL METHOD OF lo_cell 'Select'. *CALL METHOD OF lo_application 'selection' = lo_selection. *"获取验证对象lo_validation *CALL METHOD OF lo_selection 'Validation' = lo_validation. *"添加验证规则 *CALL METHOD OF lo_validation 'Add' * EXPORTING * #1 = 4 "验证类型Type 4 = xlValidateDate,验证日期 * #2 = 1 "验证弹窗类型AlertStype 1 = xlValidAlertStop * #3 = 1 "操做类型,Operator 1 = xlBetween,在之间 * #4 = '1/1/2000' "Formula1 * #5 = '1/1/2010'. "Formula2 *"设置错误信息 *SET PROPERTY OF lo_validation 'ErrorMessage' = 'Enter a valid date'. "调用lo_workbook 'SaveAs' 方法保存excel CALL METHOD OF lo_workbook 'SaveAs' EXPORTING # = lv_complete_path. if sy-subrc = 0. MESSAGE 'File downloaded successfully' TYPE 'S'. ELSE. MESSAGE 'Error downloading the file' TYPE 'E'. ENDIF. CALL METHOD OF lo_application 'QUIT'. "退出excel "释放资源 FREE OBJECT lo_worksheet. FREE OBJECT new_worksheet. FREE OBJECT lo_workbook. FREE OBJECT lo_workbooks. FREE OBJECT lo_application.
View Code

 

三、DOI操做EXCEL

DOI:Desktop Office Integretion

四个对象:

container: 存放excel电子表格(spreadsheet)的容器。

展现spreadsheet确定须要一个容器来存放。这个容器通常在dialog screen中定义,也能够直接使用ABAP程序默认的screen(即screen号码为1000的屏幕)。

container control: 容器中用于建立和管理其余Office集成所须要的对象。container control是一个接口,类型是i_oi_container_control。

document proxy: 每个document proxy的实例表明用office application打开的文档,能够是excel,也能够是word。若是想打开多个文档,须要定义多个实例。document proxy是一个接口,类型为i_oi_document_proxy。

spreadsheet: spreadsheet接口,表明最终要操做的excel文档。spreadhseet的类型是i_oi_spreadsheet

 

经过Tcode:OAOR,能够将本地文档模板上传到服务器行,经过对应类读取到ABAP程序中进行操做。

若是读取服务器上的文档模板,须要cl_bds_document_set类:

business document set: bds是business document set的缩写。

business document set用于管理后续要操做的文档,能够包含一个或多个文档。

示例:

"容器对象

DATA:gr_container type ref to cl_gui_container. "管理Excel或其余文档对象

DATA:gr_control type ref to i_oi_container_control. "文档对象,打开保存文档操做

DATA:gr_document type ref to i_oi_document_proxy. "sheet操做接口对象

DATA:gr_spreadsheet type ref to i_oi_spreadsheet. "sheet中选择操做区域range

DATA:gr_ranges type SOI_RANGE_LIST. DATA:gr_ranges_item TYPE SOI_RANGE_ITEM. "对应range的填充内容

DATA:gr_contents TYPE SOI_GENERIC_TABLE. DATA:gr_contents_item TYPE SOI_GENERIC_ITEM. start-of-selection. perform main. *get container
 form get_container. "获取容器对象,实例化
 gr_container = cl_gui_container=>screen0."screen0 表明当前1000默认屏幕
 endform. "get_container

 

* create container control
 form create_container_control. "调用方法,建立管理对象control

  CALL METHOD c_oi_container_control_creator=>get_container_control IMPORTING control = gr_control. "初始化control对象

* initialize control

  CALL METHOD gr_control->init_control EXPORTING inplace_enabled = 'X'  "嵌入屏幕显示‘x’
 inplace_scroll_documents = 'X'  "可滚动
 register_on_close_event = 'X' register_on_custom_event = 'X' r3_application_name = 'DOI demo' parent = gr_container. endform. "create excel document
 form create_excel_document. CALL METHOD gr_control->get_document_proxy EXPORTING document_type = 'Excel.Sheet' no_flush = 'X'

    IMPORTING document_proxy = gr_document. "open_inplace参数控制excel文档是独立显示仍是在SAP GUI中嵌入显示。

  "若是嵌入显示,gr_control的init_control方法中,inplace_enabled参数要设为X

  CALL METHOD gr_document->create_document EXPORTING document_title = 'DOI test' no_flush = '' open_inplace = ''. endform. "create_excel_document

 

"使用spreedsheet填充数据
 form fill_data. "sheet的名字

  DATA:sheetname(20) TYPE C . DATA:error TYPE REF TO I_OI_ERROR. DATA:retcode TYPE SOI_RET_STRING. "调用document的方法,获取sheet对象

  CALL METHOD gr_document->get_spreadsheet_interface EXPORTING no_flush = ''

    IMPORTING sheet_interface = gr_spreadsheet error = error. "spreadsheet能够获取当前活动sheetname

  CALL METHOD gr_spreadsheet->get_active_sheet EXPORTING no_flush = ''

    IMPORTING sheetname = sheetname error = error retcode = retcode. "获取当前活动sheet指定ranges区域

  CALL METHOD gr_spreadsheet->insert_range_dim EXPORTING no_flush = '' name = 'range1' left = 1 top = 1 rows = 1 columns = 2

* updating = 1

* sheetname = 'sheet1'

    IMPORTING error = error retcode = retcode. * "修改range的名字,范围

* CALL METHOD gr_spreadsheet->change_range

* EXPORTING

* rangename = 'range1'

* newname = 'range1'

* rows = 1

* columns = 1

** updating = 1 "默认-1

* IMPORTING

* error = error

* retcode = retcode.

 

  "设置ranges区域字体

  CALL METHOD gr_spreadsheet->set_font EXPORTING rangename = 'range1'  "range的名字
 family = 'Times New Roman' size = 9 bold = 0    "加粗
 italic = 0    "斜体
 align = 0    "水平居中

    IMPORTING error = error retcode = retcode. "设置ranges区域格式

  CALL METHOD gr_spreadsheet->set_format EXPORTING rangename = 'range1' typ = 0 currency = 'RMB' decimals = 1

    IMPORTING error = error retcode = retcode. "设置ranges区域
 gr_ranges_item-name = 'range1'. gr_ranges_item-rows = 1. gr_ranges_item-columns = 2. APPEND gr_ranges_item TO gr_ranges. "设置内容,value 256长度
 gr_contents_item-row = 1. gr_contents_item-column = 1. gr_contents_item-value = '112'. APPEND gr_contents_item TO gr_contents. gr_contents_item-row = 1. gr_contents_item-column = 2. gr_contents_item-value = '113'. APPEND gr_contents_item TO gr_contents. "设置sheet中的内容

  CALL METHOD gr_spreadsheet->set_ranges_data EXPORTING no_flush = '' ranges = gr_ranges   "选中区域
 contents = gr_contents "填充内容

* updating = ''

* rangesdef = ''

    IMPORTING retcode = retcode error = error. * "建立一个新的sheet

* call method gr_spreadsheet->add_sheet

* exporting

* name = 'sheet1'

* no_flush = ''

* importing

* error = error

* retcode = retcode.

* "删除一个sheet

* call method gr_spreadsheet->delete_sheet

* exporting

* name = 'sheet1'

* no_flush = ''

* importing

* error = error

* retcode = retcode.

  "active指定name的sheet

* CALL METHOD gr_spreadsheet->select_sheet

* EXPORTING

* name = 'sheet1'

* no_flush = ''

* IMPORTING

* error = error

* retcode = retcode.

* "修改sheet名字

* CALL METHOD gr_spreadsheet->set_sheet_name

* EXPORTING

* newname = 'sheet1'

* oldname = sheetname

* no_flush = ''

* IMPORTING

* error = error

* retcode = retcode.
 endform. "保存文档
 form save_doc. DATA:error TYPE REF TO I_OI_ERROR. DATA:retcode TYPE SOI_RET_STRING. "保存的文件路径

  DATA:file_name(20) TYPE C VALUE 'D:\test.xls'. "只传文件名,不设置路径,该方法会自动默认保存到c盘用户文件下的文档文件夹中)

  CALL METHOD gr_document->save_as EXPORTING file_name = file_name prompt_user = ''

    IMPORTING error = error retcode = retcode. endform. "释放资源
 form free_source. "关闭文档

  CALL METHOD gr_document->close_document. "释放容器

  CALL METHOD gr_container->free. "关闭文档管理

  CALL METHOD gr_control->destroy_control. FREE gr_control. LEAVE PROGRAM. endform. form main. * skip 1.
 perform get_container. perform create_container_control. perform create_excel_document. Perform fill_data. perform save_doc. perform free_source. endform.
View Code

 

带模板的DOI操做:

一、使用Tcode:OAOR,上传指定格式的文档;、

二、经过调用cl_bds_document_set=>get_with_url方法,获取咱们上传文档的URL;

三、而后经过gr_document->open_document,将获取的URL传入,打开的就是咱们上传的模板文档;

 

使用OAOR,上传模板。Class name:HRFPM_EXCEL_STANDARD;Class Type:OT;Object Key:能够本身定义,访问时也是经过这个找到咱们文件,示例:DOI_EXCEL_TEXT。

 

    能够选择咱们要上传的文档类型。这里咱们选中excel,双击会弹出一个文件选择框,选择咱们要上传的模板文件就能够了。

 

    修改一下描述,或者默认,点击确认就能够了。

 

    咱们能够看到已经上传成功。

使用代码访问到咱们上传的文档,获取文档的URL

示例:

*business document system
 data: gr_bds_documents type ref to cl_bds_document_set, g_classname type sbdst_classname, g_classtype type sbdst_classtype, g_objectkey type sbdst_object_key, g_doc_components type sbdst_components, g_doc_signature type sbdst_signature. * template url
 data: gt_bds_uris type sbdst_uri, gs_bds_url like line of gt_bds_uris, g_template_url(256) type c. g_classname = 'HRFPM_EXCEL_STANDARD'. g_classtype = 'OT'. g_objectkey = 'DOI_EXCEL_TEST'. form get_template_url. CREATE OBJECT gr_bds_documents. CALL METHOD cl_bds_document_set=>get_info EXPORTING classname = g_classname classtype = g_classtype object_key = g_objectkey CHANGING components = g_doc_components signature = g_doc_signature. CALL METHOD cl_bds_document_set=>get_with_url EXPORTING classname = g_classname classtype = g_classtype object_key = g_objectkey CHANGING uris = gt_bds_uris signature = g_doc_signature. free gr_bds_documents. read table gt_bds_uris into gs_bds_url index 1. g_template_url = gs_bds_url-uri. endform. "get_template_url

 

"经过URL获取模板文档
 call method gr_document->open_document exporting open_inplace = 'X' document_url = g_template_url.
相关文章
相关标签/搜索