Chsword.Excel2Object
2.2.1
.NET 6.0
This package targets .NET 6.0. The package is compatible with this framework or higher.
.NET Standard 2.0
This package targets .NET Standard 2.0. The package is compatible with this framework or higher.
.NET Framework 4.7.2
This package targets .NET Framework 4.7.2. The package is compatible with this framework or higher.
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 Chsword.Excel2Object --version 2.2.1
NuGet\Install-Package Chsword.Excel2Object -Version 2.2.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="Chsword.Excel2Object" Version="2.2.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Chsword.Excel2Object" Version="2.2.1" />
<PackageReference Include="Chsword.Excel2Object" />
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 Chsword.Excel2Object --version 2.2.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Chsword.Excel2Object, 2.2.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 Chsword.Excel2Object@2.2.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=Chsword.Excel2Object&version=2.2.1
#tool nuget:?package=Chsword.Excel2Object&version=2.2.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Excel2Object
Excel 与 .NET 对象互相转换 / Excel convert to .NET Object and vice versa.
English | 中文
平台支持
安装 NuGet
PM> Install-Package Chsword.Excel2Object
或使用 .NET CLI:
dotnet add package Chsword.Excel2Object
命令行工具 excel2obj
dotnet tool install -g Chsword.Excel2Object.Cli
excel2obj convert orders.xlsx --output orders.json --typed # Excel -> JSON
excel2obj convert orders.json --output orders.xlsx # JSON -> Excel
excel2obj generate-model orders.xlsx --class Order # 由表头生成带 [ExcelTitle] 的 C# 模型类
详见 Chsword.Excel2Object.Cli/README.md。
发布说明和路线图
暂不支持的特性
- CLI 工具 ✅ v2.2.1 新增 -
dotnet tool install -g Chsword.Excel2Object.Cli,查看 Chsword.Excel2Object.Cli/README.md - 支持自动列宽 ✅ v2.0.4 新增
- 支持 Excel 日期/日期时间/时间格式 ✅ v2.0.4 新增 - 查看 DateTimeFormats.md
- 公式列引用同一工作簿的其他 sheet ✅ v2.1.0 新增 - 查看 ExcelFunctions.md
发布说明
- 2026.09.11 - v2.2.1
- 🔧 修复命令行工具包体超过 NuGet 250 MB 上限导致 2.2.0 未能发布的问题:CLI 仅保留 net8.0 并剔除原生
.pdb(库代码无变更)
- 2026.09.11 - v2.2.0
- ✨ 新增: 命令行工具
excel2obj(dotnet tool install -g Chsword.Excel2Object.Cli):Excel ↔ JSON 转换、批量转换、由表头生成带[ExcelTitle]的模型类 - 查看 Chsword.Excel2Object.Cli/README.md(#9) - ✨ 新增: 公式列可通过模型属性引用列:
options.FormulaColumns.Add<Order>("Total", (c, m) => m.Price * m.Qty),编译期检查属性名,引用未导出的属性会抛出异常 - 查看 ExcelFunctions.md(#22) - ✨ 改进: 导出时数值列写成数字单元格、bool 列写成布尔单元格、null/DBNull 写成空白单元格(此前一律写成文本);字符串列仍为文本,前导零不会丢失
- 🐛 修复公式中运算符优先级导致缺少括号的问题,如
c["A"] * (c["B"] + c["C"])此前生成A2*B2+C2 - ✨ 改进: 公式中引用当前 sheet 不存在的列标题时抛出
Excel2ObjectException,不再静默回退
- 2026.09.08 - v2.1.0
- ✨ 新增: 公式列支持跨 sheet 引用:
c.Sheet("Products")["Price", 2]、.Matrix(...)、.Columns(...),可直接用于VLOOKUP等函数;另新增当前 sheet 的整列引用c.Columns("A列", "D列")- 查看 ExcelFunctions.md - ✨ 改进: 公式中引用不存在的列或 sheet 时抛出
Excel2ObjectException(附公式列名),不再生成无效公式 - ✨ 更新: NPOI 到 2.8.0(NPOI 2.8 起附带 OSMF EULA,本库已在 csproj 中声明接受;源代码仍为 Apache-2.0)
- 🔒 修复传递依赖 System.Security.Cryptography.Xml 的安全漏洞(钉到 8.0.4)
- 🧹 移除未使用的 SixLabors.ImageSharp 依赖;补全
FormulaColumnsCollection.CopyTo;ColumnValue.Equals/GetHashCode不再抛异常 - 🔧 CI 与测试迁移到 .NET 10;修复发布流程中 GitHub Release 创建失败的问题
- 2025.10.11 - v2.0.5
- 🔧
release.ps1支持自动递增补丁版本号与-Help(库代码无变更)
- 2025.10.11 - v2.0.4
- ℹ️ 自 2.0.0.211 之后首个发布到 NuGet 的 2.0.x 版本(v2.0.1–v2.0.3 未曾发布),包含以下全部改动:
- ✨ 新增: 基于内容的自动列宽调整
- 自动计算最优列宽
- 支持最小和最大宽度限制
- 正确处理中文/Unicode 字符
- 可通过
ExcelExporterOptions配置
- ✨ 新增: 全面的日期/时间格式支持(56 种格式)- 查看 DateTimeFormats.md
- ISO 8601 格式(支持时区和毫秒)
- 多种日期分隔符(横线、斜线、点号)
- 12 小时制和 24 小时制时间格式
- 仅时间格式
- 区域格式支持(美国、欧洲等)
- 向后兼容现有的中文日期格式(年月日)
- ✨ 更新: NPOI 到 2.7.5
- ✨ 更新: SixLabors.ImageSharp 到 3.1.11(修复安全漏洞)
- 🔧 GitHub Actions CI 取代 AppVeyor;新增 tag 触发的自动发布流程与
release.ps1一键发布脚本
- 2024.10.21
- 更新 SixLabors.ImageSharp 到 2.1.9
- 测试 .NET 8.0
- 2024.05.10
- 支持 .NET 8.0 / .NET 6.0 / .NET Standard 2.1 / .NET Standard 2.0 / .NET Framework 4.7.2
- 清理已弃用的库
- 2023.11.02
- 支持列标题映射 - Issue39DynamicMappingTitle.cs
- 2023.07.31
- 支持 DateTime 和 Nullable<DateTime> 格式,如
[ExcelColumn("Title",Format="yyyy-MM-dd HH:mm:ss")]
- 2023.03.26
- 支持列标题中的特殊符号 #37 - Issue37SpecialCharTest.cs
- 2023.02.20
- 支持平台:.NET Standard 2.0/2.1、.NET 6.0、.NET Framework 4.7.2
- 2022.03.19
- 支持 ExcelImporterOptions,跳过行 - Issue32SkipLineImport.cs
- 修复超类属性 bug - Issue31SuperClass.cs
- 2021.11.4
- 多 sheet 支持 - Pr28MultipleSheetTest.cs
- 2021.10.23
- 修复 Nullable DateTime bug @SunBrook
- 2021.10.22
- 支持 Nullable 类型 - Pr24NullableTest.cs @SunBrook
- 2021.5.28
- 支持表头和单元格样式,新增列的 [ExcelColumnAttribute]
- 支持公式 - ExcelFunctions.md
var list = new List<Pr20Model>
{
new Pr20Model
{
Fullname = "AAA", Mobile = "123456798123"
},
new Pr20Model
{
Fullname = "BBB", Mobile = "234"
}
};
var bytes = ExcelHelper.ObjectToExcelBytes(list, ExcelType.Xlsx);
// model
[ExcelTitle("SheetX")]
public class Pr20Model
{
[ExcelColumn("Full name", CellFontColor = ExcelStyleColor.Red)]
public string Fullname { get; set; }
[ExcelColumn("Phone Number",
HeaderFontFamily = "Normal",
HeaderBold = true,
HeaderFontHeight = 30,
HeaderItalic = true,
HeaderFontColor = ExcelStyleColor.Blue,
HeaderUnderline = true,
HeaderAlignment = HorizontalAlignment.Right,
//cell
CellAlignment = HorizontalAlignment.Justify
)]
public string Mobile { get; set; }
}
- v2.0.0.113
convert project to netstandard2.0 and .net452
fixbug #12 #13
- v1.0.0.80
- support simple formula
- support standard excel model
- excel & JSON convert
- excel & Dictionary<string,object> convert
Support Uri to a hyperlink cell
And also support text cell to Uri Type
- v1.0.0.43
Support xlsx [thanks Soar360]
Support complex Boolean type
- v1.0.0.36
Add ExcelToObject<T>(bytes)
示例代码
定义模型
public class ReportModel
{
[Excel("My Title", Order=1)]
public string Title { get; set; }
[Excel("User Name", Order=2)]
public string Name { get; set; }
}
创建模型列表
var models = new List<ReportModel>
{
new ReportModel{Name="a", Title="b"},
new ReportModel{Name="c", Title="d"},
new ReportModel{Name="f", Title="e"}
};
对象转 Excel 文件
var exporter = new ExcelExporter();
var bytes = exporter.ObjectToExcelBytes(models);
File.WriteAllBytes("C:\\demo.xls", bytes);
Excel 文件转对象
var importer = new ExcelImporter();
IEnumerable<ReportModel> result = importer.ExcelToObject<ReportModel>("c:\\demo.xls");
// 也可以直接使用字节数组
// IEnumerable<ReportModel> result = importer.ExcelToObject<ReportModel>(bytes);
自动列宽(新特性)
// 启用自动列宽调整
var bytes = ExcelHelper.ObjectToExcelBytes(models, options =>
{
options.ExcelType = ExcelType.Xlsx;
options.AutoColumnWidth = true; // 启用自动宽度
options.MinColumnWidth = 8; // 最小宽度(字符)
options.MaxColumnWidth = 50; // 最大宽度(字符)
options.DefaultColumnWidth = 16; // 禁用自动时的默认宽度
});
在 ASP.NET MVC 中使用
在 ASP.NET MVC 模型中,DisplayAttribute 可以像 ExcelTitleAttribute 一样被支持。
文档
更多信息请访问:http://www.cnblogs.com/chsword/p/excel2object.html
开发和发布
- 自动化发布脚本
release.ps1- 一键完成版本更新、提交和 Tag 创建的 PowerShell 脚本 - 自动化发布系统说明 - 包含 Copilot 指令、版本管理和自动发布流程的完整说明
- 版本管理规范 - 语义化版本规范和版本号递增规则
- 发布流程指南 - 详细的发布步骤和故障排查指南
- Copilot 使用说明 - GitHub Copilot 开发指导和项目规范
快速发布新版本
使用自动化脚本一键发布:
# Windows
.\release.ps1 -Version 2.0.4
# Linux/macOS (需要 PowerShell Core)
pwsh ./release.ps1 -Version 2.0.4
详细说明请参阅 RELEASE_AUTOMATION.md。
贡献者
参考
许可证
本项目采用 MIT 许可证 - 详见 LICENSE 文件。
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. 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 was computed. 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 is compatible. 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. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 is compatible. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 is compatible. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
.NETFramework 4.7.2
- NPOI (>= 2.8.0)
- System.Security.Cryptography.Xml (>= 8.0.4)
-
.NETStandard 2.0
- NPOI (>= 2.8.0)
- System.Security.Cryptography.Xml (>= 8.0.4)
-
.NETStandard 2.1
- NPOI (>= 2.8.0)
- System.Security.Cryptography.Xml (>= 8.0.4)
-
net10.0
- NPOI (>= 2.8.0)
- System.Security.Cryptography.Xml (>= 8.0.4)
-
net6.0
- NPOI (>= 2.8.0)
- System.Security.Cryptography.Xml (>= 8.0.4)
-
net8.0
- NPOI (>= 2.8.0)
- System.Security.Cryptography.Xml (>= 8.0.4)
-
net9.0
- NPOI (>= 2.8.0)
- System.Security.Cryptography.Xml (>= 8.0.4)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 2.12.0 | 65 | 9/17/2026 |
| 2.11.0 | 79 | 9/16/2026 |
| 2.10.0 | 73 | 9/16/2026 |
| 2.9.1 | 84 | 9/15/2026 |
| 2.8.1 | 86 | 9/13/2026 |
| 2.8.0 | 86 | 9/13/2026 |
| 2.7.1 | 87 | 9/13/2026 |
| 2.7.0 | 92 | 9/13/2026 |
| 2.6.0 | 88 | 9/12/2026 |
| 2.5.0 | 86 | 9/12/2026 |
| 2.4.0 | 109 | 9/11/2026 |
| 2.3.0 | 92 | 9/11/2026 |
| 2.2.1 | 92 | 9/11/2026 |
| 2.2.0 | 91 | 9/11/2026 |
| 2.1.0 | 96 | 9/8/2026 |
| 2.0.5 | 958 | 10/11/2025 |
| 2.0.4 | 217 | 10/11/2025 |
| 2.0.0.211 | 359 | 8/28/2025 |
| 2.0.0.206 | 769 | 10/21/2024 |
| 2.0.0.205 | 378 | 8/5/2024 |
Loading failed