日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達者為師

網(wǎng)站首頁 編程語言 正文

C# 利用ExcelDataReader 讀取excel文件

作者:文哥打醬油 更新時間: 2022-04-17 編程語言

github地址?https://github.com/exceldatareader/exceldatareader

1.在NuGet中輸入excel搜索,點擊安裝這兩個如圖

2.新建一個類,寫入代碼

    class ExcelReader 
    {
        public List> getData(string filePath)
        {
            List> list = new List>();
            using (var stream = File.Open(filePath, FileMode.Open, FileAccess.Read))
            {
                // Auto-detect format, supports:
                //  - Binary Excel files (2.0-2003 format; *.xls)
                //  - OpenXml Excel files (2007 format; *.xlsx, *.xlsb)
                using (var reader = ExcelReaderFactory.CreateReader(stream))
                {
                    var configuration = new ExcelDataSetConfiguration { ConfigureDataTable = tableReader => new ExcelDataTableConfiguration { UseHeaderRow = true } };
                    DataSet result = reader.AsDataSet(configuration);
                    for (int i = 0; i < result.Tables[0].Rows.Count; i++)
                    {
                        Dictionary dict = new Dictionary();
                        for (int j = 0; j < result.Tables[0].Columns.Count; j++)
                        {
                            dict.Add(result.Tables[0].Columns[j].ColumnName, result.Tables[0].Rows[i][j]);
                        }
                        list.Add(dict);
                    }
                }
            }
            return list;
        }
    }

?

原文鏈接:https://blog.csdn.net/zxw75192/article/details/113406767

欄目分類
最近更新