博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使用WeihanLi.Npoi操作Excel
阅读量:5958 次
发布时间:2019-06-19

本文共 5973 字,大约阅读时间需要 19 分钟。

WeihanLi.Npoi

Intro

Npoi 扩展,适用于.netframework4.5及以上和netstandard2.0, .netframework基于, .netstandard基于

NpoiExtensions for target framework net4.5 or netstandard2.0,for net45 basedon ,for .netstandard basedon

Use

Install

.NetFramework

Install-Package WeihanLi.Npoi

.NetCore

dotnet add package WeihanLi.Npoi

GetStarted

  1. LoadFromExcelFile

    it consider the first row of the sheet as the header not for read,it will read data from next row.You can point out your header row through the exposed api if needed.

    • Read Excel to DataSet

      // read excel to dataSet, read all sheets data to dataSet,by default it will read from the headerRowIndex(0) + 1var dataSet = ExcelHelper.ToDataSet(string excelPath);// read excel to dataSet, read all sheets data to dataSet,headerRowIndex is not for read,read from headerRowIndex+1var dataSet = ExcelHelper.ToDataSet(string excelPath, int headerRowIndex);
    • Read Excel to DataTable

      // read excel to dataTable directly,by default read the first sheet contentvar dataTable = ExcelHelper.ToDataTable(string excelPath);// read excel workbook's sheetIndex sheet to dataTable directlyvar dataTableOfSheetIndex = ExcelHelper.ToDataTable(string excelPath, int sheetIndex);// read excel workbook's sheetIndex sheet to dataTable,custom headerRowIndexvar dataTableOfSheetIndex = ExcelHelper.ToDataTable(string excelPath, int sheetIndex, int headerRowIndex);// read excel to dataTable use mapping relations and settings from typeof(T),by default read the first sheet contentvar dataTableT = ExcelHelper.ToDataTable
      (string excelPath);// ... sheetIndex and headerRowIndex is also supported like above
    • Read Excel to List

      // read excel first sheet content to a List
      var entityList = ExcelHelper.ToEntityList
      (string excelPath);// read excel sheetIndex sheet content to a List
      // you can custom header row index via sheet attribute or fluent api HasSheetvar entityList1 = ExcelHelper.ToEntityList
      (string excelPath, int sheetIndex);
  2. Get a workbook

    // load excel workbook from filevar workbook = LoadExcel(string excelPath);// prepare a workbook accounting to excelPathvar workbook = PrepareWorkbook(string excelPath);// prepare a workbook accounting to excelPath and custom excel settingsvar workbook = PrepareWorkbook(string excelPath, ExcelSetting excelSetting);// prepare a workbook whether *.xlsx filevar workbook = PrepareWorkbook(bool isXlsx);// prepare a workbook whether *.xlsx file and custom excel settingvar workbook = PrepareWorkbook(bool isXlsx, ExcelSetting excelSetting);
  3. Rich extensions

    List
    ToEntityList
    ([NotNull]this IWorkbook workbook)DataTable ToDataTable([NotNull]this IWorkbook workbook)ISheet ImportData
    ([NotNull] this ISheet sheet, DataTable dataTable)int ImportData
    ([NotNull] this IWorkbook workbook, IEnumerable
    list, int sheetIndex)int ImportData
    ([NotNull] this ISheet sheet, IEnumerable
    list)int ImportData
    ([NotNull] this IWorkbook workbook, [NotNull] DataTable dataTable, int sheetIndex)ToExcelFile
    ([NotNull] this IEnumerable
    entityList, [NotNull] string excelPath)int ToExcelStream
    ([NotNull] this IEnumerable
    entityList, [NotNull] Stream stream)byte[] ToExcelBytes
    ([NotNull] this IEnumerable
    entityList)int ToExcelFile([NotNull] this DataTable dataTable, [NotNull] string excelPath)int ToExcelStream([NotNull] this DataTable dataTable, [NotNull] Stream stream)byte[] ToExcelBytes([NotNull] this DataTable dataTable)byte[] ToExcelBytes([NotNull] this IWorkbook workbook)int WriteToFile([NotNull] this IWorkbook workbook, string filePath)object GetCellValue([NotNull] this ICell cell, Type propertyType)T GetCellValue
    ([NotNull] this ICell cell)SetCellValue([NotNull] this ICell cell, object value)

