1.创建模板
通过开发工具->空间->格式文本为word添加变量
选中要设为变量的文本,点击格式文本,然后在属性页签设置变量名
模板使用示例参考ZABAPDOCX包下的模板
2. 代码生成
参考ZABAPDOCX包下示例程序,可直接执行下载word文档
如果需要做格式转换,建议借助OLE或者其它工具进行格式转换
如下示例为OLE方式转换为PDF文件
DATA: lv_wordapp TYPE ole2_object,
lv_worddoc TYPE ole2_object,
lv_wordadoc TYPE ole2_object,
lv_wordcont TYPE c LENGTH 8,
lv_path TYPE string VALUE 'C:\Users\X\Desktop\DOXC.docx',
lv_path1 TYPE string VALUE 'C:\Users\X\Desktop\DOXC.pdf'.
CREATE OBJECT lv_wordapp 'WORD.APPLICATION'.
PERFORM errorhandler.
SET PROPERTY OF lv_wordapp 'Visible' = 0.
CALL METHOD OF lv_wordapp 'Documents' = lv_worddoc.
PERFORM errorhandler.
CALL METHOD OF lv_worddoc 'Open'
EXPORTING
#1 = lv_path.
PERFORM errorhandler.
CALL METHOD OF lv_wordapp 'ActiveDocument' = lv_wordadoc.
PERFORM errorhandler.
CALL METHOD OF lv_wordadoc 'Content' = lv_wordcont.
PERFORM errorhandler.
CALL METHOD OF lv_wordadoc 'SaveAs'
EXPORTING
#1 = lv_path1
#2 = 17.
PERFORM errorhandler.
CALL METHOD OF lv_wordapp 'Quit'.
FREE OBJECT lv_wordapp .
FORM errorhandler.
IF sy-subrc <> 0.
WRITE: / 'OLE- Error:', sy-subrc.
CALL METHOD OF lv_wordapp 'Quit'.
FREE OBJECT lv_wordapp.
STOP.
ENDIF.
ENDFORM.
3. 适用场景
需要下载word文档以供打印签字或者查看
简单文件的输出,对于下载后不需要数据处理的文件来说,更优于excel文件的生成
附ZABAPDOCX包地址:
GGitHub - AntonSikidin/zcl_docx: Best way to create Microsoft Word docx from abap