Manage Worksheets
Contents
[
Hide
]
Developers can easily create and manage worksheets in Microsoft Excel files programmatically using Aspose.Cells flexible API. This topic describes approaches for adding and removing worksheets in Microsoft Excel files.
Aspose.Cells provides a class IWorkbook that represents an Excel file. The IWorkbook class contains a Worksheets collection that allows access to each worksheet in the Excel file.
A worksheet is represented by the IWorksheet class. The IWorksheet 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 IWorksheet class.
- Call the Add method of the IWorksheetCollection 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 IWorksheetCollection collection.
- Obtain a worksheet reference.
- Perform work on the worksheets.
- Save the new Excel file with new worksheets by calling the IWorkbook class Save method.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C | |
//Output directory path | |
StringPtr outDir = new String("..\\Data\\02_OutputDirectory\\"); | |
//Path of output excel file | |
StringPtr outputManageWorksheets = outDir->StringAppend(new String("outputManageWorksheets.xlsx")); | |
//Create workbook | |
intrusive_ptr<IWorkbook> workbook = Factory::CreateIWorkbook(); | |
// Adding a new worksheet to the Workbook object | |
int i = workbook->GetIWorksheets()->Add(); | |
// Obtaining the reference of the newly added worksheet by passing its sheet index | |
intrusive_ptr<IWorksheet> worksheet = workbook->GetIWorksheets()->GetObjectByIndex(i); | |
// Setting the name of the newly added worksheet | |
worksheet->SetName(new String("My Worksheet")); | |
// Save the Excel file. | |
workbook->Save(outputManageWorksheets); | |
StringPtr msg = new String("New worksheet added successfully with in a workbook!"); | |
Console::WriteLine(msg); |
Accessing Worksheets using Sheet Index
The following sample code shows how to access or get any worksheet by specifying its index.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C | |
//Source directory path | |
StringPtr srcDir = new String("..\\Data\\01_SourceDirectory\\"); | |
//Path of input excel file | |
StringPtr sampleManageWorksheets = srcDir->StringAppend(new String("sampleManageWorksheets.xlsx")); | |
//Load the sample Excel file | |
intrusive_ptr<IWorkbook> workbook = Factory::CreateIWorkbook(sampleManageWorksheets); | |
//Accessing a worksheet using its index | |
intrusive_ptr<IWorksheet> worksheet = workbook->GetIWorksheets()->GetObjectByIndex(0); | |
//Access the cell by its name. | |
intrusive_ptr<ICell> cell = worksheet->GetICells()->GetObjectByIndex(new String("F7")); | |
//Print the value of cell F7 | |
StringPtr val = cell->GetStringValue(); | |
//Print the value on console. | |
Console::Write(new String("Value of cell F7: ")); | |
Console::WriteLine(val); |
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C | |
//Source directory path | |
StringPtr srcDir = new String("..\\Data\\01_SourceDirectory\\"); | |
//Output directory path | |
StringPtr outDir = new String("..\\Data\\02_OutputDirectory\\"); | |
//Path of input excel file | |
StringPtr sampleManageWorksheets = srcDir->StringAppend(new String("sampleManageWorksheets.xlsx")); | |
//Path of output excel file | |
StringPtr outputManageWorksheets = outDir->StringAppend(new String("outputManageWorksheets.xlsx")); | |
//Load the sample Excel file | |
intrusive_ptr<IWorkbook> workbook = Factory::CreateIWorkbook(sampleManageWorksheets); | |
//Removing a worksheet using its sheet index | |
workbook->GetIWorksheets()->RemoveAt(0); | |
//Save the Excel file. | |
workbook->Save(outputManageWorksheets); |