Load Specific Worksheets in a Workbook with C++
Contents
[
Hide
]
By default, Aspose.Cells loads the entire spreadsheet into memory. It is possible to load only specific sheets, which can improve performance and consume less memory. This approach is useful when working with a large workbook made up of many worksheets.
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
class CustomLoad : public LoadFilter {
public:
CustomLoad() : LoadFilter(LoadDataFilterOptions::All) {}
// Override StartSheet to customize loading behavior
void StartSheet(Worksheet& sheet) override {
// Custom logic for loading specific worksheet
}
};
int main() {
Aspose::Cells::Startup();
// Source directory path
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
// Define a new Workbook
Workbook workbook;
// Load the workbook with the specified worksheet only
LoadOptions loadOptions(LoadFormat::Xlsx);
loadOptions.SetLoadFilter(new CustomLoad());
// Create the workbook with custom load options
workbook = Workbook(srcDir + u"TestData.xlsx", loadOptions);
// Perform your desired task
// Save the workbook
workbook.Save(srcDir + u"outputFile.out.xlsx");
std::cout << "Workbook processed and saved successfully!" << std::endl;
Aspose::Cells::Cleanup();
return 0;
}
Here is the implementation of the CustomLoad
class.
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
class CustomLoad : public LoadFilter
{
public:
CustomLoad() : LoadFilter() {}
~CustomLoad() {}
void StartSheet(Worksheet& sheet) override
{
if (sheet.GetName() == u"Sheet2")
{
// Load everything from worksheet "Sheet2"
SetLoadDataFilterOptions(LoadDataFilterOptions::All);
}
else
{
// Load nothing
SetLoadDataFilterOptions(LoadDataFilterOptions::Structure);
}
}
};