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.
相关推荐
教练、我想打篮球2 小时前
127 apache poi3.11 写 word 中内嵌 表格换行的输出
word·excel·docx·换行
SiYuanFeng14 小时前
【Word 排版】文本框/文字行间距异常偏大的解决方案
word
belldeep18 小时前
python:markdown + python-docx 将 Markdown 文件格式转为 Word 文档
python·word·markdown·docx
爱喝水的鱼丶19 小时前
SAP-ABAP:在SAP世界里与特殊字符“斗智斗勇”:一份来自实战的避坑指南
运维·服务器·数据库·学习·sap·abap·特殊字符
gc_229920 小时前
学习C#调用OpenXml操作word文档的基本用法(16:学习文档脚注类)
word·openxml·脚注
Fighting_p1 天前
【预览word文档】使用插件 docx-preview 预览线上 word 文档
开发语言·c#·word
淋了一场太阳雨1 天前
SAP VOFM Material determination
sap·abap
薛定谔的猫-菜鸟程序员1 天前
基于Node.js+Pandoc实现Markdown文件无损转换为Word文档的小工具
node.js·word·vim
森森-曦1 天前
在word中如何设置从第二页开始页码编号
word
爱学习 爱分享2 天前
word中批量替换
word