Sophie

Sophie

distrib > Fedora > 15 > x86_64 > by-pkgid > 784c7185bb6d0646e8bce92e6f61fb78 > files > 13

freexl-devel-1.0.0a-3.fc15.x86_64.rpm

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<title>FreeXL: About the .xls binary format</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css"/>
</head>
<body onload='searchBox.OnSelectItem(0);'>
<!-- Generated by Doxygen 1.7.4 -->
<script type="text/javascript"><!--
var searchBox = new SearchBox("searchBox", "search",false,'Search');
--></script>
<div id="top">
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td style="padding-left: 0.5em;">
   <div id="projectname">FreeXL&#160;<span id="projectnumber">1.0.0a</span></div>
  </td>
 </tr>
 </tbody>
</table>
</div>
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li class="current"><a href="pages.html"><span>Related&#160;Pages</span></a></li>
      <li><a href="annotated.html"><span>Data&#160;Structures</span></a></li>
      <li><a href="files.html"><span>Files</span></a></li>
      <li><a href="examples.html"><span>Examples</span></a></li>
      <li id="searchli">
        <div id="MSearchBox" class="MSearchBoxInactive">
        <span class="left">
          <img id="MSearchSelect" src="search/mag_sel.png"
               onmouseover="return searchBox.OnSearchSelectShow()"
               onmouseout="return searchBox.OnSearchSelectHide()"
               alt=""/>
          <input type="text" id="MSearchField" value="Search" accesskey="S"
               onfocus="searchBox.OnSearchFieldFocus(true)" 
               onblur="searchBox.OnSearchFieldFocus(false)" 
               onkeyup="searchBox.OnSearchFieldChange(event)"/>
          </span><span class="right">
            <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
          </span>
        </div>
      </li>
    </ul>
  </div>
</div>
<div class="header">
  <div class="headertitle">
