Insertion, suppression de lignes et de colonnes

Introduction

Qu’il s’agisse de créer une nouvelle feuille de calcul à partir de zéro ou de travailler sur une feuille de calcul existante, nous devrons peut-être 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 à partir de 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, décrites ci-dessous.

Gestion des lignes et des colonnes

Aspose.Cells fournit une classe,IClasseur , qui représente un fichier Excel Microsoft. LeIClasseur classe contient unFeuilles de travail collection qui permet d’accéder à chaque feuille de calcul dans un fichier Excel. Une feuille de calcul est représentée par leFeuille de travail classe. LeFeuille de travail classe offre uneICellulescollection qui représente toutes les cellules de la feuille de calcul.

LeICellulescollection fournit plusieurs méthodes de gestion des lignes et des colonnes dans une feuille de calcul. Certains d’entre eux sont discutés ci-dessous.

Insérer une ligne

Insérez une ligne dans la feuille de calcul à n’importe quel endroit en appelant leInsérer une ligne méthode de laICellules le recueil. LeInsérer une ligneLa méthode prend l’index de la ligne où la nouvelle ligne sera insérée.

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

Insertion de plusieurs lignes

Pour insérer plusieurs lignes dans une feuille de calcul, appelez leInsérer des lignes méthode de laICellules le recueil. LeInsérer des lignesLa méthode prend deux paramètres :

  • Index de ligne, l’index de la ligne à partir de laquelle les nouvelles lignes seront insérées.
  • Nombre de lignes, le nombre total de lignes qui doivent être insérées.
//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);

Suppression de plusieurs lignes

Pour supprimer plusieurs lignes d’une feuille de calcul, appelez leSupprimer les lignes méthode de laICellules le recueil. LeSupprimer les lignesLa méthode prend deux paramètres :

  • Index de ligne, l’index de la ligne à partir de laquelle les lignes seront supprimées.
  • Nombre de lignes, le nombre total de lignes qui doivent être supprimées.
//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);

Insérer une colonne

Les développeurs peuvent également insérer une colonne dans la feuille de calcul à n’importe quel endroit en appelant leInsérer une colonne méthode de laICellules le recueil.Insérer une colonneprend l’index de la colonne où la nouvelle colonne sera insérée.

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

Supprimer une colonne

Pour supprimer une colonne de la feuille de calcul à n’importe quel endroit, appelez leSupprimerColonne méthode de laICellules le recueil. LeSupprimerColonneprend l’index de la colonne à supprimer.

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