Chsword.Excel2Object 2.0.5

dotnet add package Chsword.Excel2Object --version 2.0.5
                    
NuGet\Install-Package Chsword.Excel2Object -Version 2.0.5
                    
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.0.5" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Chsword.Excel2Object" Version="2.0.5" />
                    
Directory.Packages.props
<PackageReference Include="Chsword.Excel2Object" />
                    
Project file
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.0.5
                    
#r "nuget: Chsword.Excel2Object, 2.0.5"
                    
#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.0.5
                    
#: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.0.5
                    
Install as a Cake Addin
#tool nuget:?package=Chsword.Excel2Object&version=2.0.5
                    
Install as a Cake Tool

Excel2Object

install from nuget release .NET CI CodeFactor

Excel 与 .NET 对象互相转换 / Excel convert to .NET Object and vice versa.

English | 中文

平台支持

.NET 4.7.2 + .NET Standard 2.0 .NET Standard 2.1 .NET 6.0 .NET 8.0

安装 NuGet

PM> Install-Package Chsword.Excel2Object

或使用 .NET CLI:

dotnet add package Chsword.Excel2Object

发布说明和路线图

暂不支持的特性

  • CLI 工具
  • 支持自动列宽 ✅ v2.0.1 新增
  • 支持 Excel 日期/日期时间/时间格式 ✅ v2.0.2 新增 - 查看 DateTimeFormats.md

发布说明

  • 2025.10.11 - v2.0.2
  • 新增: 全面的日期/时间格式支持(56 种格式)- 查看 DateTimeFormats.md
    • ISO 8601 格式(支持时区和毫秒)
    • 多种日期分隔符(横线、斜线、点号)
    • 12 小时制和 24 小时制时间格式
    • 仅时间格式
    • 区域格式支持(美国、欧洲等)
    • 向后兼容现有的中文日期格式(年月日)
  • 更新: NPOI 到 2.7.5
  • 更新: SixLabors.ImageSharp 到 3.1.11(修复安全漏洞)
  • 2025.07.23 - v2.0.1
  • 新增: 基于内容的自动列宽调整
    • 自动计算最优列宽
    • 支持最小和最大宽度限制
    • 正确处理中文/Unicode 字符
    • 可通过 ExcelExporterOptions 配置
  • 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
  • 2023.07.31
  • 支持 DateTime 和 Nullable<DateTime> 格式,如 [ExcelColumn("Title",Format="yyyy-MM-dd HH:mm:ss")]
  • 2023.03.26
  • 2023.02.20
  • 支持平台:.NET Standard 2.0/2.1、.NET 6.0、.NET Framework 4.7.2
  • 2022.03.19
  • 2021.11.4
  • 2021.10.23
  • 修复 Nullable DateTime bug @SunBrook
  • 2021.10.22
  • 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

开发和发布

快速发布新版本

使用自动化脚本一键发布:

# Windows
.\release.ps1 -Version 2.0.4

# Linux/macOS (需要 PowerShell Core)
pwsh ./release.ps1 -Version 2.0.4

详细说明请参阅 RELEASE_AUTOMATION.md

贡献者

Contributors

参考

许可证

本项目采用 MIT 许可证 - 详见 LICENSE 文件。

Product 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 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 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

  • .NETStandard 2.0

  • .NETStandard 2.1

  • net6.0

  • net8.0

  • net9.0

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.0.5 216 10/11/2025
2.0.4 127 10/11/2025
2.0.0.211 275 8/28/2025
2.0.0.206 674 10/21/2024
2.0.0.205 298 8/5/2024
2.0.0.203 364 5/10/2024
2.0.0.200 283 4/24/2024
2.0.0.198 570 11/2/2023
2.0.0.196 328 7/31/2023
2.0.0.194 249 7/31/2023
2.0.0.193 248 7/31/2023
2.0.0.190 628 3/4/2023
2.0.0.188 414 2/20/2023
2.0.0.181 894 8/14/2022
2.0.0.178 885 3/19/2022
2.0.0.175 611 3/19/2022
2.0.0.171 1,199 11/4/2021
2.0.0.161 501 10/23/2021
2.0.0.159 544 10/22/2021
2.0.0.156 1,028 5/28/2021
2.0.0.155 559 5/24/2021
2.0.0.153 551 5/24/2021
2.0.0.143 734 10/26/2020
2.0.0.142 649 10/26/2020
2.0.0.138 968 4/15/2020
2.0.0.131 765 3/11/2020
2.0.0.130 760 3/11/2020
2.0.0.128 744 3/9/2020
2.0.0.124 777 2/5/2020
2.0.0.122 779 12/26/2019
2.0.0.113 809 12/26/2019
2.0.0.105 824 12/25/2019
2.0.0-beta 800 1/15/2019
1.0.0.100 3,416 11/9/2018
1.0.0.99 1,153 8/24/2018
1.0.0.98 1,069 8/24/2018
1.0.0.96 1,041 8/24/2018
1.0.0.94 1,087 8/24/2018
1.0.0.93 1,052 8/24/2018
1.0.0.92 1,060 8/24/2018
1.0.0.80 1,592 7/19/2017
1.0.0.43 1,753 4/13/2016
1.0.0.40 1,725 3/11/2016
1.0.0.35 1,404 3/11/2016
1.0.0.32 1,473 12/31/2015
1.0.0.20 1,461 12/30/2015
1.0.0.18 1,409 12/30/2015
1.0.0.10 1,895 8/16/2014
1.0.0.3 1,834 8/16/2014
1.0.0.1 1,721 8/16/2014
1.0.0 2,066 4/26/2014