Creating an Excel file from VC++

mawajo

New Member
Joined
Sep 16, 2002
Messages
1
Hello, I am using VC++ and would like to create a formatted file that is compatible with Excel (something more the just tab delimited). Does anyone know of a spec for .xls files or have any suggestions on this?

Thanks,
mawajo
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
This may be useful.

* Security : DEV.

======================================================================

---------------------------------------------------------------------

The information in this article applies to:



- Microsoft Visual C++, 32-bit Editions, version 5.0

- Microsoft Excel 97 for Windows

- Microsoft Excel for Windows 95, versions 7.0, 7.0a

- Microsoft Excel for Windows, versions 5.0, 5.0a, 5.0c

---------------------------------------------------------------------



SUMMARY

=======



This article demonstrates how to determine the version of a Microsoft Excel

Workbook (.xls).



MORE INFORMATION

================



Microsoft Excel saves data using structured storage. In particular, it

creates a data stream called "Workbook" (previously just "Book") where

it saves the contents starting with a BOF (beginning of file) record. This

record contains useful attributes of the workbook, as well as the version.

The following Microsoft Visual C++ code demonstrates how to open the file,

read it, and return the version number based on the BOF.



1. Create a new "Win32 Console Application" in Microsoft Developer Studio.



2. Add a C++ Source File (.cpp) to the project and add the following code

to the source file:



#include<windows.h>

#include<iostream.h> // My additions



// BOF record from Microsoft Excel

typedef struct _xlbof

{

char bofMarker; // Should be 0x09



char vers; // Version indicator for biff2, biff3, and biff4

// = 0x00 -> Biff2

// = 0x02 -> Biff3

// = 0x04 -> Biff4

// = 0x08 -> Biff5/Biff7/Biff8



char skip[2]; // Unspecified



short int vers2; // Version number

// 0x0500 -> Biff5/Biff7

// 0x0600 -> Biff8



short int dt; // Substream type (not used in this example)



short int rupBuild; // Internal build identifier

short int rupYear; // Internal Build year

} XLBOF;





//* XLVersionFromFile() ******************************************

//* Returns

//* n for BiffN

//* i.e. 8 for Biff8 (Microsoft Excel 97)

//*

//* Negative if an error occurs

//****************************************************************



int XLVersionFromFile(char *filename) {

// Translate filename to Unicode

WCHAR wcFilename[1024];

int i = mbstowcs(wcFilename, filename, strlen(filename));

wcFilename = 0;



IStorage *pStorage;

HRESULT hr;

XLBOF xlbof;



// Open the document as an OLE compound document

hr = ::StgOpenStorage(wcFilename, NULL,

STGM_READ | STGM_SHARE_EXCLUSIVE, NULL, 0, &pStorage);



if(!FAILED(hr)) {

// Open the data-stream where Microsoft Excel stores the data

IStream *pStream;

hr = pStorage->OpenStream(L"Workbook", NULL,

STGM_READ | STGM_SHARE_EXCLUSIVE, 0, &pStream);



// If "Workbook" does not exist, try "Book"

if(FAILED(hr)) {

hr = pStorage->OpenStream(L"Book", NULL,

STGM_READ | STGM_SHARE_EXCLUSIVE, 0, &pStream);

}

if(!FAILED(hr)) {

// Read the relevant BOF information

DWORD dwCount; // bytes read

pStream->Read(&xlbof, sizeof(XLBOF), &dwCount);



// Let go of the IStream pointer

pStream->Release();

}

else return -2;



// Let go of the IStorage pointer

pStorage->Release();

}

else return -1;



// Determine which version to return

if(xlbof.vers != 0x08) return (xlbof.vers + 4) / 2;

else {

switch(xlbof.vers2) {

case 0x0500: // Either Biff5 or Biff7

// Biff7's rupYear is at least 1994

if(xlbof.rupYear< 1994) return 5;



// Check for specific builds of Microsoft Excel 5

switch(xlbof.rupBuild) {

case 2412: // XL5a

case 3218: // XL5c

case 3321: // NT XL5

return 5;

default:

return 7;

}



case 0x0600: return 8;

}

}



// Version not recognized. Perhaps there is a newer version.

return -3;

}



void main()

{

int iretVal = 0;

iretVal = XLVersionFromFile("C:\Test.xls");

//Adapt the filename to your example

cout<< "The Excel Version is "<< iretVal<< "nnr";

return;

}



3. In the main()function, you may need to modify the path and filename of

the Microsoft Excel workbook in the following line of code:



iretVal = XLVersionFromFile("C:\Test.xls");
This message was edited by on 2002-09-17 12:01
 
Upvote 0

Forum statistics

Threads
1,214,944
Messages
6,122,391
Members
449,080
Latest member
Armadillos

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top