使用ABAP操作office Word文档

简介: 使用ABAP操作office Word文档

There is a useful class CL_DOCX_DOCUMENT provided by SAP which could support read and write access to a word document with file extension “.docx”.


This document gives a brief introduction about its usage and could be used as a starting point to build your own application which needs to manipulate word document via ABAP.


Office OpenXML

Starting with Microsoft Office2007, when you create a new word document, you will get a file with “.docx” file extension by default which follows the Office openXML format. You can find its detailed definition from wiki.


For example, I create a very simple word document which contains a header area, a paragraph with three lines as body, and a picture.


image.png


According to Office OpenXML protocal, after you change the file extension from “.docx” to “.zip”, its icon changes to an archive file and thus could be opened via winrar. All information about my sample document are spreaded inside a series of xml files in the archive file ( plus media file like picture, music and video if the word document has such one).


The most efficient way to study is create a word document by yourself, change extension to zip and explore it.


image.png


Using CL_DOCX_DOCUMENT to read word document

I use the following sample code to explain how to use this class.

In order to avoid unnecessary local variable declaration, I use the new feature “inline declaration” available in release 740. If this version is not available for you, just replace them with old manual declaration for local variable.

image.png

Comments

(1) you can get a instance of word document via methodcl_docx_document=>load_document. It is necessary to pass the document binary data with type xstring into this method. I don’t list source code of subroutine get_doc_binary as it is not relevant. Just find it from attachment.


(2) The system administrative data like author, creation and last modification date are stored in so called “Core property part”, which could be fetched via document instance got in step1. Once you own the instance of Core property part, you can get its binary data via method get_data().


The returned data has xml format( so does all the left other kinds of parts in this document ) so it could be easily parsed via DOM or SAX parser.


image.png


(3) from document instance we can get main part instance. Its binary data includes all the three body line texts with their font color:



image.png


(4) The binary data of all pictures embedded in the word document could be retrieved via two steps. Firstly get the image part collection from main part instance and then loop each image part instance from the image collection. The get_part method accepts the index starting from 0. The way to read header block information is exactly the same.


Using CL_DOCX_DOCUMENT to change word document

See the nice document How to – Add Custom XML Parts to Microsoft Word using ABAP from Leon Limson.


You could also achieve the same requirement with the respective class below.


image.png


Further reading

If you would like to know how a word template is merged with data from xml file ( for example a response file from web service ), you can find technical detail in my blog Understand how the word template is merged with xml data stream.


相关文章
|
2月前
|
Web App开发 JavaScript 前端开发
2024年纯前端VUE在线编辑微软Office/金山WPS的Word/Excel文档
现在,随着数字化进程渗透到到各行各业,数据安全已经成为了数字化革命中的重要组成部分,而在线Office成在OA、ERP、文档系统中得到了广泛的应用,为我国的信息化事业也做出了巨大贡献。随着操作系统、浏览器及Office软件的不断升级和更新换代,加上国家对信息化、数字化系统要求的不断提升,一些厂家的WebOffice控件产品不断被淘汰出局,而现存的几个产品也存在以下几个问题:
440 1
2024年纯前端VUE在线编辑微软Office/金山WPS的Word/Excel文档
|
3月前
|
XML 数据格式
使用 ABAP 代码将 Word 文档设置成只读
使用 ABAP 代码将 Word 文档设置成只读
22 0
|
4月前
|
前端开发
招投标系统是Electron的纯内网编辑Office Word,可以设置部分区域可编辑,其他的地方不能编辑吗?
我们是招投标系统的开发公司,框架是用的Electron,需要在纯内网的环境下编辑Office Word,可以设置部分区域可编辑,其他的地方不能编辑吗(如下红框位置)?并且在用户忘记填写一些区域的时候做提醒。
52 1
|
5月前
|
程序员 数据安全/隐私保护 Python
Python终于可以操作Office了
Python终于可以操作Office了
Python终于可以操作Office了
|
5月前
|
安全
猿大师办公助手网页在线安全浏览 Office Word 文档,只读打开 / 禁止编辑 / 禁止复制 / 禁止另存 / 禁止打印 / 禁止截屏
在企业 OA 系统或者在线协作办公场景中,有一些合同公文或者客户数据等重要文档需要我们在线共享给其他人,但是我们只希望其他人只能预览阅读文档,不能随便编辑修改文档,也禁止复制共享 Word 文档的内容到其他文档或者编辑器,不能将共享文件另存为本地文件夹,并且禁止用户打印该 Word 文档,那么该如何实现呢?
78 0
|
5月前
|
JSON 安全 数据安全/隐私保护
WebOffice 网页版在线 Office 的 Word 文档权限控制,限制编辑,只读、修订模式、禁止复制等
在一些在线 Office 文档中,有很多重要的文件需要保密控制,比如:报价单、客户资料等数据,只能给公司成员查看,但是不能编辑,并且不能拷贝,防止重要资料外泄。可以通过猿大师办公助手的在线 Office 的文档编辑权限来解决这些问题!
92 1
|
5月前
网页编辑Office Word文档,开启修订功能,启用留痕、显示留痕并接受留痕
在日常办公环境场景下,有时候会遇到帮助他人修改文档或者为文档提供修改意见,如果我们在文档中直接修改,其他人很不容易看到我们修改了哪个部分,如果一旦你的修改意见不被采纳,原作者还需要恢复原来的文档,这样为别人带来了更多的工作。 如果用猿大师办公助手在网页中编辑Office Word文档,开启修订功能,启用留痕、显示留痕并接受留痕,就可以很好的来解决此问题。
361 2
|
7月前
|
数据库
SAP ABAP 系统支持的锁操作类型和各自使用场景的详细讲解试读版
SAP ABAP 系统支持的锁操作类型和各自使用场景的详细讲解试读版
37 0
|
7月前
|
XML 存储 Java
使用 ABAP 调用 Adobe Document Service 生成 PDF 文档
使用 ABAP 调用 Adobe Document Service 生成 PDF 文档
58 0
|
1月前
|
前端开发 开发工具 Android开发
小技巧分享 - 使用 Visual Studio Code 查看和修改 ABAP 代码试读版
小技巧分享 - 使用 Visual Studio Code 查看和修改 ABAP 代码试读版
15 0
小技巧分享 - 使用 Visual Studio Code 查看和修改 ABAP 代码试读版

热门文章

最新文章