c# 使用ExcelDataReader读写excel(.xls,.xlsx)

介绍
Lightweight and fast library written in C# for reading Microsoft Excel files

ExcelDataReader是一个用c#写的轻量级且高效的操作Microsoft Excel文件的类库。

github地址: 
https://github.com/ExcelDataReader/ExcelDataReader

 

卡号卡状态人员姓名人员编号
2019071500正常卡首页95636858
using System;
using System.IO;
using ExcelDataReader;
using System.Collections.Generic;
using System.Data;
using System.Reflection;
using Utils.Framework;

namespace TestDemoC {
    class Program {
        static void Main(string[] args) {

            var excelPath = System.Environment.CurrentDirectory + "\\card2019-07-12 09_20_06.xls";

            using (var stream = File.Open(excelPath, FileMode.Open, FileAccess.Read)) {

                using (var reader = ExcelReaderFactory.CreateReader(stream)) {

                    do { while (reader.Read()) { } } while (reader.NextResult());

                    var configuration = new ExcelDataSetConfiguration { ConfigureDataTable = tableReader => new ExcelDataTableConfiguration { UseHeaderRow = true } };
                    var result = reader.AsDataSet(configuration);

                    var data = result.Tables[0].ToList<TestEntity>();

                    Log(data.Json());
                }
            }

            Console.ReadLine();
        }

        public class TestEntity {

            public string 卡号 { get; set; }

            public string 卡状态 { get; set; }

            public string 人员姓名 { get; set; }

            public string 人员编号 { get; set; }

        }

        private static void Log(string msg) {

            Console.Write($"{DateTime.Now:yyyy-MM-dd HH:mm:ss:fff} [Info] { msg}{System.Environment.NewLine}");
        }

    }
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值