Gestisci fogli di lavoro

Aspose.Cells offre un corsoCartella di lavoro che rappresenta un file Excel. IlCartella di lavoro la classe contiene unFogli di lavororaccolta che consente l’accesso a ciascun foglio di lavoro nel file Excel.

Un foglio di lavoro è rappresentato daFoglio di lavoro classe. IlFoglio di lavorofornisce una vasta gamma di metodi per la gestione dei fogli di lavoro.

Aggiunta di fogli di lavoro a un nuovo file Excel

Per creare un nuovo file Excel a livello di codice:

  1. Crea un oggetto diFoglio di lavoroclasse.
  2. Chiama ilAggiungere metodo delIWorksheetCollection collezione. Un foglio di lavoro vuoto viene aggiunto automaticamente al file Excel. È possibile fare riferimento passando l’indice del foglio del nuovo foglio di lavoro al fileIWorksheetCollectioncollezione.
  3. Ottenere un riferimento al foglio di lavoro.
  4. Eseguire il lavoro sui fogli di lavoro.
  5. Salva il nuovo file Excel con i nuovi fogli di lavoro chiamando il fileCartella di lavoro classeSalvametodo.
//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);

Accesso ai fogli di lavoro utilizzando l’indice dei fogli

Il codice di esempio seguente mostra come accedere o ottenere qualsiasi foglio di lavoro specificandone l’indice.

//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);

Rimozione di fogli di lavoro utilizzando l’indice dei fogli

La rimozione dei fogli di lavoro per nome funziona bene quando il nome del foglio di lavoro è noto. Se non conosci il nome del foglio di lavoro, usa una versione sovraccaricata del fileRimuoviAtmetodo che accetta l’indice del foglio di lavoro anziché il nome del foglio.

//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);