This sample add-in shows how to use the CXlSerialData class or the CXlOStream and CXlIStream classes to serialize and deserialize Excel data types.
The sample displays the following features:
The following line is added to serialize.cpp, to include the serialization classes.
#include <XlSerialize.h>
Each of the add-in functions is marked as a Macro function using the XLL+ Function Wizard.
When serializing data using CXlSerialData, the following code is used to serialize the data held in CXlOper xloSelection:
Alternatively, the following two lines could have been used:CXlSerialData data(xloSelection);
We can get the size and address of the serialized byte array using CXlSerialData::GetCount() and CXlSerialData::GetData(), and use these to save the data to file:CXlSerialData data; data.CopyXlOper(xloSelection);
fwrite(data.GetData(), 1, data.GetCount(), fp);
To deserialize the saved data, two steps are required. First we must allocate an object with a big enough buffer to read all the serialized data.
Then, after reading the data from file, we can deserialize it into a CXlOper object:size_t cb = [get data size from file]; CXlSerialData data(cb);
fread(data.GetDataBuffer(), 1, cb, fp); CXlOper xloValue; data.GetXlOper(xloValue);
When serializing data using CXlOStream, the following code is used to serialize the data held in CXlOper xloSelection:
std::ofstream os(file_name); CXlOStream xlos(os); xlos << &xloSelection; os.close();
To deserialize the saved data, the reverse process is used, with CXlIStream:
std::ifstream is(file_name); CXlIStream xlis(is); CXlOper xloValue; xlis >> xloValue; is.close();
If you are using MS Developer Studio 6, then you should open the project file Serialize.dsp.
If you are using MS Visual Studio .NET 2002, then you should open the solution file Serialize.sln or the project file Serialize.vcproj.
If you are using MS Visual Studio .NET 2003, then you should open the solution file Serialize71.sln or the project file Serialize71.vcproj.
If you are using MS Visual Studio 2005, then you should open the solution file Serialize8.sln or the project file Serialize8.vcproj.