XLL+ Class Library (6.3)

Serialize Sample

Demonstrates how to serialize Excel native data types to and from streams

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.

CopyC++
#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:

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

CopyC++
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:
CopyC++
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:

CopyC++
std::ofstream os(file_name);
CXlOStream xlos(os);
xlos << &xloSelection;
os.close();

To deserialize the saved data, the reverse process is used, with CXlIStream:

CopyC++
std::ifstream is(file_name);
CXlIStream xlis(is);
CXlOper xloValue;
xlis >> xloValue;
is.close();

Classes and 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

Sample project

Each sample project is located in a sub-directory of the Samples directory of the XLL+ installation. To use the sample project, open the solution file Serialize.sln or the project file Serialize.vcproj.

You can enable debugging under Excel by using the Setup Debugging command in the XLL+ ToolWindow.

When delivered, the help files are excluded from the build. You can enable the help build by selecting the files Serialize.help.xml and Serialize.chm in the Solution Explorer, and using the right-click menu to view Properties. Select the page "Configuration Properties/General" and set the "Excluded from build" property to "No". See Generating help in the User Guide for more information.

See Also

List of Sample Projects