Inserimento, Eliminazione di Righe e Colonne
Introduzione
Che si stia creando un nuovo foglio di lavoro da zero o si stia lavorando su un foglio di lavoro esistente, potremmo avere bisogno di aggiungere righe o colonne aggiuntive per ospitare più dati. In modo inverso, potremmo anche dover eliminare righe o colonne da posizioni specifiche 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 fornisce una classe, Workbook che rappresenta un file Microsoft Excel. La classe Workbook contiene una collezione di Worksheets che consente di accedere a ciascun foglio di lavoro in un file Excel. Un foglio di lavoro è rappresentato dalla classe Worksheet. La classe Worksheet fornisce una collezione di Cells che rappresenta tutte le celle nel foglio di lavoro.
La collezione Cells fornisce diversi metodi per gestire righe e colonne in un foglio di lavoro. Alcuni di questi sono discussi di seguito.
Inserire una Riga
Inserire una riga nel foglio di lavoro in qualsiasi posizione chiamando il metodo InsertRow della collezione Cells. Il metodo InsertRow richiede l’indice della riga dove verrà inserita la nuova riga.
Aspose::Cells::Startup(); | |
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C | |
//Path of input | |
U16String dirPath(u""); | |
//Path of output | |
U16String outPath(u""); | |
//Path of input excel file | |
U16String sampleInsertingDeletingRowsAndColumns = dirPath + u"sampleInsertingDeletingRowsAndColumns.xlsx"; | |
//Path of output excel file | |
U16String outputInsertingDeletingRowsAndColumns = outPath + u"outputInsertingDeletingRowsAndColumns.xlsx"; | |
//Read input excel file | |
Workbook workbook(sampleInsertingDeletingRowsAndColumns); | |
//Accessing the first worksheet in the Excel file | |
Worksheet worksheet = workbook.GetWorksheets().Get(0); | |
//Inserting a column into the worksheet at 2nd position | |
worksheet.GetCells().InsertColumn(1); | |
//Save the Excel file. | |
workbook.Save(outputInsertingDeletingRowsAndColumns); | |
Aspose::Cells::Cleanup(); |
Inserimento di Più Righe
Per inserire più righe in un foglio di lavoro, chiamare il metodo InsertRows della collezione Cells. Il metodo InsertRows richiede due parametri:
- Indice di riga, l’indice della riga da cui saranno inserite le nuove righe.
- Numero di righe, il numero totale di righe da inserire.
Aspose::Cells::Startup(); | |
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C | |
//Path of input | |
U16String dirPath(u""); | |
//Path of output | |
U16String outPath(u""); | |
//Path of input excel file | |
U16String sampleInsertingDeletingRowsAndColumns = dirPath + u"sampleInsertingDeletingRowsAndColumns.xlsx"; | |
//Path of output excel file | |
U16String outputInsertingDeletingRowsAndColumns = outPath + u"outputInsertingDeletingRowsAndColumns.xlsx"; | |
//Read input excel file | |
Workbook workbook(sampleInsertingDeletingRowsAndColumns); | |
//Accessing the first worksheet in the Excel file | |
Worksheet worksheet = workbook.GetWorksheets().Get(0); | |
//Inserting 10 rows into the worksheet starting from 3rd row | |
worksheet.GetCells().InsertRows(2, 10); | |
//Save the Excel file. | |
workbook.Save(outputInsertingDeletingRowsAndColumns); | |
Aspose::Cells::Cleanup(); |
Eliminazione di Più Righe
Per eliminare più righe da un foglio di lavoro, chiamare il metodo DeleteRows della collezione Cells. Il metodo DeleteRows richiede due parametri:
- Indice riga, l’indice della riga da cui partiranno le eliminazioni.
- Numero di righe, il numero totale di righe da eliminare.
Aspose::Cells::Startup(); | |
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C | |
//Path of input | |
U16String dirPath(u""); | |
//Path of output | |
U16String outPath(u""); | |
//Path of input excel file | |
U16String sampleInsertingDeletingRowsAndColumns = dirPath + u"sampleInsertingDeletingRowsAndColumns.xlsx"; | |
//Path of output excel file | |
U16String outputInsertingDeletingRowsAndColumns = outPath + u"outputInsertingDeletingRowsAndColumns.xlsx"; | |
//Read input excel file | |
Workbook workbook(sampleInsertingDeletingRowsAndColumns); | |
//Accessing the first worksheet in the Excel file | |
Worksheet worksheet = workbook.GetWorksheets().Get(0); | |
//Deleting 10 rows from the worksheet starting from 3rd row | |
worksheet.GetCells().DeleteRows(2, 10); | |
//Save the Excel file. | |
workbook.Save(outputInsertingDeletingRowsAndColumns); | |
Aspose::Cells::Cleanup(); |
Inserire una colonna
Gli sviluppatori possono anche inserire una colonna nel foglio di lavoro in qualsiasi posizione chiamando il metodo InsertColumn della collezione Cells. Il metodo InsertColumn richiede l’indice della colonna dove verrà inserita la nuova colonna.
Aspose::Cells::Startup(); | |
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C | |
//Path of input | |
U16String dirPath(u""); | |
//Path of output | |
U16String outPath(u""); | |
//Path of input excel file | |
U16String sampleInsertingDeletingRowsAndColumns = dirPath + u"sampleInsertingDeletingRowsAndColumns.xlsx"; | |
//Path of output excel file | |
U16String outputInsertingDeletingRowsAndColumns = outPath + u"outputInsertingDeletingRowsAndColumns.xlsx"; | |
//Read input excel file | |
Workbook workbook(sampleInsertingDeletingRowsAndColumns); | |
//Accessing the first worksheet in the Excel file | |
Worksheet worksheet = workbook.GetWorksheets().Get(0); | |
//Inserting a column into the worksheet at 2nd position | |
worksheet.GetCells().InsertColumn(1); | |
//Save the Excel file. | |
workbook.Save(outputInsertingDeletingRowsAndColumns); | |
Aspose::Cells::Cleanup(); |
Eliminare una colonna
Per eliminare una colonna dal foglio di lavoro in qualsiasi posizione, chiamare il metodo DeleteColumn della collezione Cells. Il metodo DeleteColumn richiede l’indice della colonna da eliminare.
Aspose::Cells::Startup(); | |
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C | |
//Path of input | |
U16String dirPath(u""); | |
//Path of output | |
U16String outPath(u""); | |
//Path of input excel file | |
U16String sampleInsertingDeletingRowsAndColumns = dirPath + u"sampleInsertingDeletingRowsAndColumns.xlsx"; | |
//Path of output excel file | |
U16String outputInsertingDeletingRowsAndColumns = outPath + u"outputInsertingDeletingRowsAndColumns.xlsx"; | |
//Read input excel file | |
Workbook workbook(sampleInsertingDeletingRowsAndColumns); | |
//Accessing the first worksheet in the Excel file | |
Worksheet worksheet = workbook.GetWorksheets().Get(0); | |
//Deleting a column from the worksheet at 2nd position | |
worksheet.GetCells().DeleteColumn(1); | |
//Save the Excel file. | |
workbook.Save(outputInsertingDeletingRowsAndColumns); | |
Aspose::Cells::Cleanup(); |