Einfügen und Löschen von Zeilen und Spalten einer Excel-Datei
Einführung
Unabhängig davon, ob Sie ein neues Arbeitsblatt von Grund auf neu erstellen oder an einem vorhandenen Arbeitsblatt arbeiten, müssen wir möglicherweise zusätzliche Zeilen oder Spalten hinzufügen, um mehr Daten aufzunehmen. Umgekehrt müssen wir möglicherweise auch Zeilen oder Spalten von bestimmten Positionen im Arbeitsblatt löschen. Um diese Anforderungen zu erfüllen, stellt Aspose.Cells einen sehr einfachsten Satz von Klassen und Methoden bereit, die unten besprochen werden.
Zeilen und Spalten verwalten
Aspose.Cells bietet eine KlasseArbeitsmappe , die eine Microsoft Excel-Datei darstellt. DasArbeitsmappe Klasse enthält aArbeitsblätter Sammlung, die den Zugriff auf jedes Arbeitsblatt in einer Excel-Datei ermöglicht. Ein Arbeitsblatt wird durch dargestelltArbeitsblatt Klasse. DasArbeitsblatt Klasse bietet aCellsAuflistung, die alle Zellen im Arbeitsblatt darstellt.
DasCellsDie Sammlung bietet mehrere Methoden zum Verwalten von Zeilen und Spalten in einem Arbeitsblatt. Einige davon werden unten besprochen.
Zeilen und Spalten einfügen
Zeile einfügen
Fügen Sie an beliebiger Stelle eine Zeile in das Arbeitsblatt ein, indem Sie die aufrufenZeile einfügen Methode derCells Sammlung. DasZeile einfügen-Methode nimmt den Index der Zeile, in der die neue Zeile eingefügt wird.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Creating a file stream containing the Excel file to be opened | |
FileStream fstream = new FileStream(dataDir + "book1.xls", FileMode.Open); | |
// Instantiating a Workbook object | |
// Opening the Excel file through the file stream | |
Workbook workbook = new Workbook(fstream); | |
// Accessing the first worksheet in the Excel file | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Inserting a row into the worksheet at 3rd position | |
worksheet.Cells.InsertRow(2); | |
// Saving the modified Excel file | |
workbook.Save(dataDir + "output.out.xls"); | |
// Closing the file stream to free all resources | |
fstream.Close(); |
Mehrere Zeilen einfügen
Um mehrere Zeilen in ein Arbeitsblatt einzufügen, rufen Sie die aufZeilen einfügen Methode derCells Sammlung. DasZeilen einfügenDie Methode benötigt zwei Parameter:
- Zeilenindex, der Index der Zeile, ab der die neuen Zeilen eingefügt werden.
- Anzahl der Zeilen, die Gesamtzahl der Zeilen, die eingefügt werden müssen.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Creating a file stream containing the Excel file to be opened | |
FileStream fstream = new FileStream(dataDir + "book1.xls", FileMode.Open); | |
// Instantiating a Workbook object | |
// Opening the Excel file through the file stream | |
Workbook workbook = new Workbook(fstream); | |
// Accessing the first worksheet in the Excel file | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Inserting 10 rows into the worksheet starting from 3rd row | |
worksheet.Cells.InsertRows(2, 10); | |
// Saving the modified Excel file | |
workbook.Save(dataDir + "output.out.xls"); | |
// Closing the file stream to free all resources | |
fstream.Close(); |
Zeile mit Formatierung einfügen
Um eine Zeile mit Formatierungsoptionen einzufügen, verwenden Sie dieZeilen einfügenÜberlastung, die dauertEinfügeoptionen als Parameter. Stellen Sie die einCopyFormatType Eigentum vonEinfügeoptionen Klasse mitCopyFormatType Aufzählung. DasCopyFormatTypeDie Aufzählung hat drei Mitglieder, wie unten aufgeführt.
- SameAsAbove: Formatiert die Zeile genauso wie die obige Zeile.
- SameAsBelow: Formatiert die Zeile genauso wie die untere Zeile.
- Löschen: Löscht die Formatierung.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Creating a file stream containing the Excel file to be opened | |
FileStream fstream = new FileStream(dataDir + "book1.xls", FileMode.Open); | |
// Instantiating a Workbook object | |
// Opening the Excel file through the file stream | |
Workbook workbook = new Workbook(fstream); | |
// Accessing the first worksheet in the Excel file | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Setting Formatting options | |
InsertOptions insertOptions = new InsertOptions(); | |
insertOptions.CopyFormatType = CopyFormatType.SameAsAbove; | |
// Inserting a row into the worksheet at 3rd position | |
worksheet.Cells.InsertRows(2, 1, insertOptions); | |
// Saving the modified Excel file | |
workbook.Save(dataDir + "InsertingARowWithFormatting.out.xls"); | |
// Closing the file stream to free all resources | |
fstream.Close(); |
Spalte einfügen
Entwickler können auch an beliebiger Stelle eine Spalte in das Arbeitsblatt einfügen, indem sie die aufrufenSpalte einfügen Methode derCellsSammlung. DasSpalte einfügen-Methode übernimmt den Index der Spalte, in die die neue Spalte eingefügt wird.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Creating a file stream containing the Excel file to be opened | |
FileStream fstream = new FileStream(dataDir + "book1.xls", FileMode.Open); | |
// Instantiating a Workbook object | |
// Opening the Excel file through the file stream | |
Workbook workbook = new Workbook(fstream); | |
// Accessing the first worksheet in the Excel file | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Inserting a column into the worksheet at 2nd position | |
worksheet.Cells.InsertColumn(1); | |
// Saving the modified Excel file | |
workbook.Save(dataDir + "output.out.xls"); | |
// Closing the file stream to free all resources | |
fstream.Close(); |
Zeilen und Spalten löschen
Mehrere Zeilen löschen
Um mehrere Zeilen aus einem Arbeitsblatt zu löschen, rufen Sie die aufZeilen löschen Methode derCells Sammlung. DasZeilen löschenDie Methode benötigt zwei Parameter:
- Zeilenindex, der Index der Zeile, aus der die Zeilen gelöscht werden.
- Anzahl der Zeilen, die Gesamtzahl der Zeilen, die gelöscht werden müssen.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Creating a file stream containing the Excel file to be opened | |
FileStream fstream = new FileStream(dataDir + "Book1.xlsx", FileMode.OpenOrCreate); | |
// Instantiating a Workbook object | |
// Opening the Excel file through the file stream | |
Workbook workbook = new Workbook(fstream); | |
// Accessing the first worksheet in the Excel file | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Deleting 10 rows from the worksheet starting from 3rd row | |
worksheet.Cells.DeleteRows(2, 10); | |
// Saving the modified Excel file | |
workbook.Save(dataDir + "output.xlsx"); | |
// Closing the file stream to free all resources | |
fstream.Close(); |
Löschen Sie eine Spalte
Um eine Spalte an einer beliebigen Stelle aus dem Arbeitsblatt zu löschen, rufen Sie die aufSpalte löschen Methode derCells Sammlung. DasSpalte löschen-Methode nimmt den Index der zu löschenden Spalte.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Creating a file stream containing the Excel file to be opened | |
FileStream fstream = new FileStream(dataDir + "Book1.xlsx", FileMode.Open); | |
// Instantiating a Workbook object | |
// Opening the Excel file through the file stream | |
Workbook workbook = new Workbook(fstream); | |
// Accessing the first worksheet in the Excel file | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Deleting a column from the worksheet at 5th position | |
worksheet.Cells.DeleteColumn(4); | |
// Saving the modified Excel file | |
workbook.Save(dataDir + "output.xlsx"); | |
// Closing the file stream to free all resources | |
fstream.Close(); |