`
wuhuizhong
  • 浏览: 681225 次
  • 性别: Icon_minigender_1
  • 来自: 中山
社区版块
存档分类
最新评论

Working with Office 2007 (OOXML) files using PL/SQL

 
阅读更多

Starting with Office 2007, Microsoft switched to an XML-based format called Office Open XML (OOXML).

There has been some debate as to how "open" this format really is, given that the specs run to around 7,000 pages (!).

Be that as it may, it's a fact of life that a lot of people use Microsoft's Office suite, and that means we have to deal with this new format in a lot of situations.

The OOXML format is, as it turns out, not so difficult to deal with. The main concept is that an Office document, whether it is a Word document (.docx), Excel spreadsheet (.xlsx) or Powerpoint presentation (.pptx), is actually a compressed (.zip) file that contains a number of XML documents (as well as any image files the user has included in the document).

So to work with OOXML files, we need to be able to zip and unzip files, and to parse and generate XML. Oracle (and PL/SQL) has had good support for XML for a number of years, but (even though there is a UTL_COMPRESS package in the database) there is no built-in zip/unzip support. Of course you could load some Java classes into the database to do it, but dealing with the Java stuff is always a bit of a hassle. But some time ago the good gentleman Anton Scheffer published a PL/SQL implementation based on UTL_COMPRESS that supports zipping and unzipping.

Based on this I have written a package for working with OOXML documents. It's calledOOXML_UTIL_PKG and you can download it as part of (you guessed it) the Alexandria utility library for PL/SQL.

Let's see what this package allows us to do.


Get document properties from a Word (docx) file

First we fire up Word and create a test document:


By the way, you can read and write the new OOXML formats using an older version of Office (as I do in the screenshot above), by downloading the Microsoft Office Compatibility Pack from Microsoft.

After saving the document, we can then extract the document properties using theGET_DOCX_PROPERTIES function, which returns a custom record type called T_DOCX_PROPERTIES.



Extract plain text from a Word (docx) file

Using our test document again, we can extract the plain text of the document using theGET_DOCX_PLAINTEXT function, which returns a CLOB.

This is of course very useful if you want to search and/or index (just) the text of a document, or otherwise work with the content.

Get document properties from an Excel (xlsx) file

Let's first create an Excel test file (again, using Excel 2003 but saving in Excel 2007 format):



(This has to be one of the lamest spreadsheets of all time, but it will do fine as an example. It has some text, some numbers, and a formula.)

Similar to the Word document, we can now use the GET_XLSX_PROPERTIES function, which returns a custom record type called T_XLSX_PROPERTIES.


You'll notice that Word documents and Excel spreadsheets have slightly different properties.

Extract a cell value from an Excel (xlsx) file

The GET_XLSX_CELL_VALUE function allows us to retrieve a single value from a named cell in a specific worksheet, like this:


Technical Detail: In the XLSX format, strings (as opposed to numbers) are not stored in the actual cell where they are entered, but rather in a "shared strings" section. The cell just contains a reference back to this "shared strings" section. The GET_XLSX_CELL_VALUE function handles this for you, so you don't have to worry about that.


Extract multiple cell values from an Excel (xlsx) file

Since the function that extracts a single value from a spreadsheet must open the file, unzip it, and parse the XML content every time you call that function, there is another function (GET_XLSX_CELL_VALUES, notice the plural) that allows you to retrieve multiple values in one call. In other words, the file is unzipped and the contents parsed as XML just once, which is obviously more efficient.

Simply specify the names of multiple cells using an array of strings:



Write contents into OOXML file using PL/SQL

Since the contents of OOXML files are XML files, you can manipulate the existing content, or generate new content, and then save it back to the zip file that contains your document.

The following demonstrates one approach; it uses a Powerpoint file, but this technique will also work with Word and Excel files.

We create a Powerpoint 2007 file (.pptx) and put in some tags that we want to replace via code. In other words, this becomes a template that we can fill with dynamic values from the database.



The GET_FILE_FROM_TEMPLATE function takes a template file as input, and two string arrays: The tag names and actual values to replace the tags with. It unzips the file, performs the substitutions, writes back the file to the zip archive, and returns the file, which you can then save back to disk (or, more likely, store in the database or send to a web browser).

The code is trivial:



Here is the result when opening the output file:



So the next time you do a presentation, you could actually update your Powerpoint slides with the latest sales figures (or whatever) from within SQL*Plus...


Conclusion

Working with Office 2007 (OOXML) files from PL/SQL is easy and opens up many possibilities, both for extracting information from documents and storing them in the database, as well as generating or modifying OOXML files in the database server.

 

http://ora-00001.blogspot.com/2011/02/working-with-office-2007-ooxml-files.html

分享到:
评论

相关推荐

    poi全家桶ooxml-schemas/poi/poi-examples/poi-ooxml/poi-ooxml-schemas/poi-scratchpad

    标题中的"poi全家桶ooxml-schemas/poi/poi-examples/poi-ooxml/poi-ooxml-schemas/poi-scratchpad"提及的是Apache POI项目中的多个关键组件和目录结构。Apache POI是一个开源的Java库,专门用于读写Microsoft Office...

    alexandria-plsql-utils:Oracle PLSQL实用程序库

    亚历山大plsql实用程序 Oracle PL / SQL实用程序库 该库是PL / SQL各种实用程序包的集合,以及指向在其他位置托管和维护... http://ora-00001.blogspot.com/2011/02/working-with-office-2007-ooxml-files.html http

    poi-ooxml/schemas/xmlbeans/om4j/poi-3.8.jar

    poi-ooxml-3.8.jar和poi-ooxml-schemas-3.8.jar和xmlbeans-2.3.0.jar和dom4j-1.6.1.jar和poi-3.8.jar,需要的童鞋可以下载。

    OOXML Strict Converter for Office 2010

    OOXML Strict Converter for Office 2010 allows you to open ISO strict documents that are created using Office 2013 in Office 2010. It will preserve the fidelity of the document. If you make any changes...

    poi-ooxml-4.1.2.jar中文文档.zip

    poi-ooxml-***.jar中文文档.zip,java,poi-ooxml-***.jar,org.apache.poi,poi-ooxml,***,org.apache.poi.ooxml,jar包,Maven,第三方jar包,组件,开源组件,第三方组件,Gradle,apache,poi,ooxml,中文API文档,手册,开发...

    poi-bin-3.0.2-FINAL

    Apache POI 是一个Java库,专门用于处理Microsoft Office格式的文件,如Excel、Word和PowerPoint。"poi-bin-3.0.2-FINAL" 是一个特定版本的Apache POI二进制发行版,它包含了处理Excel文件所需的所有类和资源。这个...

    office2007兼容包

    Office 2007 兼容包是一款专为使用旧版Microsoft Office(如Office 2003)的用户设计的软件,它使得用户能够在不升级到Office 2007或更高版本的情况下,能够打开、编辑和保存采用新格式(如.docx, .xlsx, .pptx等)...

    poi-ooxml-lite-5.2.2.jar

    poi-ooxml-5.2.2.jar

    资源 ooxml-schemas-1.0.jar和ooxml-schemas-2.0

    OOXML(Office Open XML)是微软推出的一种开放文档格式,用于存储Word、Excel、PowerPoint等办公软件的数据。ooxml-schemas-1.0.jar和ooxml-schemas-2.0是与OOXML相关的Java库,它们包含了处理OOXML文件所必需的XML...

    Office 2007文件格式转换器

    Office 2007文件格式转换器是一款专为处理Microsoft Office 2007文档设计的工具,它能够帮助用户将新版本的Office文件转换为更旧版本的格式,或者将其他格式的文档转换为Office 2007支持的格式。在日常工作中,我们...

    Office2003或Office2000如何打开Office2007的文件

    这是因为从Office 2007开始,微软引入了一种新的文件格式,称为Open XML(也称为OOXML),这导致了旧版Office无法识别的新文件扩展名,例如.docx代替了传统的.doc,.xlsx代替.xlsx,以及.pptx代替.ppt。 为了解决这...

    poi-3.9、poi-ooxml-3.9、poi-ooxml-schemas-3.9

    HSSF是用于读写旧的BIFF格式(Excel 97-2007),而XSSF则是用于处理新的OOXML格式(Excel 2007及以后版本)。 2. poi-ooxml-3.9.jar:这个库提供了对Office Open XML (OOXML) 格式的额外支持。OOXML是微软推出的一...

    poi-ooxml-schemas-3.8,poi-3.8,poi-ooxml jar包合集

    OOXML是Microsoft为Office文档定义的一种新的XML标准,它用于Excel 2007及以后版本的文件。此库提供了对这些XML schema的访问,使开发者能够解析和构建符合OOXML规范的Excel文件。 2. **poi-3.8**: 这是Apache POI...

    poi-ooxml-5.2.2.jar中文文档.zip

    poi-ooxml-***.jar中文文档.zip,java,poi-ooxml-***.jar,org.apache.poi,poi-ooxml,***,org.apache.poi.ooxml,jar包,Maven,第三方jar包,组件,开源组件,第三方组件,Gradle,apache,poi,ooxml,中文API文档,手册,开发...

    poi-ooxml3.9.zip

    `poi-ooxml-3.9.jar`是主库文件,包含了所有处理Open XML格式(OOXML)的API,这是Microsoft Office 2007及以后版本所使用的文件格式。开发者可以使用这些API来创建、修改和解析OOXML文件。 标签"poi-ooxml"是这个...

    poi-ooxml-3.15.zip

    这个版本的POI引入了对Office Open XML (OOXML)标准的支持,这是一种XML-based文件格式,广泛用于现代的Microsoft Office应用程序。 Apache POI的核心功能在于提供API,允许程序员在Java环境中创建、修改和读取MS ...

    Office+2003打开Office+2007文件格式的兼容软件包

    标题中的“Office 2003打开Office 2007文件格式的兼容软件包”指的是微软Office在不同版本之间存在文件格式不兼容的问题,尤其是从较旧版本(如Office 2003)尝试打开较新版本(如Office 2007)创建的文档时。...

    poi-ooxml-schemas-3.17.zip 最新版下载

    Apache POI是一个流行的Java库,用于处理Microsoft Office格式的文件,如Word(.docx)、Excel(.xlsx)和PowerPoint(.pptx)。而"poi-ooxml-schemas-3.17.zip"是Apache POI项目的一个组件,包含了Open XML(OOXML...

    poi-ooxml-5.2.3.jar中文文档.zip

    poi-ooxml-***.jar中文文档.zip,java,poi-ooxml-***.jar,org.apache.poi,poi-ooxml,***,org.apache.poi.ooxml,jar包,Maven,第三方jar包,组件,开源组件,第三方组件,Gradle,apache,poi,ooxml,中文API文档,手册,开发...

Global site tag (gtag.js) - Google Analytics