Library tutorials & articles
Read and write Open XML files (MS Office 2007)
- Introduction
- Excel 2007 Open XML Specifics
- Implementation
Introduction
Introduction
With Office 2007, Microsoft decided to change default application formats from old, proprietary, closed formats (DOC, XLS, PPT) to new, open and standardized XML formats (DOCX, XLSX and PPTX). New formats share some similarities with old Office XML formats (WordML, SpreadsheetML) and some similarities with competing OpenOffice.org OpenDocument formats, but there are many differences. Since new formats will be default in Office 2007 and Microsoft Office is the most predominant office suite, these formats are destined to be popular and you will probably have to deal with them sooner or later.
This article will explain basics of Open XML file format and specifically XLSX format, the new format for Excel 2007. Presented is a demo application which writes / reads tabular data to / from XLSX files. Application is written in C# using Visual Studio 2005. Created XLSX files can be opened using Excel 2007 Beta (we used build 12.0.3820.1003).
Microsoft Open XML format
Every Open XML file is essentially a ZIP archive containing many other files. Office-specific data is stored in multiple XML files inside that archive. This is in direct contrast with old WordML and SpreadsheetML formats which were single, non-compressed XML files. Although more complex, new approach offers few benefits:
- You don’t need to process entire file in order to extract specific data.
- Images and multimedia are now encoded in native format, not as text streams.
- Files are smaller as a result of compression and native multimedia storage.
Picture 1: Parts and relations inside XLSX file.
To cut a long story short, in order to read the data from an Open XML file you need to:
1)
Open package as a ZIP archive
– any standard ZIP library will do.
2)
Find parts that contain data you want to read
– you can navigate through relationship graph (more complex) or you can presume that certain parts have defined name and path (Microsoft can change that in the future).
3)
Read parts you are interested in
– using standard XML library (if they are XML) or some other method (if they are images, sounds or of some other type).
On the other side, if you want to create a new Open XML file, you need to:
1)
Create/get all necessary parts
– by using some standard XML library (if they are XML), by copying them or by using some other method.
2)
Create all relationships
– create “.rels” files.
3)
Create content types
– create “[Content_Types].xml” file.
4)
Package everything into a ZIP file with appropriate extension (DOCX, XLSX or PPTX)
– any standard ZIP library will do.
The whole story about packages, parts, content types and relations is the same for all Open XML documents (regardless of they originating application) and Microsoft refers to it as Open Packaging Conventions.
Related articles
Related discussion
-
Creating a Windows Service in VB.NET
by codet (102 replies)
-
Compatibility Issue on Firefox to display on Cursor Location
by ansari.wajid (0 replies)
-
An Introduction to VB.NET and Database Programming
by yen (12 replies)
-
Create a Site Search Engine in ASP.NET
by Soundguy53 (64 replies)
-
Changing the Attribute value of an xml node in VB.NET
by cra_tek (0 replies)
Related podcasts
-
Looking into the C# Crystal Ball with Charlie Calvert and Bill Wagner
One of the most exciting announcements from PDC was the news about C# 4.0 and Visual Studio 2010. With all the excitement and discussion throughout the event about these new developer tools, we reached out to two experts in the fields. Charlie Calvert and Bill Wagner sat down with Keith and Woody...
Events coming up
-
Jun
16
Code Generation 2009
Cambridge, United Kingdom
A developer event with a practical focus on helping people get to grips with code generation tools and technologies.
Problem to create a MS Excel or Open office Calc file............
Both S Excel or Open office Calc are using the open xml file format. So we can unzip that archive and read the content including there. But the problem is creating MS Excel or Open office Calc file. I have the all files which want to contain inside the file. But how to create a suitable archive file. Can we use any stranded Zip library such as WinZip, WinRar, 7Zip, etc.
How to create a single archive file (MS Excel or Open office) by using xml files.
(Bundle the xml file is only the problem)
Not a bad post. I was looking for a way to create Excel 2007 files from code and came accross the article. However, seeing how much work was involved - I thought it would be much easier to create an Excel 2003 file and let my Office 2007 using colleagues open the file in compatibility mode.
Fortunately, I came accross another article, which explains how to use the OfficeOpenXml package provided by Microsoft. Using this namespace is a breeze!
For more info - check here: http://www.codeproject.com/office/ExcelPackage.asp
Hallo I just installed Office 2007 and wanted to find out, how the xlsx-Format works. First I just unzipped an Excel file and tried to make sense of the structure on my own. But I failed. Your article helped my to understand the key concept behind the file structure.
Thank you very much
At work I've been tasked with opening an xlsx file and extracting the information.
Many thanks for this article which has made understanding the new file format easier.
Excellent article. It will save me a least a couple of days of digging this information out from msdn.
This thread is for discussions of Read and write Open XML files (MS Office 2007).