Click here to Skip to main content
13,353,322 members (52,741 online)
Click here to Skip to main content
Add your own
alternative version

Stats

334.6K views
7.1K downloads
112 bookmarked
Posted 12 Sep 2002

Saving Excel 2.1 Workbook

, 12 Sep 2002
Rate this:
Please Sign up or sign in to vote.
Classes used to save data in Excel 2.1 Workbook format

Sample Image

Introduction

Recently I worked on a project that required exporting some reports containing various file statistics. The goal was having one format that would look good immediately and one easy accessible format for the user to import and build custom reports. For the first export type, plain HTML was used. But for importing, HTML is not really easy to use, so Excel file creation seemed a good choice.

Requirements

Since the Excel exported file is just for the user to have easy access to data, only a small part of the Excel file format features were needed. Also, there was no guarantee that Excel would even be installed on the computer where the reports are generated and since a port to Linux was imminent, it would be nice for the class to be platform independent. The goal was to be able to write something as this:

CMiniExcel miniexcel;

miniexcel(0,0) = "FileName";
miniexcel(0,1) = "Size (MB)";

miniexcel(1,0) = "c:\\bigfile.txt";
miniexcel(1,1) = 123.45;

miniexcel.SaveAs("Report.xls")

There are no real memory constraints, since Excel cannot handle over ~65K rows anyway. (Actually we also needed a single big report with more that 65K rows, and we had to save it as a "Comma separated values" file). So the whole excel worksheet could be kept in memory.

Excel file format

After a little bit of searching on the net, I decided on the Excel 2.1 Workbook format (description of the format found here). Even though this format is old, it can be imported by Excel or by other programs and it seemed suitable for our simple needs.

From the documentation of the Excel 2.1 format it turns out that an excel document is stored using a format called Binary File Format (BIFF). This format is a sequence of BIFF records, each record containing a certain property/value of the workgroup. Each BIFF record starts with a 2 byte number containing it's type and another 2 bytes for the size of the record. After this header, record-specific data will follow:

XX XX

 XX XX

 ....

Type  Length  Specific Data

From the several record types needed, only a few were really important for simple excel exporting. BOF and EOF records were unavoidable and also text strings (LABEL record) and numbers (NUMBER record) needed to be saved.

Implementation

First of all, to take care of the platform independence and also to make life easier when saving the BIFF records, I implemented a writer class (CLittleEndianWriter) that should be able to write 1 byte, 2 byte and 4 byte values in little endian byte order. Also, it needed to be able to save double values in IEEE format, since this is the format used to store real numbers (excel has a BIFF record fr storing a 16 bit unsigned integer, but it is not enough):

class LittleEndianWriter{
  ...
public:
  ...
  void Write1 (char v);
  void Write2 (int v);
  void Write4 (long v);
  void WriteFloatIEEE (float v);
  void WriteDoubleIEEE (double v); 
};

After this, it also seems pretty clear that since every single record is saved in a biff file format, it would be nice have a abstract BIFFRecord class and that every single record should be derived from it.

class BIFFRecord{
  ...

  /* Write a BIFF header for the opcode nRecno of length nRecLen */
  void Write (LittleEndianWriter *pWriter, int nRecNo, int nRecLen);

public:

  /* We should be able to write every type of BIFF records */
  virtual void Write (LittleEndianWriter *pWriter) = 0; 
}; 

This being done, implementing excel file saving is only adding the required BIFF records. I added in this demo project BOF, EOF, NUMBER and LABEL (others should be easy to add). We also need a generic container class, so that Excel file creation is easy. The container class is needed because BIFF records have to be in a specific order in the XLS file and it's nice if the container class does this for us:

class CMiniExcel{
  ...
public:
  ... 
  /* Access the columns in the excel document */
  ExcelCell &operator() (unsigned row, unsigned column);

  /* Write into a file. */
  void Write (FILE *dest);
};

Note that the operator () returns an ExcelCell. This is just a simple container for either numbers or strings and it makes like a lot easier when coding by not having to worry about cell types in the Excel Workbook.

Final notes

