Click here to Skip to main content
15,905,914 members
Articles / Productivity Apps and Services / Microsoft Office

How to Read and Write ODF/ODS Files (OpenDocument Spreadsheets)

Rate me:
Please Sign up or sign in to vote.
5.00/5 (120 votes)
28 Jul 2011CPOL6 min read 216K   7.5K   166   16
This article will explain the basics of ODF format, and specifically its implementation in spreadsheet applications ( Calc and Microsoft Office Excel 2007 SP2). Presented is a demo application which writes/reads tabular data to/from .ods files.


The OpenDocument Format (ODF) is an XML-based file format for representing electronic documents such as spreadsheets, charts, presentations and word processing documents. The standard was developed by the OASIS (Organization for the Advancement of Structured Information Standards), and it is a free and open format.

The OpenDocument format is used in free software and in proprietary software. Originally, the format was implemented by the office suite and, with Office 2007 SP2, Microsoft also supports ODF subset.

This article will explain the basics of ODF format, and specifically its implementation in spreadsheet applications ( Calc and Microsoft Office Excel 2007 SP2). Presented is a demo application which writes/reads tabular data to/from .ods files. The application is written in C# using Visual Studio 2010. Created .ods files can be opened using Excel 2007 SP2 or greater and Calc.

ODF Format

OpenDocument format supports document representation:

  • As a single XML document
  • As a collection of several subdocuments within a package

Office applications use the second approach, so we will explain in detail.

Every ODF file is a collection of several subdocuments within a package (ZIP file), each of which stores part of the complete document. Each subdocument stores a particular aspect of the document. For example, one subdocument contains the style information and another subdocument contains the content of the document.

This approach has the following benefits:

  • You don't need to process the 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.

There are four subdocuments in the package that contain file's data:

  • content.xml - Document content and automatic styles used in the content
  • styles.xml - Styles used in the document content and automatic styles used in the styles themselves
  • meta.xml - Document meta information, such as the author or the time of the last save action
  • settings.xml - Application-specific settings, such as the window size or printer information

Besides them, in the package, there can be many other subdocuments like document thumbnail, images, etc.

In order to read the data from an ODF file, you need to:

  1. Open package as a ZIP archive
  2. Find parts that contain data you want to read
  3. Read parts you are interested in

On the other side, if you want to create a new ODF file, you need to:

  1. Create/get all necessary parts
  2. Package everything into a ZIP file with appropriate extension

Spreadsheet Documents

Spreadsheet document files are the subset of ODF files. Spreadsheet files have .ods file extensions.

The content (sheets) is stored in content.xml subdocument.


Picture 1: content.xml subdocument.

As we can see in Picture 1, sheets are stored as XML elements. They contain column and row definitions, rows contain cells and so on... In the picture is data from one specific document, but from this we can see the basic structure of content.xml file (you can also download the full ODF specification).


Our demo is Windows Presentation Foundation application (picture 2) written in C# using Visual Studio 2010.


Picture 2: Demo application.

The application can:

  • create a new Spreadsheet document.
  • read an existing Spreadsheet document.
  • write a created Spreadsheet document.

Creating New Document and Underlying Model of Application

Internally, spreadsheet document is stored as DataSet. Each sheet is represented with DataTable, sheet's row with DataRow, and sheet's column with DataColumn. So, to create a new document, we have to create a new DataSet, with DataTables. Each DataTable has a number of rows and columns that conforms to our needs.

To show data from our DataSet (and to allow editing that data) the application dynamically creates tabs with DataGridViews (that are connected to our DataTables).

Through the interface, a user can read, write, edit data and add new rows to the Spreadsheet document.

As application, basically, transforms Spreadsheet document to / from DataSet, it can also be used as a reference for Excel to DataSet export / import scenarios.

Zip Component and XML Parser

Although classes from System.IO.Packaging namespace (.NET 3.0) provide a way to read and write ZIP files, they require a different format of ZIP file. Because of that, our demo uses the open source component called DotNetZip.

Using ZIP component we can extract files, get subdocument, replace (or add) subdocuments that we want and save that file as .ods file (which is a ZIP file).

For processing documents, we have used XmlDocument because it offers an easy way to reach parts that we want. Note that, if performance is crucial for you, you should use XmlTextReader and XmlTextWriter. That solution needs more work (and code), but provides better performance.

Reading Spreadsheet Document

To read a document, we follow these steps:

  1. Extract .ods file
  2. Get content.xml file (which contains sheets data)
  3. Create XmlDocument object from content.xml file
  4. Create DataSet (that represent Spreadsheet file)
  5. With XmlDocument, we select "table:table" elements, and then we create adequate DataTables
  6. We parse children of "table:table" element and fill DataTables with those data
  7. At the end, we return DataSet and show it in the application's interface

