Inserimento, eliminazione di righe e colonne
introduzione
Sia che si crei un nuovo foglio di lavoro da zero o che si lavori su un foglio di lavoro esistente, potrebbe essere necessario aggiungere ulteriori righe o colonne per contenere più dati. Al contrario, potremmo anche dover eliminare righe o colonne da posizioni specificate nel foglio di lavoro. Per soddisfare questi requisiti, Aspose.Cells fornisce un insieme molto semplice di classi e metodi, discussi di seguito.
Gestione di righe e colonne
Aspose.Cells offre un corso,Cartella di lavoro , che rappresenta un file Excel Microsoft. IlCartella di lavoro la classe contiene unFogli di lavoro raccolta che consente l’accesso a ciascun foglio di lavoro in un file Excel. Un foglio di lavoro è rappresentato daFoglio di lavoro classe. IlFoglio di lavoro la classe fornisce unICelleraccolta che rappresenta tutte le celle del foglio di lavoro.
IlICellecollection fornisce diversi metodi per gestire righe e colonne in un foglio di lavoro. Alcuni di questi sono discussi di seguito.
Inserisci una riga
Inserisci una riga nel foglio di lavoro in qualsiasi posizione chiamando il metodoInserisciRiga metodo delICelle collezione. IlInserisciRigaIl metodo accetta l’indice della riga in cui verrà inserita la nuova riga.
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C | |
//Path of input excel file | |
StringPtr sampleInsertingDeletingRowsAndColumns = dirPath->StringAppend(new String("sampleInsertingDeletingRowsAndColumns.xlsx")); | |
//Path of output excel file | |
StringPtr outputInsertingDeletingRowsAndColumns = outPath->StringAppend(new String("outputInsertingDeletingRowsAndColumns.xlsx")); | |
//Read input excel file | |
intrusive_ptr<IWorkbook> workbook = Factory::CreateIWorkbook(sampleInsertingDeletingRowsAndColumns); | |
//Accessing the first worksheet in the Excel file | |
intrusive_ptr<IWorksheet> worksheet = workbook->GetIWorksheets()->GetObjectByIndex(0); | |
//Inserting a row into the worksheet at 3rd position | |
worksheet->GetICells()->InsertRow(2); | |
//Save the Excel file. | |
workbook->Save(outputInsertingDeletingRowsAndColumns); | |
Inserimento di più righe
Per inserire più righe in un foglio di lavoro, chiama il metodoInserisciRighe metodo delICelle collezione. IlInserisciRighemetodo accetta due parametri:
- Indice di riga, l’indice della riga da cui verranno inserite le nuove righe.
- Numero di righe, il numero totale di righe che devono essere inserite.
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C | |
//Path of input excel file | |
StringPtr sampleInsertingDeletingRowsAndColumns = dirPath->StringAppend(new String("sampleInsertingDeletingRowsAndColumns.xlsx")); | |
//Path of output excel file | |
StringPtr outputInsertingDeletingRowsAndColumns = outPath->StringAppend(new String("outputInsertingDeletingRowsAndColumns.xlsx")); | |
//Read input excel file | |
intrusive_ptr<IWorkbook> workbook = Factory::CreateIWorkbook(sampleInsertingDeletingRowsAndColumns); | |
//Accessing the first worksheet in the Excel file | |
intrusive_ptr<IWorksheet> worksheet = workbook->GetIWorksheets()->GetObjectByIndex(0); | |
//Inserting 10 rows into the worksheet starting from 3rd row | |
worksheet->GetICells()->InsertRows(2, 10); | |
//Save the Excel file. | |
workbook->Save(outputInsertingDeletingRowsAndColumns); |
Eliminazione di più righe
Per eliminare più righe da un foglio di lavoro, chiama il metodoElimina righe metodo delICelle collezione. IlElimina righemetodo accetta due parametri:
- Indice di riga, l’indice della riga da cui verranno eliminate le righe.
- Numero di righe, il numero totale di righe che devono essere eliminate.
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C | |
//Path of input excel file | |
StringPtr sampleInsertingDeletingRowsAndColumns = dirPath->StringAppend(new String("sampleInsertingDeletingRowsAndColumns.xlsx")); | |
//Path of output excel file | |
StringPtr outputInsertingDeletingRowsAndColumns = outPath->StringAppend(new String("outputInsertingDeletingRowsAndColumns.xlsx")); | |
//Read input excel file | |
intrusive_ptr<IWorkbook> workbook = Factory::CreateIWorkbook(sampleInsertingDeletingRowsAndColumns); | |
//Accessing the first worksheet in the Excel file | |
intrusive_ptr<IWorksheet> worksheet = workbook->GetIWorksheets()->GetObjectByIndex(0); | |
//Deleting 10 rows from the worksheet starting from 3rd row | |
worksheet->GetICells()->DeleteRows(2, 10); | |
//Save the Excel file. | |
workbook->Save(outputInsertingDeletingRowsAndColumns); |
Inserisci una colonna
Gli sviluppatori possono anche inserire una colonna nel foglio di lavoro in qualsiasi posizione chiamando il metodoInserisci colonna metodo delICelle collezione.Inserisci colonnaIl metodo accetta l’indice della colonna in cui verrà inserita la nuova colonna.
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C | |
//Path of input excel file | |
StringPtr sampleInsertingDeletingRowsAndColumns = dirPath->StringAppend(new String("sampleInsertingDeletingRowsAndColumns.xlsx")); | |
//Path of output excel file | |
StringPtr outputInsertingDeletingRowsAndColumns = outPath->StringAppend(new String("outputInsertingDeletingRowsAndColumns.xlsx")); | |
//Read input excel file | |
intrusive_ptr<IWorkbook> workbook = Factory::CreateIWorkbook(sampleInsertingDeletingRowsAndColumns); | |
//Accessing the first worksheet in the Excel file | |
intrusive_ptr<IWorksheet> worksheet = workbook->GetIWorksheets()->GetObjectByIndex(0); | |
//Inserting a column into the worksheet at 2nd position | |
worksheet->GetICells()->InsertColumn(1); | |
//Save the Excel file. | |
workbook->Save(outputInsertingDeletingRowsAndColumns); |
Elimina una colonna
Per eliminare una colonna dal foglio di lavoro in qualsiasi posizione, chiama il metodoElimina colonna metodo delICelle collezione. IlElimina colonnaIl metodo accetta l’indice della colonna da eliminare.
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C | |
//Path of input excel file | |
StringPtr sampleDeleteColumn = dirPath->StringAppend(new String("sampleInsertingDeletingRowsAndColumns.xlsx")); | |
//Path of output excel file | |
StringPtr outputDeleteColumn = outPath->StringAppend(new String("outputInsertingDeletingRowsAndColumns.xlsx")); | |
//Read input excel file | |
intrusive_ptr<IWorkbook> workbook = Factory::CreateIWorkbook(sampleDeleteColumn); | |
//Accessing the first worksheet in the Excel file | |
intrusive_ptr<IWorksheet> worksheet = workbook->GetIWorksheets()->GetObjectByIndex(0); | |
//Deleting a column from the worksheet at 2nd position | |
worksheet->GetICells()->DeleteColumn(4); | |
//Save the Excel file. | |
workbook->Save(outputDeleteColumn); |