sap excel 操作的基类

这是一个关于如何使用SAP进行Excel操作的代码示例,包括打开、读取、写入、设置单元格格式、合并单元格、调整列宽等操作。通过创建OLE对象并调用相关方法实现自动化处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

*----------------------------------------------------------------------*
*   INCLUDE Z_EXCEL                                                    *
*----------------------------------------------------------------------*
data: w_xl     type ole2_object,
      w_cl     type ole2_object,
      w_wkbook type ole2_object,
      w_ws     type ole2_object.

data: w_success type c.

data: columnsobj type ole2_object,   " Columns object
      rowsobj   type ole2_object,   " Range object
      bordersobj   type ole2_object,   " Borders object
      entirecolumnobj type ole2_object,
      fontobj type ole2_object.   " Font object.

*&---------------------------------------------------------------------*
*&      Form  open_excel
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_P_FILE  text
*----------------------------------------------------------------------*
form open_excel using  p_file.
  create object w_xl 'Excel.Application'.
  if sy-subrc ne 0.
    write: / 'Excel init failed, return code is', sy-subrc.
    exit.
  else.
    set property of w_xl 'Visible' = 0.
    call method of w_xl 'Workbooks' = w_wkbook.
    call method of w_wkbook 'Open' = w_wkbook
                   exporting #1 = p_file.
    call method of w_wkbook 'Worksheets' = w_ws
                   exporting #1 = 'Sheet1'.
    call method of w_ws 'Activate'.
  endif.
endform.                    " open_excel
*&---------------------------------------------------------------------*
*&      Form  GET_CELL
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_W_ROW  text
*      -->P_2      text
*      -->P_I_BASEDATA_ORGEN2  text
*----------------------------------------------------------------------*
form get_cell using   p_row
                      p_column
           changing   p_record.
  call method of w_ws 'cells' = w_cl
                   exporting #1 = p_row #2 = p_column.
  get property of w_cl 'value' = p_record.
endform.                    " GET_CELL
*&---------------------------------------------------------------------*
*&      Form  GET_CELL
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_W_ROW  text
*      -->P_2      text
*      -->P_I_BASEDATA_ORGEN2  text
*----------------------------------------------------------------------*
form get_cellt using   p_row
                      p_column
           changing   p_record.
  call method of w_ws 'cells' = w_cl
                   exporting #1 = p_row #2 = p_column.
  get property of w_cl 'text' = p_record.
endform.                    " GET_CELL

*&---------------------------------------------------------------------*
*&      Form  CLOSE_EXCEL
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form close_excel.
  call method of w_wkbook 'Save'.
  call method of w_wkbook 'Close' exporting #1 = 0.
  call method of w_xl 'Quit'.
  free object w_cl.
  free object w_ws.
  free object w_wkbook.
  free object w_xl.
endform.                    " CLOSE_EXCEL
*&---------------------------------------------------------------------*
*&      Form  SET_CELL
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_W_ROW  text
*      -->P_1      text
*      -->P_W_XUHAO  text
*----------------------------------------------------------------------*
form set_cell using p_row
                    p_colum
                    p_value.
  call method of w_ws 'cells' = w_cl exporting #1 = p_row #2 = p_colum.
  set property of w_cl 'value' = p_value.
endform.                    " SET_CELL
*&---------------------------------------------------------------*
*&      Form  FILL_XLS
*&---------------------------------------------------------------*
* 将数据放入EXCEL的单元格中并设置属性
*      -->P_ROW  行
*      -->P_COLUMN  列
*      -->P_DATUM  数据
*----------------------------------------------------------------*
form fill_xls using value(p_row) value(p_column) value(p_datum)
                    value(p_color) value(p_size) value(p_bold)
                    value(is_number).
  if is_number = 1
*  &n

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值