Demonstrates how to serialize Excel native data types to and from streams
The sample displays the following features:
datafile_1.dat). They use the CXlSerialData class to convert the data to and from a serialized form as an array of bytes; the byte array is saved and restored to and from file using the standard fwrite() and fread() functions.
datafile_2.dat), but use the CXlOStream and CXlIStream classes to serialize the data directly to file.
The following line is added to serialize.cpp, to include the serialization classes.
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:
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);
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();
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
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
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.