EasyExcel.Tools
4.0.0.3
dotnet add package EasyExcel.Tools --version 4.0.0.3
NuGet\Install-Package EasyExcel.Tools -Version 4.0.0.3
<PackageReference Include="EasyExcel.Tools" Version="4.0.0.3" />
<PackageVersion Include="EasyExcel.Tools" Version="4.0.0.3" />
<PackageReference Include="EasyExcel.Tools" />
paket add EasyExcel.Tools --version 4.0.0.3
#r "nuget: EasyExcel.Tools, 4.0.0.3"
#:package EasyExcel.Tools@4.0.0.3
#addin nuget:?package=EasyExcel.Tools&version=4.0.0.3
#tool nuget:?package=EasyExcel.Tools&version=4.0.0.3
EasyExcel - A Simple Excel Utility for C#
EasyExcel is a lightweight utility for reading from and writing to Excel files in C#. It is built using only the built-in features of .NET Core(OpenXML), without relying on any external libraries or packages. This makes it a great choice for projects where you want to avoid additional dependencies.
Features
- Read Excel Files: Convert Excel files into a list of strongly-typed objects.
- Write Excel Files: Export a list of objects or a
DataTableto an Excel file. - No External Dependencies: Uses only the built-in features of .NET Core.
- Flexible: Supports multiple sheets and custom column names via attributes.
- Lightweight: Minimal overhead and easy to integrate into existing projects.
Installation
Since EasyExcel is a single-file utility, you can simply copy the EasyExcel.cs file into your project and start using it.
Usage
Reading from Excel
To read data from an Excel file into a list of objects, use the ReadExcelFile method:
[ExcelSheetName(SheetName ="Sheet1")]
public class Person
{
[ExcelColumnName(ColumnName = "CustomerName")]
public string Name { get; set; }
[ExcelColumnName(ColumnName = "CustomerAge")]
public int Age { get; set; }
[ExcelColumnName(ColumnName = "BirthDate")]
public DateTime BirthDate { get; set; }
}
using (var stream = new FileStream("path_to_excel_file.xlsx", FileMode.Open))
{
var people = EasyExcel.ReadExcelFile<Person>(stream);
foreach (var person in people)
{
Console.WriteLine($"Name: {person.Name}, Age: {person.Age}, BirthDate: {person.BirthDate}");
}
}
Writing to Excel
To write a list of objects to an Excel file, use the ExportToExcel method:
public class Person
{
[ExcelExport(DisplayName = "Person Name")]
public string Name { get; set; }
[ExcelExport(DisplayName = "Person Age")]
public int Age { get; set; }
public DateTime BirthDate { get; set; }
}
var people = new List<Person>
{
new Person { Name = "John Doe", Age = 30, BirthDate = new DateTime(1990, 1, 1) },
new Person { Name = "Jane Doe", Age = 25, BirthDate = new DateTime(1995, 5, 5) }
};
var excelBytes = EasyExcel.ExportToExcel(people, "People");
File.WriteAllBytes("output.xlsx", excelBytes);
Customizing Column Names and Sheet Names
You can customize the column names and sheet names using attributes:
[ExcelSheetName("Employees")]
public class Employee
{
[ExcelColumnName("Full Name")]
public string Name { get; set; }
[ExcelColumnName("Age in Years")]
public int Age { get; set; }
[ExcelColumnName("Date of Birth")]
public DateTime BirthDate { get; set; }
}
Handling Multiple Sheets
You can read data from multiple sheets in a single Excel file:
using (var stream = new FileStream("path_to_excel_file.xlsx", FileMode.Open))
{
var (people, employees) = EasyExcel.ReadExcelFile<Person, Employee>(stream);
}
How Using in Api
You can use in api method like this:
var result = EasyExcel.ExportToExcel(oNewList);
FileContentResult content = new FileContentResult(result, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
return content;
Attributes
ExcelColumnNameAttribute: Specifies the column name in the Excel file.
ExcelSheetNameAttribute: Specifies the sheet name in the Excel file.
ExcelExportAttribute: Specifies whether a property should be included in the export and its display name.
Limitations
Excel Format: Only supports .xlsx files (Excel 2007 and later).
Data Types: Supports basic data types like int, double, decimal, bool, DateTime, and string.
Contributing
Contributions are welcome! If you find a bug or have a feature request, please open an issue or submit a pull request.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 is compatible. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp3.0 is compatible. netcoreapp3.1 is compatible. |
| .NET Standard | netstandard2.1 is compatible. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETCoreApp 3.0
- DocumentFormat.OpenXml (>= 3.2.0)
-
.NETCoreApp 3.1
- DocumentFormat.OpenXml (>= 3.2.0)
-
.NETStandard 2.1
- DocumentFormat.OpenXml (>= 3.2.0)
-
net5.0
- DocumentFormat.OpenXml (>= 3.2.0)
-
net6.0
- DocumentFormat.OpenXml (>= 3.2.0)
-
net7.0
- DocumentFormat.OpenXml (>= 3.2.0)
-
net8.0
- DocumentFormat.OpenXml (>= 3.2.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.