管理工作表
Contents
[
Hide
]
开发人员可以使用 Aspose.Cells 灵活 API 以编程方式轻松地在 Microsoft Excel 文件中创建和管理工作表。本主题介绍在 Microsoft Excel 文件中添加和删除工作表的方法。
Aspose.Cells提供类工作簿表示一个 Excel 文件。这工作簿类包含一个工作表允许访问 Excel 文件中每个工作表的集合。
将工作表添加到新的 Excel 文件
要以编程方式创建新的 Excel 文件:
- 创建对象的工作表班级。
- 打电话给添加的方法IWorksheet 集合收藏。一个空工作表会自动添加到 Excel 文件中。可以通过将新工作表的工作表索引传递给IWorksheet 集合收藏。
- 获取工作表参考。
- 在工作表上执行工作。
- 通过调用工作簿班级救球方法。
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); |
使用工作表索引访问工作表
以下示例代码显示如何通过指定其索引来访问或获取任何工作表。
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); |
使用工作表索引删除工作表
当工作表的名称已知时,按名称删除工作表效果很好。如果您不知道工作表的名称,请使用移除位置采用工作表的工作表索引而不是其工作表名称的方法。
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); |