ABAP读取SMW0 Excel模板并写入数据

背景

做了一个批导程序,用于数据导入,为了方便用户使用,做了一个对应的导出程序,两者模板一致,这样用户需要修改数据时,先导出系统原有的,然后在这个基础上修改,再执行导入程序即可。

原理

读取Excel模板,并用开源库abap2xlsx完成编辑。

示例代码

data:lt_mime_data type table of w3mime,
     ls_id        type wwwdataid,
     ls_key       type wwwdatatab.
data:lv_string       type string,
     lv_xstring      type xstring,
     lv_input_length type i.

ls_key-relid ='MI'.
ls_key-objid = 'ZMDGR019'."模板ID
call function 'WWWDATA_IMPORT'
  exporting
    key               = ls_key
  tables
*   HTML              =
    mime              = lt_mime_data
  exceptions
    wrong_object_type = 1
    import_error      = 2
    others            = 3.
if sy-subrc <> 0.
* Implement suitable error handling here
endif.

select single value
from  wwwparams
where  relid       eq @ls_key-relid
and    objid       eq @ls_key-objid
and name eq 'filesize'
into @data(lv_para_value).

lv_input_length = lv_para_value.

call function 'SCMS_BINARY_TO_XSTRING'
  exporting
    input_length = lv_input_length
*   FIRST_LINE   = 0
*   LAST_LINE    = 0
  importing
    buffer       = lv_xstring
  tables
    binary_tab   = lt_mime_data
  exceptions
    failed       = 1
    others       = 2.
if sy-subrc <> 0.
* Implement suitable error handling here
endif.


"lv_xstring = lv_string.
data: reader          type ref to zif_excel_reader,
      lo_excel_writer type ref to zif_excel_writer
      .

create object reader type zcl_excel_reader_2007.
data(lo_excel) = reader->load( i_excel2007 =  lv_xstring ).

data: lv_file      type xstring,
      lv_bytecount type i,
      lt_file_tab  type solix_tab.

data(lo_worksheet) = lo_excel->get_active_worksheet( ).
*  lo_worksheet->set_title( ip_title = 'Sheet1' ).
lo_worksheet->set_cell( ip_column = 'A' ip_row = 4 ip_value = 'Hello world' ).


create object lo_excel_writer type zcl_excel_writer_2007.
lv_file = lo_excel_writer->write_file( lo_excel ).

" Convert to binary
call function 'SCMS_XSTRING_TO_BINARY'
  exporting
    buffer        = lv_file
  importing
    output_length = lv_bytecount
  tables
    binary_tab    = lt_file_tab.

cl_gui_frontend_services=>gui_download(
exporting
  bin_filesize = lv_bytecount
  filename     = 'C:\BP export.xlsx'
  filetype     = 'BIN'
changing
  data_tab     = lt_file_tab ).
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值