ExcelToolKit 1.0.3

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

ExcelTools

基于NPOI快捷读取和输出excel的小工具

实现功能

  1. 支持读取.xlsx和.xls格式的excel文件为对象列表
  2. 支持将对象列表输出为.xlsx和.xls格式的excel文件
  3. 如果读取的excel有错误,可在原文件最后增加具体错误信息

快速开始

1.定义实体类

  • 定义实体对象,通过 ExcelTemplateAttribute 指定读取方式,按标题读取或按位置读取;
  • 通过 ExcelColumnAttribute 自定义列信息
    /// <summary>
    /// 测试数据1
    /// 按字段名读取,读取第0个sheet
    /// </summary>
    [ExcelTemplate]
    [MyExcelRowMessage]
    public class TestData1
    {
        [Required]
        /// <summary>
        /// 编码
        /// </summary>
        [ExcelColumn("编码")]
        public string Code { get; set; }

        /// <summary>
        /// 商品名称
        /// </summary>
        [ExcelColumn("商品名称")]
        public string Name { get; set; }
    
        /// …… 省略其他属性
    }

2.读取Excel

  • 读取Excel文件,并返回对象列表
   var data = ExcelHelper.ReadData<TestData1>(path);
  • 读取Excel文件,在原excel文件中加入错误信息列
   (_,var stream) = ExcelHelper.ReadDataAndExport<TestData1>(path);

3.生成excel

    var data = new List<TestData1>();
    data.Add(new TestData1() { Code = "123", Name = "测试" });
    data.Add(new TestData1() { Code = "123", Name = "测试" });
    var bytes = data.Export();

其他配置

  • 自定义数据转换DataConvertAttribute
 /// <summary>
 /// decimal类型转换
 /// </summary>
 public class DecimalDataConvertAttribute : DataConvertAttribute
 {
     public override object ConvertValue(object value)
     {
         if (value == null)
             return value;

         return decimal.Parse(value.ToString());
     }
 }


  • 自定义错误提示格式ExcelRowMessageAttribute
  public class MyExcelRowMessageAttribute : ExcelRowMessageAttribute
  {
      public override string GetMessage(ExcelRowInfo excelRowInfo)
      {
          return $"读取错误:{JsonConvert.SerializeObject(excelRowInfo)}";
      }
  }
  • 使用示例
[ExcelTemplate]
[MyExcelRowMessage]
public class TestData1
{
    /// <summary>
    /// 价格
    /// </summary>
    [ExcelColumn("价格")]
    [DecimalDataConvert]
    public decimal Price { get; set; }
}

具体用法可参照TestProject

Product Compatible and additional computed target framework versions.
.NET 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 was computed.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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
1.0.3 180 8/21/2024
1.0.2 158 8/21/2024
1.0.1 162 8/21/2024
1.0.0 158 8/21/2024