<div class="title">About the .xls binary format </div>  </div>
</div>
<div class="contents">
<div class="textblock"><p>What a .xls binary file really is</p>
<p>(Prepare yourself to be continuously surprised by many unexpected revelations ...)</p>
<p>You may already know that there are many different versions of .xls files. Different versions have different capabilities. So we'll start by reviewing the Excel evolutionary history and and we'll introduce some Microsoft jargon because it's central to understanding the underlying operations.</p>
<h2><a class="anchor" id="CFBF"></a>
CFBF</h2>
<p>Unexpected Revelation #1: <em>There is no .xls file format. Its really a common file suffix applied to many different things.</em></p>
<p>Recent Microsoft Office document files are based on a common container layout named CFBF (Compound File Binary Format). This container format is the same for Excel (.xls), Word (.doc_ and PowerPoint (.ppt) amonst other applications. More information:</p>
<ul>
<li><a href="http://en.wikipedia.org/wiki/Compound_File_Binary_Format">http://en.wikipedia.org/wiki/Compound_File_Binary_Format</a></li>
<li><a href="http://msdn.microsoft.com/en-us/library/dd942138%28v=prot.13%29.aspx">http://msdn.microsoft.com/en-us/library/dd942138%28v=prot.13%29.aspx</a></li>
</ul>
<p>Unexpected Revelation #2: <em>CFBF is more of a file system than a file format</em></p>
<p>A CFBF file is divided into many equal-sized blocks named sectors. Such sectors cannot be directly accessed. In order to retrieve sectors in the expected logical order a FAT (File Allocation Table) is allocated within the CFBF file. A CFBF file is internally organized as if it was a raw physical disk. The design is based on Microsoft own FAT file-system as used by MS-DOS and early versions of Windows. The first sector of the CFBF file acts as if it was a kind-of MBR (Master Boot Record) - this first sector provides information about the layout and type of the CFBF file, such as block/sector size and version. A FAT chain allows a reader to re-assemble the sectors in the required logical order. There is a list of free block, and very large files may use a double indirection (DIFAT - Double Indirection FAT). A CFBF file always has at least a root directory: but a complete directory tree can be provided.</p>
<p>A CFBF file can contain many and many distinct independent files. Just to make things a little clearer, Microsoft calls such pseudo-files (I mean: the many fake ones contained within the CFBF real file) <em>streams</em></p>
<p>The practical consequence is that any software tool attempting to access an Excel binary document must first be able to correctly access this CFBF container format.</p>
<h2><a class="anchor" id="BIFF"></a>
BIFF</h2>
<p>Unexpected Revelation #3: <em>An Excel document will contain a stream (pseudo-file) named Workbook in the root directory of the CFBF file (filesystem).</em></p>
<p>The Workbook stream is internally structured accordingly to the BIFF (Binary Interchange File Format) specifications. You can think of the BIFF as the real Excel binary format (following more conventional naming rules). Several BIFF versions were introduced during the years: and there are significant differences between them.</p>
<p>An useful correspondence table relating corresponding Excel and BIFF versions:</p>
<table class="doxtable">
<tr>
<th><b>Excel Version</b>  </th><th><b>Commercial Name</b>  </th><th><b>BIFF Version</b>  </th><th><b>Release Year</b>  </th><th><b>Notes</b>   </th></tr>
<tr>
<td>2.x </td><td>Excel 2.0 </td><td>BIFF2 </td><td>1987 </td><td>Before CFBF. File is the BIFF stream, containing a single worksheet.  </td></tr>
<tr>
<td>3.0 </td><td>Excel 3.0 </td><td>BIFF3 </td><td>1990 </td><td>Before CFBF. File is the BIFF stream, containing a single worksheet.  </td></tr>
<tr>
<td>4.0 </td><td>Excel 4.0 </td><td>BIFF4 </td><td>1992 </td><td>Before CFBF. File is the BIFF stream, containing a single worksheet.  </td></tr>
<tr>
<td>5.0 </td><td>Excel 5.0 </td><td>BIFF5 </td><td>1993 </td><td>Starting with BIFF5, a single Workbook can internally store many individual Worksheets. The BIFF stream is stored in the CFBF file container.  </td></tr>
<tr>
<td>7.0 </td><td>Excel 95 </td><td>BIFF5 </td><td>1995 </td><td></td></tr>
<tr>
<td>8.0 </td><td>Excel 98 </td><td>BIFF8 </td><td>1998 </td><td></td></tr>
<tr>
<td>9.0 </td><td>Excel 2000 </td><td>BIFF8 </td><td>1999 </td><td></td></tr>
<tr>
<td>10.0 </td><td>Excel XP </td><td>BIFF8 </td><td>2001 </td><td></td></tr>
<tr>
<td>11.0 </td><td>Excel 2003 </td><td>BIFF8 </td><td>2003 </td><td></td></tr>
<tr>
<td>12.0 </td><td>Excel 2007 </td><td>BIFF8 </td><td>2007 </td><td>Introduced alternate XML format, which is usually the default for new files.  </td></tr>
<tr>
<td>14.0 </td><td>Excel 2010 </td><td>BIFF8 </td><td>2010 </td><td>XML format is usually the default for new files.  </td></tr>
</table>
<p>Note that FreeXL does not support the new XML format which is a completely different and unrelated format.</p>
<p>Perhaps you are now expecting that BIFF will simply and directly encode your spreadsheet data. Unfortunately, you should have know better given the steps we took to get here...</p>
<p>Unexpected Revelation #4: <em>Any BIFF stream (pseudo-file stored within a CFBF container file) is internally organized as a collection of variable-length records.</em>.</p>
<p>Each record starts with</p>
<ul>
<li>a 16 bit unsigned integer specifying the record type</li>
<li>another 16 bit unsigned integer specifies the record data length (in bytes) excluding the standard type-size prefix.</li>
</ul>
<p>Note that there are many different record types, and the record size / layout may differ for different BIFF versions.</p>
<p>Three record types have an absolutely special meaning:</p>
<ul>
<li>a BOF [Beginning Of File] record marks starting of a different sub-stream.</li>
<li>an EOF [End Of File] record marks ending of current sub-stream.</li>
<li>a CONTINUE record means that the previous record exceeded the maximum size for a record, and the previous record data payload will be spanned on following CONTINUE records for as many CONTINUE records as are required to store the full data size.</li>
</ul>
<p>Unexpected Revelation #5: <em>So a BIFF stream (pseudo-file) isn't really a file - it's more like a collection of individual sub-streams, each one of which is enclosed between BOF / EOF markers.</em></p>
<p>The most recent BIFF8 requires that at least the following internal sub-streams are be defined:</p>
<ul>
<li>the first sub-stream contains workbook level global data and metadata, such as author, password protection, styles, formats, window settings and so on</li>
<li>list of individual worksheets included into the Workbook, where each worksheet is identified by a name and by a type (data Worksheet, Chart, Visual Basic module ... visible, hidden ...), and relative offset position of the corresponding BOF record allows for fast positioning.</li>
<li>any text string is stored here into the SST [Shared String Table], so individual text cells simply refer the corresponding SST entry by index (in all previous BIFF version text strings are directly stored into the appropriate cell).</li>
<li>any subsequent sub-stream represents a single Worksheet, and the most relevant data stored at Worksheet level are dimension (number of valid rows and columns) and any cell value data.</li>
</ul>
<p>We will now see how BIFF encodes individual data types with several further amazing surprises are still to come. Be prepared!</p>
<h2><a class="anchor" id="BIFF"></a>
BIFF</h2>
<p>Leaving aside special values such as images, OLE, COM, Visual Basic related items and so on, the basic data types are supported in BIFF:</p>
<ul>
<li>text strings</li>
<li>numbers (both integers and decimals)</li>
<li>dates, date-times and times</li>
<li>NULL (empty cell)</li>
</ul>
<p>Note that any multi-byte value is stored in BIFF accordingly using Little Endian byte ordering (i.e. leastsignificant byte comes first, most significant byte comes last).</p>
<table class="doxtable">
<tr>
<th><b>BIFF Record Type</b> </th><th><b>BIFF Version</b> </th><th><b>Content Type</b>  </th></tr>
<tr>
<td>INTEGER </td><td>BIFF2 </td><td>16 bit unsigned integer  </td></tr>
<tr>
<td>NUMBER </td><td>BIFF2 BIFF3 BIFF4 BIFF5 BIFF8 </td><td>64 bit floating point (double precision)  </td></tr>
<tr>
<td>RK </td><td>BIFF3 BIFF4 BIFF5 BIFF8 </td><td>number, variant-type: INTEGER FLOAT DATE DATETIME TIME (please see the corresponding detailed description)  </td></tr>
<tr>
<td>MULRK </td><td>BIFF5 BIFF8 </td><td>a variable-sized array of elementary RK values. associated to a range of consecutive cells on the same row   </td></tr>
<tr>
<td>LABEL </td><td>BIFF2 BIFF3 BIFF4 BIFF5 BIFF8 </td><td>text string, variable-length. (please see the corresponding detailed description)  </td></tr>
<tr>
<td>LABELSST </td><td>BIFF8 </td><td>text string, variable-length. based on the global SST [Shared String Table] stored at the workbook level, so a LABELSST simply requires providing the corresponding SST index.  </td></tr>
</table>
<p>So the BIFF record type that is easy to handle is NUMBER, which is essentially a C-style <em>double</em>. Other record types require additional handling.</p>
<h3><a class="anchor" id="RK"></a>
RK values</h3>
<p>An RK value is a 32 bit value.</p>
<p>The least significant two bits are a bit-mask (in little endian order, so the least two significant bits in the first byte that is read):</p>
<ul>
<li>if 0x02 is set the RK value represents a 30 bit signed integer, otherwise it represents a 64 bit floating point double precision number requiring special reconstruction.</li>
<li>if 0x01 is set the corresponding value needs to be divided by 100, so even an integer actually becomes a floating point double precision.</li>
</ul>
<p>When interpreting RK values as a signed integer, right shifting two bits is required: </p>
<div class="fragment"><pre class="fragment"><span class="keywordtype">int</span> <a class="code" href="structFreeXL__CellValue__str.html#acaf7ae03285c299b05f0e345eff3c6a1" title="The value of the data stored in the cell.">value</a> = rk_value &gt;&gt; 2;
</pre></div><p>When interpreting RK values as a 64 bit floating point, two steps are required:</p>
<ul>
<li>the RK value requires appropriate masking: <div class="fragment"><pre class="fragment"><span class="keywordtype">int</span> <a class="code" href="structFreeXL__CellValue__str.html#acaf7ae03285c299b05f0e345eff3c6a1" title="The value of the data stored in the cell.">value</a> = rk_value &amp; 0xfffffffc;
</pre></div></li>
<li>then the 32 bit value will be copied into a 64 bit buffer, and the least significant four bytes need to be initialized as zeroes: 0x00000000.</li>
</ul>
<p>As a final step, if 0x01 was set into the bit-mask, now we have to divide by 100 before returning the effective cell value. So for 32 bit integers: </p>
<div class="fragment"><pre class="fragment"><span class="keywordtype">double</span> final_value = (double)<a class="code" href="structFreeXL__CellValue__str.html#acaf7ae03285c299b05f0e345eff3c6a1" title="The value of the data stored in the cell.">value</a> / 100.0;
</pre></div><p> and for 64 bit floats: </p>
<div class="fragment"><pre class="fragment"><span class="keywordtype">double</span> final_value = <a class="code" href="structFreeXL__CellValue__str.html#acaf7ae03285c299b05f0e345eff3c6a1" title="The value of the data stored in the cell.">value</a> / 100.0;
</pre></div><h3><a class="anchor" id="Text"></a>
Text values</h3>
<p>Any BIFF version from BIFF2 to BIFF5 simply supports CodePage based character encoding, i.e. each character simply requires 8 bits to be represented (single byte). Correct representation of characters requires knowing which one CodePage table has to be applied. This can be determined from the workbook or worksheed metadata (it is the CODEPAGE record).</p>
<p>BIFF8 is much more sophisticated, since any text string is usually encoded as Unicode in UTF-16 Little Endian [UTF-16LE] format. This encoding is a multi-byte encoding (two bytes are required to represent a single character), but being universal no character table is required.</p>
<p>BIFF text strings are never null-terminated. The actual length is always explicitly stated, as an 8 bit unsigned int or as a 16 bit unsigned int (depending on BIFF versions).</p>
<p>FreeXL is intended to be strictly interoperable with SQLite and SpatiaLite, so any text string has to be converted to UTF-8 encoding. GNU libiconv can easily handle any required charset conversion. So we can simply fetch the appropriate bytes, then call iconv() as appropriate, and we'll immediately get back the corresponding UTF-8 encoded text string.</p>
<p>Converting Unicode based text strings is a little more complex, because each Unicode string is prefixed by a mask byte, specifying how the string is encoded:</p>
<ul>
<li>if 0x01 is set, then the string really is 16 bit per character Unicode, otherwise a stripped notation is used instead. Stripped notation means that the characters are actually represented as single bytes, so already have the UTF8 equivalent.</li>
<li>if 0x04 and/or 0x08 are set, than some further variable-length data (providing information on text decoration such as italics, bold, underline) is inserted immediately before and after the text string itself, so we must carefully skip over this extra data so to maintain the right byte alignment.</li>
</ul>
<p>Note that the string length is expressed in characters, not in bytes, so the actual length in bytes is twice the indicated length.</p>
<h3><a class="anchor" id="Dates"></a>
Retrieving Date, DateTime and Time values.</h3>
<p>Dates, DateTimes and Time values are also a little complicated. Any Date is expressed as an Integer (number of days since the conventional reference day):</p>
<ul>
<li>for Windows Excel the reference day (day 0) is 1900, January 1</li>
<li>for Mac Excel the reference day (day 0) is 1904, January 2</li>
</ul>
<p>There is no possible ambiguity, because the DATEMODE metadata record specifies tells which reference day is to be used.</p>
<p>An odd bug affects Excel, which (incorrectly) treats 1900 as a leap year. Therefore, the non-existant 29 February 1900 has to be included in the days calculation so to get the expected Date value.</p>
<p>Any Time is expressed as a Fraction (percent of seconds since midnight). 0.5 corresponds to 12:00:00 (midday), 0.25 corresponds to 06:00:00, 0.75 corresponds to 18:00:00 and so on.</p>
<p>So a DateTime is simply the sum of a Date value and of a Time value. Dates can be represented by Integers: but Times and DateTimes require a floating point number.</p>
<p>The complication with Dates, DateTimes and Time values is that the data-type does not specify when a cell values has to be interpreted as a Date or Time - it is simply an Integer or Float numbers like any other. A further indirection has to applied so to correctly recognize Dates, DateTimes and Times:</p>
<ul>
<li>each NUMBER, RK or MULRK value exposes an index referencing the XF (Extended Format) entry associated with the corresponding cell.</li>
<li>each XF record specifies an unique combination of font, alignment, color and so on, however a further indirection specifies the corresponding FORMAT entry</li>
<li>each FORMAT record specifies an output format, such as M/D/YY, h:mm:ss AM/PM or M/D/YY h:mm: and this finally give us a good chance to guess which cell values are intended to represent Date/Time values.</li>
</ul>
<p>Both XF and FORMAT records are globally stored at the Workbook level, and represent ordered arrays.</p>
<p>If you haven't yet given up, if you aren't yet become totally mind-boggled, and if you are still awake and conscious, then you now know how .xls files are internally organized and structured.</p>
<p>Be happy and feel proud of yourself. </p>
</div></div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
<a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(0)"><span class="SelectionMark">&#160;</span>All</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(1)"><span class="SelectionMark">&#160;</span>Data Structures</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(2)"><span class="SelectionMark">&#160;</span>Files</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(3)"><span class="SelectionMark">&#160;</span>Functions</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(4)"><span class="SelectionMark">&#160;</span>Variables</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(5)"><span class="SelectionMark">&#160;</span>Typedefs</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(6)"><span class="SelectionMark">&#160;</span>Defines</a></div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<hr class="footer"/><address class="footer"><small>Generated on Fri Jan 13 2012 for FreeXL by&#160;
<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.7.4 </small></address>
</body>
</html>