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.
相关推荐
2601_963771371 小时前
Hardening Enterprise WordPress Sites Against REST API Leaks and Bad Headers
前端·windows·word·php
在海边看星星6 小时前
SAP 邮编管理
sap
_清风来叙1 天前
【软件】Word软件不支持显示emoji表情包如何解决
电脑·word
CallmeFoureyes1 天前
使用 C# 提取 Word 文档中的表格数据
开发语言·c#·word
AI刀刀2 天前
豆包智能体对话导出后,如何构建长期归档与高效检索体系?
android·人工智能·word·excel·ai导出鸭
Jazz_z2 天前
通过 Java 实现 Word 转 TXT
java·c#·word
小弥儿2 天前
无需安装WPS 和 Office!开源 OfficeCLI,Claude/Copilot 可直接调用,自动生成编辑Word/PPT/Excel
word·powerpoint·excel
AI导出鸭PC端3 天前
豆包智能体停运在即,历史对话如何一键导出为 Word 或 PDF?
人工智能·pdf·word·豆包·ai导出鸭
asdzx673 天前
C# 通过模板生成 Word 文档:文本与图片占位符替换完整攻略
开发语言·c#·word
Metaphor6924 天前
使用 Python 在 Word 文档中添加批注
python·word