Insertion, suppression des lignes et des colonnes
Introduction
Que ce soit pour créer une nouvelle feuille de calcul à partir de zéro ou pour travailler sur une feuille de calcul existante, nous pouvons avoir besoin d’ajouter des lignes ou des colonnes supplémentaires pour accueillir plus de données. Inversement, nous pouvons également avoir besoin de supprimer des lignes ou des colonnes à des positions spécifiées dans la feuille de calcul. Pour répondre à ces exigences, Aspose.Cells fournit un ensemble très simple de classes et de méthodes, discutées ci-dessous.
Gestion des lignes et des colonnes
Aspose.Cells fournit une classe, Workbook, qui représente un fichier Microsoft Excel. La classe Workbook contient une collection Worksheets qui permet d’accéder à chaque feuille de calcul dans un fichier Excel. Une feuille de calcul est représentée par la classe Worksheet. La classe Worksheet fournit une collection Cells qui représente toutes les cellules de la feuille de calcul.
La collection Cells propose plusieurs méthodes de gestion des lignes et des colonnes dans une feuille de calcul. Certaines d’entre elles sont discutées ci-dessous.
Insérer une ligne
Insérez une ligne dans la feuille de calcul à n’importe quel endroit en appelant la méthode InsertRow de la collection Cells. La méthode InsertRow prend l’index de la ligne où la nouvelle ligne sera insérée.
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(); |
Insertion de plusieurs lignes
Pour insérer plusieurs lignes dans une feuille de calcul, appelez la méthode InsertRows de la collection Cells. La méthode InsertRows prend deux paramètres :
- Index de la ligne, l’index de la ligne à partir de laquelle les nouvelles lignes seront insérées.
- Nombre de lignes, le nombre total de lignes à insérer.
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(); |
Suppression de plusieurs lignes
Pour supprimer plusieurs lignes d’une feuille de calcul, appelez la méthode DeleteRows de la collection Cells. La méthode DeleteRows prend deux paramètres :
- Index de la ligne, l’index de la ligne à partir de laquelle les lignes seront supprimées.
- Nombre de lignes, le nombre total de lignes à supprimer.
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(); |
Insérer une colonne
Les développeurs peuvent également insérer une colonne dans la feuille de calcul à n’importe quel emplacement en appelant la méthode InsertColumn de la collection Cells. La méthode InsertColumn prend l’index de la colonne où la nouvelle colonne sera insérée.
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(); |
Supprimer une colonne
Pour supprimer une colonne de la feuille de calcul à n’importe quel emplacement, appelez la méthode DeleteColumn de la collection Cells. La méthode DeleteColumn prend l’index de la colonne à supprimer.
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(); |