Click here to Skip to main content
6,306,412 members and growing! (16,868 online)
Email Password   helpLost your password?
Languages » C / C++ Language » General     Intermediate

Saving Excel 2.1 Workbook

By Andrei Litvin

Classes used to save data in Excel 2.1 Workbook format
VC6, VC7Win2K, WinXP, Visual Studio, MFC, Dev
Posted:12 Sep 2002
Views:179,438
Bookmarked:77 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
56 votes for this article.
Popularity: 8.22 Rating: 4.70 out of 5

1

2
5 votes, 10.4%
3
2 votes, 4.2%
4
41 votes, 85.4%
5

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

About the Author

Andrei Litvin


Member

Occupation: Web Developer
Location: Canada Canada

Other popular C / C++ Language articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 122 (Total in Forum: 122) (Refresh)FirstPrevNext
News[ANN] Libexcel library for writing xls-files Pinmemberchipmunk6:16 20 Jul '08  
GeneralMore than one time write PinmemberSimon Potzkai22:11 25 Jun '08  
GeneralSystem Hang(quite urgent).... Pinmembermk200622:43 27 Jun '07  
GeneralHow write a formulate ? PinmemberCG2i030:15 26 May '07  
General.csv file ??? [modified] Pinmembermkmut21:26 25 Apr '07  
GeneralRe: .csv file ??? Pinmembermkmut21:38 25 Apr '07  
GeneralHow should I save a picture into this? Pinmembersuvendu Laha12:48 6 Dec '06  
Generalproblem in strdup (); function PinmemberGlamar20:37 22 Nov '06  
GeneralRe: problem in strdup (); function PinmemberGlamar20:40 26 Nov '06  
GeneralThank you very much Andrey! PinmemberLac_equinu15:27 25 Jul '06  
GeneralMerger Cell Pinmembermbcvamsidhar6:28 6 Jun '06  
GeneralHow to run it in VC environment Pinmemberthomsy21:25 23 May '06  
GeneralRe: How to run it in VC environment Pinmemberemranallan23:05 22 Sep '06  
GeneralRe: How to run it in VC environment Pinmemberdavemaster998:05 24 Nov '06  
GeneralHow to Run In Visual C++6? PinmemberChhoto Bou10:27 15 May '06  
GeneralRe: How to Run In Visual C++6? PinmemberAndrei Litvin14:23 6 Jul '06  
GeneralRe: How to Run In Visual C++6? Pinmemberemranallan2:04 24 Sep '06  
QuestionHow to process merged cell? Pinmemberwangqinyincoder17:34 27 Mar '06  
GeneralStringsize. PinmemberPeter Hendrix3:09 27 Mar '06  
GeneralRe: Stringsize. PinmemberAndrei Litvin14:29 6 Jul '06  
GeneralTried to compile you code, gives memory exception error Pinmembermawani3110:22 3 Jan '06  
AnswerRe: Tried to compile you code, gives memory exception error Pinmemberdavemaster998:01 24 Nov '06  
QuestionChanging Cell to Bold PinmemberKrishna Kumar N2:18 22 Sep '05  
Generalhow do I change the cell number format Pinmemberidanmnr@iprimus.com.au15:06 7 Aug '05  
GeneralCharts PinmemberNinjaNL2:18 29 Jun '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 12 Sep 2002
Editor: Chris Maunder
Copyright 2002 by Andrei Litvin
Everything else Copyright © CodeProject, 1999-2009
Web13 | Advertise on the Code Project