Einfügen, Löschen von Zeilen und Spalten

Einführung

Ob beim Erstellen eines neuen Arbeitsblatts von Grund auf oder beim Arbeiten an einem vorhandenen Arbeitsblatt, wir können zusätzliche Zeilen oder Spalten hinzufügen, um mehr Daten aufzunehmen. Im Gegensatz dazu müssen wir möglicherweise auch Zeilen oder Spalten von bestimmten Positionen im Arbeitsblatt löschen. Um diese Anforderungen zu erfüllen, bietet Aspose.Cells eine sehr einfache Reihe von Klassen und Methoden, die unten diskutiert werden.

Verwalten von Zeilen und Spalten

Aspose.Cells bietet eine Klasse, Workbook, die eine Microsoft Excel-Datei darstellt. Die Klasse Workbook enthält eine Worksheets -Sammlung, die den Zugriff auf jedes Arbeitsblatt in einer Excel-Datei ermöglicht. Ein Arbeitsblatt wird durch die Klasse Worksheet dargestellt. Die Klasse Worksheet bietet eine Cells -Sammlung, die alle Zellen im Arbeitsblatt repräsentiert.

Die Cells -Sammlung bietet verschiedene Methoden zum Verwalten von Zeilen und Spalten in einem Arbeitsblatt. Einige davon werden unten diskutiert.

Eine Zeile einfügen

Fügen Sie eine Zeile in das Arbeitsblatt an einer beliebigen Stelle ein, indem Sie die InsertRow -Methode der Cells -Sammlung aufrufen. Die InsertRow -Methode übernimmt den Index der Zeile, an der die neue Zeile eingefügt werden soll.

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

Einfügen mehrerer Zeilen

Um mehrere Zeilen in ein Arbeitsblatt einzufügen, rufen Sie die InsertRows -Methode der Cells -Sammlung auf. Die InsertRows -Methode verwendet zwei Parameter:

  • Zeilenindex, der Index der Zeile, ab der die neuen Zeilen eingefügt werden.
  • Anzahl der Zeilen, die insgesamt eingefügt werden müssen.
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();

Mehrere Zeilen löschen

Um mehrere Zeilen aus einem Arbeitsblatt zu löschen, rufen Sie die DeleteRows -Methode der Cells -Sammlung auf. Die DeleteRows -Methode verwendet zwei Parameter:

  • Zeilenindex, der Index der Zeile, ab der die Zeilen gelöscht werden.
  • Anzahl der Zeilen, die insgesamt gelöscht werden müssen.
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();

Eine Spalte einfügen

Entwickler können auch eine Spalte in das Arbeitsblatt an einer beliebigen Stelle einfügen, indem sie die InsertColumn -Methode der Cells -Sammlung aufrufen. Die InsertColumn -Methode übernimmt den Index der Spalte, an dem die neue Spalte eingefügt werden soll.

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

Eine Spalte löschen

Um eine Spalte aus dem Arbeitsblatt an einer beliebigen Stelle zu löschen, rufen Sie die DeleteColumn -Methode der Cells -Sammlung auf. Die DeleteColumn -Methode übernimmt den Index der zu löschenden Spalte.

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