Manage Worksheets
Aspose.Cells provides a class Workbook that represents an Excel file. The Workbook class contains a Worksheets collection that allows access to each worksheet in the Excel file.
A worksheet is represented by the Worksheet class. The Worksheet class provides a wide range of methods for managing worksheets.
Adding Worksheets to a New Excel File
To create a new Excel file programmatically:
- Create an object of the Worksheet class.
- Call the Add method of the WorksheetCollection collection. An empty worksheet is added to the Excel file automatically. It can be referenced by passing the sheet index of the new worksheet to the WorksheetCollection collection.
- Obtain a worksheet reference.
- Perform work on the worksheets.
- Save the new Excel file with new worksheets by calling the Workbook class Save method.
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C | |
Aspose::Cells::Startup(); | |
//Output directory path | |
U16String outDir(u"..\\Data\\02_OutputDirectory\\"); | |
//Path of output excel file | |
U16String outputManageWorksheets = outDir + u"outputManageWorksheets.xlsx"; | |
//Create workbook | |
Workbook workbook; | |
// Adding a new worksheet to the Workbook object | |
int i = workbook.GetWorksheets().Add(); | |
// Obtaining the reference of the newly added worksheet by passing its sheet index | |
Worksheet worksheet = workbook.GetWorksheets().Get(i); | |
// Setting the name of the newly added worksheet | |
worksheet.SetName(u"My Worksheet"); | |
// Save the Excel file. | |
workbook.Save(outputManageWorksheets); | |
std::cout <<"New worksheet added successfully with in a workbook!" << std::endl; | |
Aspose::Cells::Cleanup(); |
Accessing Worksheets using Sheet Index
The following sample code shows how to access or get any worksheet by specifying its index.
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C | |
Aspose::Cells::Startup(); | |
//Source directory path | |
U16String srcDir(u"..\\Data\\01_SourceDirectory\\"); | |
//Path of input excel file | |
U16String sampleManageWorksheets = srcDir + u"sampleManageWorksheets.xlsx"; | |
//Load the sample Excel file | |
Workbook workbook(sampleManageWorksheets); | |
//Accessing a worksheet using its index | |
Worksheet worksheet = workbook.GetWorksheets().Get(0); | |
//Access the cell by its name. | |
Cell cell = worksheet.GetCells().Get(u"F7"); | |
//Print the value of cell F7 | |
U16String val = cell.GetStringValue(); | |
//Print the value on console. | |
std::cout << "Value of cell F7: " << val.ToUtf8() << std::endl; | |
Aspose::Cells::Cleanup(); |
Removing Worksheets using Sheet Index
Removing worksheets by name works well when the name of the worksheet is known. If you don’t know the worksheet’s name, use an overloaded version of the RemoveAt method that takes the sheet index of the worksheet instead of its sheet name.
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C | |
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 sampleManageWorksheets = srcDir + u"sampleManageWorksheets.xlsx"; | |
//Path of output excel file | |
U16String outputManageWorksheets = outDir + u"outputManageWorksheets.xlsx"; | |
//Load the sample Excel file | |
Workbook workbook(sampleManageWorksheets); | |
//Removing a worksheet using its sheet index | |
workbook.GetWorksheets().RemoveAt(0); | |
//Save the Excel file. | |
workbook.Save(outputManageWorksheets); | |
Aspose::Cells::Cleanup(); |