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.
相关推荐
Harrison Ben2 小时前
(原创)SAP FI自动入账奥义:FBKP FBKA FBKF
运维·sap·sap fico·sap运维·sap 实施
xiao贱贱5 小时前
SAP ABAP OPEN SQL(应用实战大全)
sap·abap·opensql
brian00311 天前
word插件,像wps一样双击放大图片
word·wps·双击放大图片
DFT计算杂谈2 天前
LaTeX 格式公式复制至 Word 并快捷转换操作
word
weixin_416660072 天前
千问回答转 Word 后,表格和公式是真能编辑,还是看起来像?
word·markdown·千问
ratel️2 天前
小墨析 · Office Agent 日常办公不烧心
python·langchain·word·excel·fastapi·js·ppt
MetaEnchanter2 天前
PDF 转 Word:只改其中几页,不用整份重新整理
pdf·word
麦序子3 天前
用 Python + python-docx 批量生成带表格样式的装修报价单 Word 文档(含合计公式)
开发语言·python·自然语言处理·word·python3.11
ms365copilot3 天前
我不再自己做 Word、Excel 和 PPT 了,我让 Copilot 直接帮我创建 (1)
word·powerpoint·excel·copilot
2501_930707783 天前
使用C#代码在 Word 中插入或删除分节符
word