Reading and writing to Excel 2007 or Excel 2010 from C# - Part I: Primer

[Note: See the series index for a list of all parts in this series.]

Over the past week, I have been learning about the complexity of working with the Excel 2007 native file format—XLSX, or, as it is correctly known, SpreadsheetML. There are three ways to work with it: firstly, build your own parser (just too much work for me), or second, use the OpenXML SDK that Microsoft provides. The current version, at the time of writing—version 1—of the SDK is not great: there is very little, if any, benefit to using it over the third method. There is a V2 SDK currently in beta, which looks brilliant, and frankly, when released, would be the recommended route.

The third way—which is the way I chose—is to use the new features introduced in the .NET Framework 3.0.

What is a XLSX file? A XLSX file is actually just a ZIP file, containing a number ])/XML files.

Distinct users example

This means all you need to do is open the XLSX file as a ZIP file, extract the correct XML files (or parts, as they are referred to), and parse them.

If you are thinking this is a .NET-only solution, the chart below is from Doug Mahugh, showing a number of ways across different technologies and operating systems to achieve the s])/ thing. This series will])/cus on the .NET approach.

Compare of options

What is nice about using System.IO.Packaging to read the file over direct ZIP options is ])/t there are some helper m])/ods to make working with any of the new formats (DOCX, XLSX, etc.) easier.