Although ODF specification provides a way to specify default row, column and cell style, implementations have nasty practice (that specially apply for Excel) that they rather write sheet as sheet with maximum number of columns and maximum number of rows, and then they write all cells with their style. So you could see that your sheet has more than 1000 columns (1024 in Calc and 16384 in Excel), and even more rows (and each rows contains the number of cells that are equal to the number of columns), although you only have to write data to the first few rows/columns.

ODF specification provides a way that you specify some element (like column/row/cell) and then you specify the number of times it repeats. So the above behavior doesn't affect the size of the file, but that complicates our implementation.

Because of that, we can't just read the number of columns and add an equal number of DataColumns to DataTable (because of performance issues). In this implementation, we rather read cells and, if they have data, we first create rows/columns they belong to, and then we add those cells to the DataTable. So, at the end, we allocate only space that we need to.

Writing Spreadsheet Document

To write a document, we follow these steps:

  1. Extract template.ods file (.ods file that we use as template)
  2. Get content.xml file
  3. Create XmlDocument object from content.xml file
  4. Erase all "table:table" elements from the content.xml file
  5. Read data from our DataSet and composing adequate "table:table" elements
  6. Add "table:table" elements to content.xml file
  7. Zip that file as new .ods file.

In this application, as template, we have to use an empty document. But the application can be easily modified to use some other template (so that you have preserved styles, etc.).

Download Links

You can download the latest version of the demo application (together with the C# source code) from here.


  • 24th July, 2009: Initial post
  • 28th July, 2011: Project converted from Visual Studio 2008 Windows Forms project to Visual Studio 2010 WPF project


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Software Developer (Senior) GemBox Ltd
United Kingdom United Kingdom
Josip Kremenic works as a developer at GemBox Software.
He works on:

  • GemBox.Spreadsheet - a C# / VB.NET Excel component for reading and/or writing XLS, XLSX, CSV, HTML, PDF, XPS and ODS files.
  • GemBox.Document - a C# / VB.NET Word component for reading and/or writing DOCX, DOC, HTML, PDF, XPS, RTF and TXT files.
  • GemBox.Presentation- a C# / VB.NET PowerPoint component for reading and/or writing PPTX, PPT, PDF and XPS files.
  • GemBox.Email - a C# / VB.NET Email component for composing, receiving and sending MSG, EML and MHTML email messages using IMAP, POP and SMTP.
  • GemBox.Pdf - a C# / VB.NET Pdf component for for reading and/or writing PDF files.

Comments and Discussions

Questionout of memory exception Pin
pk_mittal20-Jul-18 3:21
pk_mittal20-Jul-18 3:21 
QuestionSmall correction Pin
Formentz10-Feb-17 4:15
Formentz10-Feb-17 4:15 
QuestionOutOfMemoryException Pin
J.Thomas4-Apr-16 0:24
J.Thomas4-Apr-16 0:24 
GeneralNice Article. Pin
AbhinavAggarwal19-May-14 18:16
AbhinavAggarwal19-May-14 18:16 
QuestionDo you still have the old windows forms version??? Pin
Member 797834613-May-12 0:32
Member 797834613-May-12 0:32 
QuestionStoring formulas in spreadsheet Pin
Bernhard Häussermann27-Mar-12 23:58
Bernhard Häussermann27-Mar-12 23:58 
QuestionProblem reading consecutive cells with same value Pin
lync____20-Dec-11 21:27
lync____20-Dec-11 21:27 
AnswerRe: Problem reading consecutive cells with same value Pin
mcosic11-Jun-12 6:36
mcosic11-Jun-12 6:36 
GeneralRe: Problem reading consecutive cells with same value Pin
lync____11-Jun-12 15:15
lync____11-Jun-12 15:15 
QuestionStoring numbers in spreadsheet Pin
Bernhard Häussermann30-Aug-11 1:54
Bernhard Häussermann30-Aug-11 1:54 
AnswerRe: Storing numbers in spreadsheet Pin
Nyrr19-Jul-13 10:40
Nyrr19-Jul-13 10:40 
AnswerRe: Storing numbers in spreadsheet Pin
Feibih18-Dec-22 0:59
Feibih18-Dec-22 0:59 
QuestionTnks for sample Pin
Daniel Olivares Cuevas7-Jul-11 7:34
Daniel Olivares Cuevas7-Jul-11 7:34 
GeneralStrugling to reading excel spreadsheet Pin
Mongezi16-Sep-09 20:25
Mongezi16-Sep-09 20:25 
Generalthank you Pin
ignatandrei27-Jul-09 17:34
professionalignatandrei27-Jul-09 17:34 
GeneralSome more clarification required Pin
Md. Marufuzzaman24-Jul-09 2:29
professionalMd. Marufuzzaman24-Jul-09 2:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.