XLL+ Class Library

Serialize Sample

Overview

This sample add-in shows how to use the CXlSerialData class or the CXlOStream and CXlIStream classes to serialize and deserialize Excel data types.

Features

The sample displays the following features:

Notes

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.

Using CXlSerialData

When serializing data using CXlSerialData, the following code is used to serialize the data held in CXlOper xloSelection:

CXlSerialData data(xloSelection);
Alternatively, the following two lines could have been used:
CXlSerialData data;
data.CopyXlOper(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:
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.

size_t cb = [get data size from file];
CXlSerialData data(cb);
Then, after reading the data from file, we can deserialize it into a CXlOper object:
fread(data.GetDataBuffer(), 1, cb, fp);
CXlOper xloValue;
data.GetXlOper(xloValue);

Using CXlOStream and CXlIStream

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();

Functions used

CXlSerialData
CXlSerialData::CXlSerialData
CXlSerialData::GetCount
CXlSerialData::GetData
CXlSerialData::GetDataBuffer
CXlOStream
CXlOStream::CXlOStream
CXlOStream::operator <<
CXlIStream
CXlIStream::CXlIStream
CXlIStream::operator >>
CXlSerialData::GetDataBuffer
CXlSerialData::GetDataBuffer
::XllGetTypedApp
CXlMacros::Selection
CXlOper::Coerce
CXllApp::XlMessageBox
CXlOper::GetActiveCell
CXlOper::GetRef
CXlOper::FromSRef

Project files

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.

See Also

List of Sample Projects | Samples and Walkthroughs