Ein Leitfaden zum Lesen und Schreiben von Excel-Dateien in C
Das Lesen und Erstellen von Excel-Dateien (XLS, XLSX und CSV) in C# und allen anderen .NET-Sprachen ist mit der IronXL-Softwarebibliothek von Iron Software ganz einfach.
IronXL erfordert nicht, dass Excel Interop auf Ihrem Server installiert ist. IronXL bietet eine schnellere und intuitivere API als Microsoft.Office.Interop.Excel.
IronXL funktioniert auf den folgenden Plattformen:
- .NET-Framework 4.6.2 und höher für Windows und Azure
- .NET Core 2 und höher für Windows, Linux, MacOS und Azure
-
.NET 5, .NET 6, .NET 7, .NET 8, Mono, Maui und Xamarin
IronXL installieren
Zuerst installieren Sie IronXL, indem Sie unser NuGet-Paket verwenden oder die DLL herunterladen. IronXL-Klassen finden Sie im IronXL
-Namensraum.
Am einfachsten lässt sich IronXL mit dem NuGet Package Manager für Visual-Studio installieren:
Der Paketname ist IronXL.Excel.
Install-Package IronXL.Excel
https://www.nuget.org/packages/ironxl.excel/
Lesen eines Excel-Dokuments
Mit IronXL kann das Extrahieren von Daten aus einer Excel-Datei in nur wenigen Codezeilen durchgeführt werden.
:path=/static-assets/excel/content-code-examples/get-started/get-started-1.cs
using IronXL;
// Supported spreadsheet formats for reading include: XLSX, XLS, CSV and TSV
WorkBook workBook = WorkBook.Load("data.xlsx");
WorkSheet workSheet = workBook.WorkSheets.First();
// Select cells easily in Excel notation and return the calculated value, date, text or formula
int cellValue = workSheet["A2"].IntValue;
// Read from Ranges of cells elegantly.
foreach (var cell in workSheet["A2:B10"])
{
Console.WriteLine("Cell {0} has value '{1}'", cell.AddressString, cell.Text);
}
Imports IronXL
' Supported spreadsheet formats for reading include: XLSX, XLS, CSV and TSV
Private workBook As WorkBook = WorkBook.Load("data.xlsx")
Private workSheet As WorkSheet = workBook.WorkSheets.First()
' Select cells easily in Excel notation and return the calculated value, date, text or formula
Private cellValue As Integer = workSheet("A2").IntValue
' Read from Ranges of cells elegantly.
For Each cell In workSheet("A2:B10")
Console.WriteLine("Cell {0} has value '{1}'", cell.AddressString, cell.Text)
Next cell
Neue Excel-Dokumente erstellen
IronXL bietet eine schnelle und einfache Schnittstelle zur Erstellung von Excel-Dokumenten mit C# oder VB.NET.
:path=/static-assets/excel/content-code-examples/get-started/get-started-2.cs
using IronXL;
// Create new Excel WorkBook document.
WorkBook workBook = WorkBook.Create(ExcelFileFormat.XLSX);
workBook.Metadata.Author = "IronXL";
// Add a blank WorkSheet
WorkSheet workSheet = workBook.CreateWorkSheet("main_sheet");
// Add data and styles to the new worksheet
workSheet["A1"].Value = "Hello World";
workSheet["A2"].Style.BottomBorder.SetColor("#ff6600");
workSheet["A2"].Style.BottomBorder.Type = IronXL.Styles.BorderType.Double;
// Save the excel file
workBook.SaveAs("NewExcelFile.xlsx");
Imports IronXL
' Create new Excel WorkBook document.
Private workBook As WorkBook = WorkBook.Create(ExcelFileFormat.XLSX)
workBook.Metadata.Author = "IronXL"
' Add a blank WorkSheet
Dim workSheet As WorkSheet = workBook.CreateWorkSheet("main_sheet")
' Add data and styles to the new worksheet
workSheet("A1").Value = "Hello World"
workSheet("A2").Style.BottomBorder.SetColor("#ff6600")
workSheet("A2").Style.BottomBorder.Type = IronXL.Styles.BorderType.Double
' Save the excel file
workBook.SaveAs("NewExcelFile.xlsx")
Exportieren als CSV, XLS, XLSX, JSON oder XML
IronXL ermöglicht es Ihnen auch, Daten in eine Vielzahl beliebter strukturierter Tabellenkalkulationsformate zu speichern oder zu exportieren.
:path=/static-assets/excel/content-code-examples/get-started/get-started-3.cs
// Export to many formats with fluent saving
workSheet.SaveAs("NewExcelFile.xls");
workSheet.SaveAs("NewExcelFile.xlsx");
workSheet.SaveAsCsv("NewExcelFile.csv");
workSheet.SaveAsJson("NewExcelFile.json");
workSheet.SaveAsXml("NewExcelFile.xml");
' Export to many formats with fluent saving
workSheet.SaveAs("NewExcelFile.xls")
workSheet.SaveAs("NewExcelFile.xlsx")
workSheet.SaveAsCsv("NewExcelFile.csv")
workSheet.SaveAsJson("NewExcelFile.json")
workSheet.SaveAsXml("NewExcelFile.xml")
Zellen und Bereiche formatieren
Sie können Formatierungen auf Excel-Zellen und -Bereiche mit dem IronXL.Range.Style-Objekt anwenden.
:path=/static-assets/excel/content-code-examples/get-started/get-started-4.cs
// Set cell's value and styles
workSheet["A1"].Value = "Hello World";
workSheet["A2"].Style.BottomBorder.SetColor("#ff6600");
workSheet["A2"].Style.BottomBorder.Type = IronXL.Styles.BorderType.Double;
' Set cell's value and styles
workSheet("A1").Value = "Hello World"
workSheet("A2").Style.BottomBorder.SetColor("#ff6600")
workSheet("A2").Style.BottomBorder.Type = IronXL.Styles.BorderType.Double
Sortierbereiche
Mit IronXL können Sie einen Bereich von Excel-Zellen einfach mit dem Range-Objekt sortieren.
:path=/static-assets/excel/content-code-examples/get-started/get-started-5.cs
using IronXL;
WorkBook workBook = WorkBook.Load("test.xls");
WorkSheet workSheet = workBook.WorkSheets.First();
// This is how we get range from Excel worksheet
Range range = workSheet["A2:A8"];
// Sort the range in the sheet
range.SortAscending();
workBook.Save();
Imports IronXL
Private workBook As WorkBook = WorkBook.Load("test.xls")
Private workSheet As WorkSheet = workBook.WorkSheets.First()
' This is how we get range from Excel worksheet
Private range As Range = workSheet("A2:A8")
' Sort the range in the sheet
range.SortAscending()
workBook.Save()
Formeln bearbeiten
Das Ändern einer Excel-Formel ist so einfach wie das Zuweisen eines Wertes, der mit einem "="-Zeichen beginnt. Die Formel wird sofort berechnet.
:path=/static-assets/excel/content-code-examples/get-started/get-started-6.cs
// Set a formula
workSheet["A1"].Value = "=SUM(A2:A10)";
// Get the calculated value
decimal sum = workSheet["A1"].DecimalValue;
' Set a formula
workSheet("A1").Value = "=SUM(A2:A10)"
' Get the calculated value
Dim sum As Decimal = workSheet("A1").DecimalValue
Warum IronXL wählen?
IronXL bietet eine entwicklerfreundliche API zum Lesen und Schreiben von Excel-Dokumenten in .NET.
Es funktioniert, ohne dass Microsoft Excel oder Excel Interop auf dem Server installiert sein müssen, was das Arbeiten mit Excel-Dateien schnell, leichtgewichtig und problemlos macht.
Vorwärts bewegen
Um mehr Funktionen und Möglichkeiten zu erkunden, empfehlen wir, das .NET API-Referenz, das ähnlich wie die MSDN-Dokumentation formatiert ist, zu überprüfen.