Define Custom Mapping and settings

  1. Attributes

    Add ColumnAttribute on the property of the entity which you used for export or import

    Add SheetAttribute on the entity which you used for export or import,you can set the StartRowIndex on your need(by default it is 1)

    for example:

    public class TestEntity{    [Column("PKID")]    public int PKID { get; set; }    [Column("账单标题")]    public string BillTitle { get; set; }    [Column("账单详情")]    public string BillDetails { get; set; }    [Column("创建人")]    public string CreatedBy { get; set; }    [Column("创建时间")]    public DateTime CreatedTime { get; set; }}internal class TestEntity1{    ///     /// 用户名    ///     [Column("用户名")]    public string Username { get; set; }    [Column(IsIgnored = true)]    public string PasswordHash { get; set; }    [Column("可用余额")]    public decimal Amount { get; set; } = 1000M;    [Column("微信id")]    public string WechatOpenId { get; set; }    [Column("是否启用")]    public bool IsActive { get; set; }}
  2. FluentApi

    You can also use FluentApi above version 1.0.3

    for example:

    var setting = ExcelHelper.SettingFor
    ();// ExcelSettingsetting.HasAuthor("WeihanLi") .HasTitle("WeihanLi.Npoi test") .HasDescription("") .HasSubject("");setting.HasSheetConfiguration(0, "系统设置列表");setting.HasFilter(0, 1) .HasFreezePane(0, 1, 2, 1);setting.Property(_ => _.SettingId) .HasColumnIndex(0);setting.Property(_ => _.SettingName) .HasColumnTitle("设置名称") .HasColumnIndex(1);setting.Property(_ => _.DisplayName) .HasColumnTitle("设置显示名称") .HasColumnIndex(2);setting.Property(_ => _.SettingValue) .HasColumnTitle("设置值") .HasColumnIndex(3);setting.Property(_ => _.CreatedTime) .HasColumnTitle("创建时间") .HasColumnIndex(5) .HasColumnFormatter("yyyy-MM-dd HH:mm:ss");setting.Property(_ => _.CreatedBy) .HasColumnIndex(4) .HasColumnTitle("创建人");setting.Property(_ => _.UpdatedBy).Ignored();setting.Property(_ => _.UpdatedTime).Ignored();setting.Property(_ => _.PKID).Ignored();

Samples

Contact

Contact me: weihanli@outlook.com

转载地址:http://cxexx.baihongyu.com/

你可能感兴趣的文章
第六章:thymeleaf页面模版-1. 信息输出
查看>>
Ubuntu 16.04 install Docker 1.12.0
查看>>
2012《Linux杂志》读者选择奖 (Readers' Choice Awards 2012- Linux Journal)
查看>>
21天让你成为Horizon View高手—Day11:手动池的创建
查看>>
Python迭代对象、迭代器、生成器
查看>>
请求转发与重定向的区别
查看>>
大数据分析 | 百年奥运往事知多少
查看>>
矩形覆盖-----批了外皮的亲蛙跳
查看>>
@RequestParam今天才知道是咋用的..
查看>>
全国第一家FPGA云主机(FAAS)正式启动售卖,被阿里云抢先了。
查看>>
Linux 局域网路由新手指南:第 2 部分
查看>>
TensorSpace:超酷炫3D神经网络可视化框架
查看>>
横向ListView (二)—— 添加快速滚动功能及item相关事件实现
查看>>
java 开发银行支付、对账时证书相关的操作总结
查看>>
为什么你的缓存更新策略是先更新数据库后删除缓存,讲讲其他的情况有什么问题?...
查看>>
计数服务设计
查看>>
如何在windows中使用cmd命令去编译,运行C++程序
查看>>
solidity语言开发智能合约
查看>>
再有人问你Netty是什么,就把这篇文章发给他
查看>>
centos7安装kibana5.x
查看>>