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 davidvanr (108 replies)
-
hey developers out there
by pitsophera (0 replies)
-
How can i develop opc server using .net?
by vairajaig (1 replies)
-
High-Performance .NET Application Development & Architecture
by Manjot Bawa (0 replies)
-
write to XML file vb.net
by acnetonline (2 replies)
Related podcasts
-
Vote of No Confidence
In this episode Ward Bell (from IdeaBlade) and Jeremy Miller discuss the Vote of No Confidence on Microsoft's Entity Framework and what it means for the Alt.NET and .NET communities.This episode does not have a sponsor, so if you are interested in sponsoring the podcast please contact us.Ward and...
Events coming up
-
Dec
9
GL.net Group Meeting - December 2009
Gloucester, United Kingdom
The beginning of this year holiday season will belong to mocks. Ronnie and Stephen will take us for a tour around exciting world of unit testing.
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
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).