This is by no means a complete project. The saving is basic and error checking is almost none existing. Adding other BIFF records to the generic format should be pretty easy and if somebody finds it useful, I might add a few more into this demo source code.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Andrei Litvin
Web Developer
Canada Canada
No Biography provided

You may also be interested in...

Comments and Discussions

 
AnswerRe: How to change font properties? Pin
Andrei Litvin25-Mar-03 17:04
memberAndrei Litvin25-Mar-03 17:04 
GeneralRe: How to change font properties? Pin
dipukundu29-Mar-03 9:47
memberdipukundu29-Mar-03 9:47 
GeneralRe: How to change font properties? Pin
dipukundu29-Mar-03 9:48
sussdipukundu29-Mar-03 9:48 
GeneralThank you very much .... Pin
hero24_m23-Mar-03 3:02
memberhero24_m23-Mar-03 3:02 
Questionhow to merge cells? Pin
AnnJiao17-Mar-03 5:23
sussAnnJiao17-Mar-03 5:23 
AnswerRe: how to merge cells? Pin
Andrei Litvin17-Mar-03 15:11
memberAndrei Litvin17-Mar-03 15:11 
GeneralI want to write use Biff5..7 files Pin
superrock10-Mar-03 8:01
membersuperrock10-Mar-03 8:01 
GeneralAdding URL LINK Pin
keeyong27-Feb-03 15:33
memberkeeyong27-Feb-03 15:33 
GeneralHoe to use setFontNum and setFormatNum Pin
cpede22-Feb-03 6:31
membercpede22-Feb-03 6:31 
Generaltuii've got errors...when building Pin
Anonymous2-Feb-03 3:17
sussAnonymous2-Feb-03 3:17 
GeneralRe: tuii've got errors...when building Pin
cyberrob3-Feb-03 4:03
membercyberrob3-Feb-03 4:03 
GeneralErrors building mfc project Pin
NeilDevlin15-Jan-03 7:18
memberNeilDevlin15-Jan-03 7:18 
GeneralRe: Errors building mfc project Pin
.S.Rod.15-Jan-03 7:47
member.S.Rod.15-Jan-03 7:47 
GeneralRe: Errors building mfc project Pin
mimosa7-Aug-10 8:20
membermimosa7-Aug-10 8:20 
GeneralOther kind of records Pin
moricg7-Dec-02 5:49
membermoricg7-Dec-02 5:49 
GeneralSorry, have problem with miniexecl and mfc Pin
gebrudergrimm22-Nov-02 0:45
membergebrudergrimm22-Nov-02 0:45 
GeneralRe: Sorry, have problem with miniexecl and mfc Pin
Andrei Litvin22-Nov-02 2:39
memberAndrei Litvin22-Nov-02 2:39 
GeneralRe: Sorry, have problem with miniexecl and mfc Pin
gebrudergrimm22-Nov-02 3:43
membergebrudergrimm22-Nov-02 3:43 
GeneralRe: Sorry, have problem with miniexecl and mfc Pin
iwx24-Apr-03 7:01
memberiwx24-Apr-03 7:01 
GeneralRe: Sorry, have problem with miniexecl and mfc Pin
Smeghead8-Jul-03 4:51
memberSmeghead8-Jul-03 4:51 
Generaladd range name Pin
Anonymous18-Nov-02 6:32
sussAnonymous18-Nov-02 6:32 
GeneralSupport for functions Pin
Eddie Mark14-Nov-02 14:10
memberEddie Mark14-Nov-02 14:10 
GeneralRe: Support for functions Pin
Andrei Litvin17-Nov-02 14:17
memberAndrei Litvin17-Nov-02 14:17 
GeneralReading excel file and displaying in listcontrol Pin
sunilsamineni5-Nov-02 2:17
membersunilsamineni5-Nov-02 2:17 
GeneralRe: Reading excel file and displaying in listcontrol Pin
.S.Rod.5-Nov-02 2:32
member.S.Rod.5-Nov-02 2:32 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.180111.1 | Last Updated 13 Sep 2002
Article Copyright 2002 by Andrei Litvin
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid