C#   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了让 .NET 更方便的导入导出 Excel大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

让 .Net 更方便的导入导出Excel

Intro

因为前一段时间需要处理一些 excel 数据,主要是导入/导出操作,将 Excel 数据转化为对象再用程序进行处理和分析,没有找到比较满意的库,于是就自己造了一个轮子,屏蔽掉了 xLSX 与 xls 的差别,屏蔽了 Npoi 操作 Excel 的细节,提供简单容易上手的 api。完整的 API 列表请查看:https://weihanli.github.io/WeihanLi.Npoi/docs/api/WeihanLi.Npoi.html

导入/导出

添加 nuget 包引用 WeihanLi.Npoi

根据 excel 文件获取一个 IWorkbook 对象,支持 *.xls/*.xLSX

IWorkbook workbook = ExcelHelper.LoadExcel("excelFilePath");

将 Excel 文件的第一个 sheet 里的内容转成 list 对象

List<TEntity> entityList = ExcelHelper.ToEntityList<TEntity>("excelFilePath");

将 Excel 文件的第一个 sheet 里的内容转成 DataTable 对象

DataTable dataTable = ExcelHelper.ToDataTable("excelFilePath");

将 list 对象导出到 Excel 字节数组

List<TEntity> entityList = ExcelHelper.ToEntityList<TEntity>("excelFilePath");

entityList.ToExcelBytes();

将 list 对象导出到 Excel 文件

List<TEntity> entityList = ExcelHelper.ToEntityList<TEntity>("excelFilePath");

entityList.ToExcelFile("excelFilePath");

自定义配置

认的导入导出是按照属性名为导出的列名称的,如果不能满足或者想要自定义某些属性不导出或设置导出顺序,可以通过配置实现

提供了两种配置方式,一种是使用 Attribute 方式来配置,第二种是使用 FluentAPI 方式配置(推荐,对代码无侵入性)

  1. Attributes

    在要导入导出的属性上设置 columnAttribute自定义导出的列名称或排序或忽略

    添加 SheetAttribute 来设置导出的excel sheet名称

    for example:

    public class TestEntity
    {
        [column("Id")]
        public int PKID { get; set; }
    
        [column("Bill title")]
        public String Billtitle { get; set; }
    
        [column("Bill Details")]
        public String BillDetails { get; set; }
    
        [column("createdby")]
        public String createdby { get; set; }
    
        [column("CreatedTime")]
        public datetiR_745_11845@e CreatedTime { get; set; }
    }
    
    public class TestEntity1
    {
        [column("Username")]
        public String Username { get; set; }
    
        [column(IsIgnored = truE)]
        public String passwordHash { get; set; }
    
        [column("amount")]
        public decimal amount { get; set; } = 1000M;
    
        [column("WechatOpenId")]
        public String WechatOpenId { get; set; }
    
        [column("IsActive")]
        public bool IsActive { get; set; }
    }
    
  2. FluentApi (Recommend)

    var setTing = ExcelHelper.SetTingFor<TestEntity>();
    // ExcelSetTing
    setTing.HasAuthor("WeihanLi")
        .Hastitle("WeihanLi.Npoi test")
        .HasDescription("")
        .HasSubject("");
    
    setTing.HasSheetConfiguration(0,"System SetTings");
    
    setTing.HasFilter(0,1)
        .HasFreezePane(0,1,2,1);
    
    setTing.Property(_ => _.SetTingId)
        .HascolumnIndex(0);
    
    setTing.Property(_ => _.SetTingName)
        .Hascolumntitle("SetTingName")
        .HascolumnIndex(1);
    
    setTing.Property(_ => _.DisplayName)
        .HascolumnFormatter((entity,displayName) => $"AAA_{entity.SetTingNamE}_{displayNamE}")
        .Hascolumntitle("DisplayName")
        .HascolumnIndex(2);
    
    setTing.Property(_ => _.SetTingvalue)
        .Hascolumntitle("SetTingValue")
        .HascolumnIndex(3);
    
    setTing.Property(_ => _.CreatedTimE)
        .Hascolumntitle("CreatedTime")
        .HascolumnIndex(5)
        .HascolumnFormatter("yyyy-MM-dd HH:mm:ss");
    
    setTing.Property(_ => _.createdby)
        .HascolumnIndex(4)
        .Hascolumntitle("createdby");
    
    setTing.Property(_ => _.updatedBy).Ignored();
    setTing.Property(_ => _.updatedTimE).Ignored();
    setTing.Property(_ => _.PKID).Ignored();
    
@H_756_78@more

想要更多自定义选项,参示例项目,或者给我提 issue

Contact

如果使用过程中有遇到什么问题,欢迎与我联系。

Contact me: weihanli@oulook.com

大佬总结

以上是大佬教程为你收集整理的让 .NET 更方便的导入导出 Excel全部内容,希望文章能够帮你解决让 .NET 更方便的导入导出 Excel所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: