Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
If there are too many defined names or named ranges in the Excel files, we have to clear some because they are not referenced again.
To remove a named range from Excel, you can follow these steps:
With Aspose.Cells for C++, you can remove named ranges or defined names by text or index in the collection.
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// Source directory path
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
// Output directory path
U16String outDir(u"..\\Data\\02_OutputDirectory\\");
// Path of input Excel file
U16String inputFilePath = srcDir + u"Book1.xlsx";
// Path of output Excel file
U16String outputFilePath = outDir + u"Book2.xlsx";
// Instantiate a new Workbook
Workbook workbook(inputFilePath);
// Get all the worksheets in the book
WorksheetCollection worksheets = workbook.GetWorksheets();
// Delete a named range by text
worksheets.GetNames().Remove(u"NamedRange");
// Delete a defined name by index
worksheets.GetNames().RemoveAt(0);
// Save the workbook to retain the changes
workbook.Save(outputFilePath);
std::cout << "Named ranges removed successfully!" << std::endl;
Aspose::Cells::Cleanup();
}
Note: If the defined name is referenced by formulas, it cannot be removed. You can only remove the formula that references the defined name.
When we remove a defined name, we have to check whether it is referenced by any formulas in the file.
In order to improve the performance of removing named ranges, we can remove several at once.
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// Source directory path
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
// Output directory path
U16String outDir(u"..\\Data\\02_OutputDirectory\\");
// Path of input Excel file
U16String inputFilePath = srcDir + u"Book1.xlsx";
// Path of output Excel file
U16String outputFilePath = outDir + u"Book2.xlsx";
// Instantiate a new Workbook
Workbook workbook(inputFilePath);
// Get all the worksheets in the book
WorksheetCollection worksheets = workbook.GetWorksheets();
// Delete some defined names
Vector<U16String> namesToRemove = { u"NamedRange1", u"NamedRange2" };
worksheets.GetNames().Remove(namesToRemove);
// Save the workbook to retain the changes
workbook.Save(outputFilePath);
std::cout << "Named ranges removed successfully!" << std::endl;
Aspose::Cells::Cleanup();
}
Some Excel files become corrupted because they contain duplicate defined names. So we can remove these duplicate names to repair the file.
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// Source directory path
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
// Output directory path
U16String outDir(u"..\\Data\\02_OutputDirectory\\");
// Path of input Excel file
U16String inputFilePath = srcDir + u"Book1.xlsx";
// Path of output Excel file
U16String outputFilePath = outDir + u"Book2.xlsx";
// Instantiate a new Workbook
Workbook workbook(inputFilePath);
// Get all the worksheets in the book
WorksheetCollection worksheets = workbook.GetWorksheets();
// Delete duplicate defined names
worksheets.GetNames().RemoveDuplicateNames();
// Save the workbook to retain the changes
workbook.Save(outputFilePath);
std::cout << "Workbook saved successfully after removing duplicate names!" << std::endl;
Aspose::Cells::Cleanup();
}
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.