TExcel 2.0.1
There is a newer version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package TExcel --version 2.0.1
NuGet\Install-Package TExcel -Version 2.0.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="TExcel" Version="2.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="TExcel" Version="2.0.1" />
<PackageReference Include="TExcel" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add TExcel --version 2.0.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: TExcel, 2.0.1"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package TExcel@2.0.1
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=TExcel&version=2.0.1
#tool nuget:?package=TExcel&version=2.0.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
<h1 align="center">TExcel - 高性能 .NET Excel 读写库</h1>
<div align="center">
轻量级、高性能、低内存占用的 Excel 读写库,支持 AOT 编译
ThingsGateway 生态系统中的 Excel 组件
</div>
中文
项目简介
TExcel 是一个为 .NET 平台设计的轻量级、高性能 Excel 读写库。它提供了简洁流畅的 API,支持流式操作,无需任何第三方依赖,并针对高性能场景进行了深度优化,完美支持 AOT 编译。
核心特性
- AOT 支持:完美支持 AOT 编译,适用于原生应用场景
- 零依赖:无需任何第三方 Excel 库依赖
- 高性能:流式读写,低内存占用
- 多格式支持:支持实体列表、字典、多列数据等多种数据格式
- 多 Sheet 支持:支持读取和写入多个 Sheet
- 异步支持:完整的同步/异步 API
性能测试
TExcel 与 MiniExcel 的性能对比测试结果:
BenchmarkDotNet v0.15.8, Windows 11 (10.0.26200.8037/25H2/2025Update/HudsonValley2)
Intel Core Ultra 9 285H 2.90GHz, 1 CPU, 16 logical and 16 physical cores
.NET SDK 10.0.201
[Host] : .NET 8.0.25 (8.0.25, 8.0.2526.11203), X64 RyuJIT x86-64-v3
Job-YFEFPZ : .NET 8.0.25 (8.0.25, 8.0.2526.11203), X64 RyuJIT x86-64-v3
ShortRun : .NET 8.0.25 (8.0.25, 8.0.2526.11203), X64 RyuJIT x86-64-v3
WarmupCount=3
| Method | Job | IterationCount | LaunchCount | Mean | Error | StdDev | Min | Max | Gen0 | Gen1 | Gen2 | Allocated |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 'TExcel - Write 100 rows' | Job-YFEFPZ | 10 | Default | 1,059.1 μs | 65.15 μs | 43.09 μs | 1,004.2 μs | 1,146.6 μs | 21.4844 | 10.7422 | 8.7891 | 1821.97 KB |
| 'MiniExcel - Write 100 rows' | Job-YFEFPZ | 10 | Default | 3,716.7 μs | 369.08 μs | 219.63 μs | 3,476.1 μs | 4,180.2 μs | 132.8125 | 125.0000 | 78.1250 | 15683.2 KB |
| 'TExcel - Write 10,000 rows' | Job-YFEFPZ | 10 | Default | 22,187.5 μs | 681.30 μs | 450.64 μs | 21,482.6 μs | 22,902.5 μs | 281.2500 | 93.7500 | - | 11153.55 KB |
| 'MiniExcel - Write 10,000 rows' | Job-YFEFPZ | 10 | Default | 27,250.0 μs | 1,833.84 μs | 1,212.97 μs | 24,390.4 μs | 28,672.8 μs | 1687.5000 | 62.5000 | - | 36436.56 KB |
| 'TExcel - Write 100,000 rows' | Job-YFEFPZ | 10 | Default | 213,537.9 μs | 4,088.91 μs | 2,433.25 μs | 208,856.6 μs | 216,787.3 μs | 2666.6667 | 333.3333 | - | 94062.6 KB |
| 'MiniExcel - Write 100,000 rows' | Job-YFEFPZ | 10 | Default | 244,117.3 μs | 19,421.45 μs | 12,846.09 μs | 219,797.5 μs | 257,877.9 μs | 17666.6667 | - | - | 232266.4 KB |
| 'TExcel - Read 100 rows' | Job-YFEFPZ | 10 | Default | 284.7 μs | 34.18 μs | 22.61 μs | 222.1 μs | 300.8 μs | 14.6484 | 1.9531 | - | 181.19 KB |
| 'MiniExcel - Read 100 rows' | Job-YFEFPZ | 10 | Default | 1,691.5 μs | 68.62 μs | 40.83 μs | 1,636.4 μs | 1,761.3 μs | 58.5938 | 27.3438 | - | 722.27 KB |
| 'TExcel - Read 10,000 rows' | Job-YFEFPZ | 10 | Default | 24,746.0 μs | 1,790.22 μs | 1,184.12 μs | 22,314.0 μs | 25,537.4 μs | 812.5000 | 250.0000 | 62.5000 | 10072.59 KB |
| 'MiniExcel - Read 10,000 rows' | Job-YFEFPZ | 10 | Default | 54,524.2 μs | 500.55 μs | 261.80 μs | 54,119.4 μs | 54,895.3 μs | 4600.0000 | 1000.0000 | 200.0000 | 55544.65 KB |
| 'TExcel - Read 100,000 rows' | Job-YFEFPZ | 10 | Default | 266,923.7 μs | 5,265.72 μs | 3,482.95 μs | 259,846.8 μs | 271,873.1 μs | 8000.0000 | 3000.0000 | 1000.0000 | 102740.44 KB |
| 'MiniExcel - Read 100,000 rows' | Job-YFEFPZ | 10 | Default | 551,289.8 μs | 13,745.96 μs | 9,092.10 μs | 539,301.3 μs | 567,266.6 μs | 47000.0000 | 11000.0000 | 2000.0000 | 558125.63 KB |
| 'TExcel - Read as Dictionary 10,000 rows' | Job-YFEFPZ | 10 | Default | 31,506.2 μs | 446.76 μs | 265.86 μs | 30,932.3 μs | 31,734.8 μs | 1156.2500 | 781.2500 | 187.5000 | 12884.47 KB |
| 'MiniExcel - Read as Dictionary 10,000 rows' | Job-YFEFPZ | 10 | Default | 61,709.8 μs | 3,023.52 μs | 1,999.87 μs | 58,652.3 μs | 64,536.6 μs | 4750.0000 | 1250.0000 | 250.0000 | 56571.91 KB |
| 'TExcel - Write Dictionary 10,000 rows' | Job-YFEFPZ | 10 | Default | 18,441.1 μs | 1,818.59 μs | 1,202.88 μs | 16,855.4 μs | 20,007.1 μs | 156.2500 | 31.2500 | - | 9981.42 KB |
| 'MiniExcel - Write Dictionary 10,000 rows' | Job-YFEFPZ | 10 | Default | 26,600.6 μs | 776.30 μs | 513.47 μs | 25,892.5 μs | 27,354.7 μs | 1625.0000 | 93.7500 | 31.2500 | 35212.48 KB |
| 'TExcel - Streaming Read 100,000 rows (count only)' | Job-YFEFPZ | 10 | Default | 229,602.2 μs | 28,054.18 μs | 18,556.10 μs | 181,982.3 μs | 245,431.7 μs | 7000.0000 | 1000.0000 | - | 100683.77 KB |
| 'MiniExcel - Streaming Read 100,000 rows (count only)' | Job-YFEFPZ | 10 | Default | 483,627.9 μs | 14,598.97 μs | 9,656.31 μs | 472,547.4 μs | 499,506.6 μs | 45000.0000 | - | - | 556043.36 KB |
| 'TExcel - Write 100 rows' | ShortRun | 3 | 1 | 1,113.4 μs | 119.02 μs | 6.52 μs | 1,109.6 μs | 1,121.0 μs | 15.6250 | 5.8594 | 3.9063 | 1821.98 KB |
| 'MiniExcel - Write 100 rows' | ShortRun | 3 | 1 | 4,374.9 μs | 3,531.58 μs | 193.58 μs | 4,193.6 μs | 4,578.7 μs | 93.7500 | 82.0313 | 54.6875 | 15682.95 KB |
| 'TExcel - Write 10,000 rows' | ShortRun | 3 | 1 | 22,604.7 μs | 7,683.89 μs | 421.18 μs | 22,277.5 μs | 23,079.9 μs | 250.0000 | 62.5000 | - | 11126.35 KB |
| 'MiniExcel - Write 10,000 rows' | ShortRun | 3 | 1 | 36,016.0 μs | 15,032.68 μs | 823.99 μs | 35,084.8 μs | 36,650.6 μs | 1750.0000 | 62.5000 | 31.2500 | 36436.36 KB |
| 'TExcel - Write 100,000 rows' | ShortRun | 3 | 1 | 233,939.4 μs | 52,480.95 μs | 2,876.66 μs | 231,772.8 μs | 237,203.2 μs | 2666.6667 | 333.3333 | - | 94007.51 KB |
| 'MiniExcel - Write 100,000 rows' | ShortRun | 3 | 1 | 285,767.6 μs | 257,629.48 μs | 14,121.54 μs | 274,296.7 μs | 301,539.6 μs | 17666.6667 | - | - | 232264.83 KB |
| 'TExcel - Read 100 rows' | ShortRun | 3 | 1 | 213.8 μs | 45.71 μs | 2.51 μs | 211.1 μs | 216.1 μs | 14.6484 | 1.9531 | - | 181.96 KB |
| 'MiniExcel - Read 100 rows' | ShortRun | 3 | 1 | 2,071.7 μs | 15,930.01 μs | 873.18 μs | 1,363.8 μs | 3,047.4 μs | 58.5938 | 27.3438 | - | 720.73 KB |
| 'TExcel - Read 10,000 rows' | ShortRun | 3 | 1 | 25,644.7 μs | 5,848.91 μs | 320.60 μs | 25,284.8 μs | 25,899.7 μs | 812.5000 | 250.0000 | 62.5000 | 10072.74 KB |
| 'MiniExcel - Read 10,000 rows' | ShortRun | 3 | 1 | 53,639.1 μs | 76,406.11 μs | 4,188.08 μs | 48,810.7 μs | 56,288.5 μs | 4700.0000 | 1300.0000 | 200.0000 | 55591.38 KB |
| 'TExcel - Read 100,000 rows' | ShortRun | 3 | 1 | 256,210.4 μs | 68,315.20 μs | 3,744.59 μs | 251,914.2 μs | 258,781.5 μs | 8000.0000 | 3000.0000 | 1000.0000 | 102756.24 KB |
| 'MiniExcel - Read 100,000 rows' | ShortRun | 3 | 1 | 518,052.4 μs | 407,624.52 μs | 22,343.28 μs | 494,856.1 μs | 539,431.7 μs | 47000.0000 | 11000.0000 | 2000.0000 | 558064.09 KB |
| 'TExcel - Read as Dictionary 10,000 rows' | ShortRun | 3 | 1 | 34,998.4 μs | 46,704.90 μs | 2,560.05 μs | 32,252.7 μs | 37,319.8 μs | 1142.8571 | 785.7143 | 214.2857 | 12884.46 KB |
| 'MiniExcel - Read as Dictionary 10,000 rows' | ShortRun | 3 | 1 | 67,282.7 μs | 71,449.46 μs | 3,916.39 μs | 62,792.2 μs | 69,991.3 μs | 4875.0000 | 1375.0000 | 375.0000 | 56492.41 KB |
| 'TExcel - Write Dictionary 10,000 rows' | ShortRun | 3 | 1 | 22,142.3 μs | 10,135.84 μs | 555.58 μs | 21,561.2 μs | 22,668.2 μs | 156.2500 | 31.2500 | - | 9927.77 KB |
| 'MiniExcel - Write Dictionary 10,000 rows' | ShortRun | 3 | 1 | 25,572.2 μs | 19,225.55 μs | 1,053.82 μs | 24,893.7 μs | 26,786.2 μs | 1625.0000 | 93.7500 | 31.2500 | 35204.37 KB |
| 'TExcel - Streaming Read 100,000 rows (count only)' | ShortRun | 3 | 1 | 231,945.9 μs | 269,256.84 μs | 14,758.88 μs | 215,370.8 μs | 243,664.7 μs | 7000.0000 | 1000.0000 | - | 100690.75 KB |
| 'MiniExcel - Streaming Read 100,000 rows (count only)' | ShortRun | 3 | 1 | 471,080.6 μs | 224,474.12 μs | 12,304.18 μs | 462,134.7 μs | 485,112.4 μs | 45000.0000 | - | - | 556044.45 KB |
TExcel 在读写速度和内存占用方面均表现优异,内存占用仅为 MiniExcel 的 1/5 到 1/3。
快速开始
安装 NuGet 包
dotnet add package TExcel
写入 Excel
实体列表写入:
using TExcel;
public class User
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}
var users = new List<User>
{
new User { Id = 1, Name = "张三", Age = 25 },
new User { Id = 2, Name = "李四", Age = 30 }
};
Excel.SaveAs("output.xlsx", users);
字典列表写入:
var data = new List<Dictionary<string, object?>>
{
new Dictionary<string, object?> { ["Id"] = 1, ["Name"] = "张三" },
new Dictionary<string, object?> { ["Id"] = 2, ["Name"] = "李四" }
};
Excel.SaveAs("output.xlsx", data);
多 Sheet 写入:
using var writer = Excel.CreateWriter("output.xlsx");
writer.WriteSheet(users, "Users");
writer.WriteSheet(orders, "Orders");
writer.Complete();
读取 Excel
实体列表读取:
var users = Excel.Query<User>("input.xlsx").ToList();
字典列表读取:
var data = Excel.QueryAsDictionary("input.xlsx").ToList();
流式读取(大数据量场景):
foreach (var user in Excel.Query<User>("large_file.xlsx"))
{
// 逐行处理,低内存占用
}
异步读取:
await foreach (var user in Excel.QueryAsync<User>("input.xlsx"))
{
// 异步逐行处理
}
获取 Sheet 信息
var sheets = Excel.GetSheets("input.xlsx");
foreach (var sheet in sheets)
{
Console.WriteLine($"Sheet[{sheet.Index}]: {sheet.Name}");
}
列特性配置
使用 ExcelColumnAttribute 自定义列名和顺序:
public class User
{
[ExcelColumn("用户ID", Order = 0)]
public int Id { get; set; }
[ExcelColumn("用户名", Order = 1)]
public string Name { get; set; }
[ExcelColumn("创建时间", Format = "yyyy-MM-dd HH:mm:ss")]
public DateTime CreateTime { get; set; }
[ExcelColumn(Ignore = true)]
public string? Password { get; set; }
}
API 概览
| 方法 | 说明 |
|---|---|
Excel.SaveAs(path, data) |
保存数据到 Excel 文件 |
Excel.Query<T>(path) |
从 Excel 读取实体列表 |
Excel.QueryAsDictionary(path) |
从 Excel 读取字典列表 |
Excel.GetSheets(path) |
获取所有 Sheet 信息 |
Excel.CreateWriter(path) |
创建写入器(支持多 Sheet) |
Excel.CreateReader(path) |
创建读取器 |
所有方法均支持异步版本和 Stream 参数。
项目结构
TExcel/
├── src/TExcel/
│ ├── Core/
│ │ ├── Excel.cs # 静态便捷方法
│ │ ├── ExcelReader.cs # 读取器实现
│ │ ├── ExcelWriter.cs # 写入器实现
│ │ ├── IExcelReader.cs # 读取器接口
│ │ ├── IExcelWriter.cs # 写入器接口
│ │ ├── SheetInfo.cs # Sheet 信息
│ │ └── ExcelColumnAttribute.cs # 列特性
│ └── Internal/
│ ├── CellReferenceHelper.cs
│ ├── OpenXmlConstants.cs
│ └── SharedStringsTable.cs
├── benchmark/TExcel.Benchmark/ # 性能测试
└── test/TExcel.Test/ # 单元测试
运行性能测试
cd benchmark/TExcel.Benchmark
dotnet run -c Release
许可证
相关链接
- Gitee: https://gitee.com/diego2098/ThingsGateway
- GitHub: https://github.com/kimdiego2098/ThingsGateway
- 文档: https://thingsgateway.cn/
- QQ群: 605534569
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | 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 is compatible. 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. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
NuGet packages (1)
Showing the top 1 NuGet packages that depend on TExcel:
| Package | Downloads |
|---|---|
|
ThingsGatewayRuntime.Admin
Package Description |
GitHub repositories
This package is not used by any popular GitHub repositories.