ABAP打印WORD的解决方案

客户要求按照固定格式输出到WORD模板中,目前OLE和DOI研究了均不太适合用于这种需求。

cl_docx_document类可以将WORD转化为XML文件,利用替换字符串方法将文档内容进行填充同

时不破坏WORD现有格式。

  首先需要将WORD的单元格用各种预定义的字符进行填充,为后续替换作准备。

  CALL METHOD cl_gui_frontend_services=>gui_upload
    EXPORTING
      filename   = pv_file
      filetype   = 'BIN'
    IMPORTING
      filelength = lv_length
    CHANGING
      data_tab   = lt_data_tab.

  CALL FUNCTION 'SCMS_BINARY_TO_XSTRING'
    EXPORTING
      input_length = lv_length
    IMPORTING
      buffer       = lv_docx
    TABLES
      binary_tab   = lt_data_tab.

  "获取WORD
  CALL METHOD cl_docx_document=>load_document
    EXPORTING
      iv_data = lv_docx
    RECEIVING
      rr_doc  = lr_docx.

* Get main part where content of word document is stored
  lr_main = lr_docx->get_maindocumentpart( ).

* Get data (XSTRING) of main part
  lv_xml = lr_main->get_data( ).

* Convert to string for simple maintaining
  CALL FUNCTION 'CRM_IC_XML_XSTRING2STRING'
    EXPORTING
      inxstring = lv_xml
    IMPORTING
      outstring = lv_string.

  DEFINE replace_word.
    CLEAR: LR_ERROR,L_RETCODE,L_WORD.
    REPLACE ALL OCCURRENCES OF &2 IN lv_string WITH &1.
  END-OF-DEFINITION.

  "调用宏替换
  replace_word gs_base-ename    'B01'.  "姓名
  replace_word gs_base-gesch    'B02'.  "性别
  replace_word gs_base-pernr    'B03'.  "ERP系统编码
  replace_word gs_base-zhr_mz   'B04'.  "民族
  replace_word gs_base-wtfld    'B05'.  "血型
  replace_word ''               'B06'.  "EHR系统编码
  replace_word gs_base-zhr_jg   'B07'.  "籍贯
  replace_word gs_base-zhr_whcd 'B08'.  "文化程序
  replace_word gs_base-usrid    'B09'.  "电话
  "  replace_word gs_base-usrid    'B10'.  "政治面貌

 替换的宏

  DEFINE replace_word.
    CLEAR: LR_ERROR,L_RETCODE,L_WORD.
    REPLACE ALL OCCURRENCES OF &2 IN lv_string WITH &1.
  END-OF-DEFINITION.

相关推荐

  1. 打印HTML页面,表格元素被截断解决方案

    2024-07-22 16:14:16       48 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-22 16:14:16       52 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-22 16:14:16       54 阅读
  3. 在Django里面运行非项目文件

    2024-07-22 16:14:16       45 阅读
  4. Python语言-面向对象

    2024-07-22 16:14:16       55 阅读

热门阅读

  1. 20240722-【抽象类和接口的区别】

    2024-07-22 16:14:16       20 阅读
  2. vue中怎么改变状态值?

    2024-07-22 16:14:16       17 阅读
  3. Go语言 流程控制和循环语句

    2024-07-22 16:14:16       21 阅读
  4. Python笔记

    2024-07-22 16:14:16       15 阅读
  5. 配置阿里云

    2024-07-22 16:14:16       15 阅读
  6. 微软Edge浏览器全解析

    2024-07-22 16:14:16       16 阅读