UI5与后端的文件交互(四)

发布时间:2024年01月04日


前言

这系列文章详细记录在Fiori应用中如何在前端和后端之间使用文件进行交互。
这篇的主要内容有:

  1. 后端RAP的开发(S4HANA On-Premise)
    • 新建表格及CDS,管理模板文件
    • 新建Function(动态创建Excel文档并返回文件流)
  2. 前端(UI5)
    • 提交请求并下载文档(例:在职证明)

一、后端开发

1. 新建管理模板表格

@EndUserText.label : 'Form Template Table'
@AbapCatalog.enhancement.category : #NOT_EXTENSIBLE
@AbapCatalog.tableCategory : #TRANSPARENT
@AbapCatalog.deliveryClass : #A
@AbapCatalog.dataMaintenance : #ALLOWED
define table ymoon_t000 {
  key client   : abap.clnt not null;
  key uuid     : sysuuid_x16 not null;
  attachment   : abap.string(0);
  attachment_x : abap.rawstring(0);
  filename     : abap.char(200);
  erdat        : erdat;
  erzet        : erzet;

}
  • 附上数据
    在这里插入图片描述

工作模式和之前里的内容是一样的。就是把文件流保存到表格里。 额外一点就是把XString类型的数据也一并保存。这里不再熬述

  • Method实现
  method upload_template.
    "获取UI5传送的parameter

    data(attachment) = keys[ 1 ]-%param-attachment.
    data(filename_full) = keys[ 1 ]-%param-filename.

    "
    split filename_full at `.` into data(lv_filename) data(lv_filetype).

    "拆分数据,只保留excel内容 - lv_data
    split attachment at `;` into data(lv_dummy) data(lv_data).
    split lv_data at `,` into data(lv_format) lv_data.

    data:lv_excel_data type xstring.
    "将base64的String转换为xstring
    call function 'SCMS_BASE64_DECODE_STR'
      exporting
        input  = lv_data
      importing
        output = lv_excel_data
      exceptions
        failed = 1
        others = 2.

    "更新模板文件
    data:ls_t000 type ymoon_t000.

    select single uuid from ymoon_t000 into @data(uuid_00) where filename = @filename_full.

    if uuid_00 is not initial.
      ls_t000-uuid = uuid_00.
    else.
      ls_t000-uuid = cl_system_uuid=>create_uuid_x16_static( ).
    endif.

    ls_t000-attachment = attachment.
    ls_t000-attachment_x = lv_excel_data.
    ls_t000-filename = filename_full.
    ls_t000-erdat = sy-datum.
    ls_t000-erzet = sy-uzeit.

    modify ymoon_t000 from ls_t000.


  endmethod.

2. 新建Function,动态创建文档

  • BEDF 添加Function
static function download_form parameter ymoon_s012 result [1] ymoon_s013;
  • Method实现(使用abap2xlsx插入数据并返回base64值)
  method download_form.



    types: begin of t_sheet1,
             name          type c length 60,
             bdate(200),
             id(20),
             sdate(200),
             position(100),
             salary(20),
             nyear(4),
             nmonth(2),
             nday(2),
           end of t_sheet1.
    data: gs_sheet1 type t_sheet1.
    data: ls_ymoon_s013 type ymoon_s013,
          ls_t000       type ymoon_t000.


    data: lo_excel  type ref to zcl_excel, "??
          lo_reader type ref to zif_excel_reader,
          lo_root   type ref to cx_root. "异常类
    "excel worksheet类对象
    data:lo_worksheet type ref to zcl_excel_worksheet.
    "excel超链接
    data:lo_hyperlink type ref to zcl_excel_hyperlink.
    "style类
    data:lo_style type ref to zcl_excel_style.
    "style的guid
    data:lv_style_guid type zexcel_cell_style.

    data:lo_column    type ref to zcl_excel_column.

    data: lo_data type ref to zcl_excel_template_data.

    data:lv_line  type sy-tabix,
         lv_line2 type sy-tabix,
         lv_line3 type sy-tabix.


    data(uuid) = keys[ 1 ]-%param-uuid.

    "动态获取内容
    select single name from ymoon_t010 into @data(lv_uname) where uuid = @uuid.


    "测试数据
    gs_sheet1-name     = lv_uname.
    gs_sheet1-bdate    = '1990年1月1日'.
    gs_sheet1-id       = '11101019900101XXXX'.
    gs_sheet1-sdate    = '2020年10月20日'.
    gs_sheet1-position = '技术总监'.
    gs_sheet1-salary   = '20,000'.
    gs_sheet1-nyear    = sy-datum+0(4).
    gs_sheet1-nmonth   = sy-datum+4(2).
    gs_sheet1-nday     = sy-datum+6(2).




    select single attachment_x from ymoon_t000 into @data(lv_attachment_x).

    try.
* prepare data
        create object lo_data.
        lo_data->add( iv_sheet = 'Sheet1' iv_data = gs_sheet1 ).

* create reader
        create object lo_reader type zcl_excel_reader_2007.

* load template
        lo_excel = lo_reader->load( i_excel2007 = lv_attachment_x ).

* merge data with template
        lo_excel->fill_template( lo_data ).

        lo_worksheet = lo_excel->get_active_worksheet( ).

*      lo_worksheet->set_cell( ip_column = 'C' ip_row = 4 ip_value = '???' ).
*
        lo_worksheet->calculate_column_widths( ).

*** Create output

        data cl_writer type ref to zif_excel_writer.
        create object cl_writer type zcl_excel_writer_2007.

        data: xdata     type xstring,             " Will be used for sending as email
              t_rawdata type solix_tab,           " Will be used for downloading or open directly
              bytecount type i.                   " Will be used for downloading or open directly
        xdata = cl_writer->write_file( lo_excel ).

        data:lv_base64  type string.
        call function 'SCMS_BASE64_ENCODE_STR'
          exporting
            input  = xdata
          importing
            output = lv_base64.

        ls_ymoon_s013-attachment = lv_base64.
        ls_ymoon_s013-filename = '在职证明-' && lv_uname && '.xlsx'.

        "返回base64
        append value #( %param = ls_ymoon_s013 ) to result .

      catch cx_root into lo_root.

    endtry.

  endmethod.

二、修改UI5项目

1.Table里添加下载证明列

<Button text="下载证明" press="onClickDown" type="Neutral"></Button>

2. 实现onClickDown事件

这里需要注意的是BDEF里定义的function是GET方法

onClickDown: function (e) {
                var that = this
                var oModel = this.getView().getModel();
                var uuid = e.getSource().getBindingContext().getProperty("Uuid");

                oModel.callFunction("/download_form",
                    {
                        method: "GET",
                        urlParameters: {   //参数,首字母大写 
                            "Uuid": uuid
                        },
                        success: function (odata, response) {
                            //Model Refresh
                            // debugger
                            var vContent = response.data.download_form;
                            var file = vContent.Filename.split(".")

                            var blob = that.base64toBlob(vContent.Attachment, 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
                            File.save(blob, file[0], file[1], 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
                        },
                        error: function (res) {
                            console.log(res)
                        }
                    })

            },

三、测试

在这里插入图片描述

四、附

  • 此外,如果是OP的话,可以使用smw0管理模板文件。具体load方法可以参考如下代码。
//io_reader  type ref to zif_excel_reader
//ro_excel   type ref to zcl_excel
  ls_key-relid = 'MI'.
  ls_key-objid = iv_w3objid .  //对象名称

" 读取SMW0文件
  call function 'WWWDATA_IMPORT'
    exporting
      key    = ls_key
    tables
      mime   = lt_mime
    exceptions
      others = 1.
  if sy-subrc <> 0.
    lv_errormessage = 'A problem occured when reading the MIME object'(004).
    zcx_excel=>raise_text( lv_errormessage ).
  endif.

" 读取 filesize 
  call function 'WWWPARAMS_READ'
    exporting
      relid = ls_key-relid
      objid = ls_key-objid
      name  = 'filesize'
    importing
      value = lv_filesizec.

* XSTRING转换
  lv_filesize = lv_filesizec.
  call function 'SCMS_BINARY_TO_XSTRING'
    exporting
      input_length = lv_filesize
    importing
      buffer       = lv_excel_data
    tables
      binary_tab   = lt_mime
    exceptions
      failed       = 1
      others       = 2.

*--------------------------------------------------------------------*
* Parse Excel data into ZCL_EXCEL object from binary string
*--------------------------------------------------------------------*
  ro_excel = io_reader->load( i_excel2007 = lv_excel_data ).
文章来源:https://blog.csdn.net/pely122/article/details/135384686
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。