首页 技术 正文
技术 2022年11月15日
0 收藏 590 点赞 4,336 浏览 2712 个字

让 .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 DateTime 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();

More

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

Contact

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

Contact me: https://www.shuzhiduo.com/A/o75Nv7ZN5W/weihanli@oulook.com

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,082
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,556
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,405
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,179
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,815
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,898