C#中ExcelDataReader的具體使用
今天來給大家介紹一下ExcelDataReader,ExcelDataReader是一個輕量級的可快速讀取Excel文件中數(shù)據(jù)的工具。話不多說直接開始。
ExcelDataReader簡介
ExcelDataReader支持.xlsx、.xlsb、.xls、.csv格式文件的讀取,版本基本在2007及以上版本,支持的格式和版本看下圖。
我們首先需要再VS中添加ExcelDataReader程序集,在 工具 -> NuGet包管理器 -> 管理解決方案的NuGet程序包 中搜索ExcelDataReader,并安裝到工程中。如下圖:
ExcelDataReader和ExcelDataReader.DataSet都可以調(diào)用ExcelDataReader庫,官網(wǎng)介紹由于版本升級問題,如果需要使用AsDataSet()方法則需要安裝ExcelDataReader.DataSet。
ExcelDataReader使用
我們下來介紹一下ExcelDataReader對于數(shù)據(jù)的讀取,有兩種方式可以讀取Excel,IExcelDataReader方法和DataSet方法。
我們先來展示一下Excel文件的示例,圖1是Sheet1,圖2是Sheet2
IExcelDataReader
首先我們需要通過 File.OpenRead 來獲取 Excel.xlsx 的文件流數(shù)據(jù)。通過 ExcelReaderFactory.CreateReader 接口來創(chuàng)建 IExcelDataReader reader 對象,reader采取的是逐個工作表、逐行的方式讀取數(shù)據(jù),然后通過 reader.GetString(0) 獲取數(shù)據(jù)。
reader默認為第一個工作表,第一行數(shù)據(jù)開始。調(diào)用 reader.NextResult() 接口 reader 會移入下一個工作表。調(diào)用 reader.Read() 接口 reader 會讀取一行數(shù)據(jù)。reader.GetString(0) 接口是通過索引來獲取字符串?dāng)?shù)據(jù),除此之外 reader 還有其他獲取不同類型數(shù)據(jù)的接口,小伙伴可以自行查閱文檔。
由于 IExcelDataReader 采用的是逐頁、逐行讀取數(shù)據(jù)的方式,這種方式隨便不夠靈活便捷,但由于每次讀取數(shù)據(jù)量少,所以在運行效率和性能上有比較大的優(yōu)勢。
using System.Text; using ExcelDataReader; public class Program { static void Main(string[] args) { Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); string filePath = "Excel.xlsx"; using (FileStream stream = File.OpenRead(filePath)) { using (IExcelDataReader reader = ExcelReaderFactory.CreateReader(stream)) { // 1. Use the reader methods do { Console.WriteLine($"Sheet name:{reader.Name}"); while (reader.Read()) { Console.WriteLine($"{reader.GetString(0)} \t\t{reader.GetString(1)}"); } Console.WriteLine(); } while (reader.NextResult()); } } } }
AsDataSet
同樣我們需要通過 File.OpenRead 來獲取 Excel.xlsx 的文件流數(shù)據(jù),然后通過 ExcelReaderFactory.CreateReader 接口來創(chuàng)建 IExcelDataReader reader 對象,這與之前的操作一樣。
之后我們通過 reader.AsDataSet() 接口來獲取 DataSet dataSet 對象,dataSet 可以采用索引的方式來獲取數(shù)據(jù)。通過 dataSet.Tables[0] 可以獲取到 DataTable dataTable 對象。dataTable 就是Excel的工作頁,輸出 dataTable.TableName 可以獲取到工作頁名稱。我們再通過 dataTable.Rows[0] 來獲取 DataRow dataRow 對象,dataRow 是當(dāng)前工作頁下每一行的數(shù)據(jù),通過 dataRow[0] 可以獲取單元格中的數(shù)據(jù)。
除了索引獲取數(shù)據(jù)以外,依然可以通過遍歷的方式來獲取數(shù)據(jù),示例代碼如下。
using System.Data; using System.Text; using ExcelDataReader; public class Program { static void Main(string[] args) { Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); string filePath = "Excel.xlsx"; using (FileStream stream = File.OpenRead(filePath)) { using (IExcelDataReader reader = ExcelReaderFactory.CreateReader(stream)) { // 2. Use the AsDataSet extension method DataSet dataSet = reader.AsDataSet(); DataTable dataTable = dataSet.Tables[0]; Console.WriteLine(dataTable.TableName); DataRow dataRow = dataTable.Rows[0]; Console.WriteLine($"{dataRow[0]} {dataRow[1]}\n"); foreach (DataTable table in dataSet.Tables) { Console.WriteLine(table.TableName); foreach (DataRow row in table.Rows) { Console.WriteLine($"{row[0]} \t{row[1]}"); } Console.WriteLine(); } } } } }
補充說明
需要補充說明一下的是 ExcelDataReader 在.NET Core 和 .NET 5.0 或更高版本中會拋出encoding 1252的錯誤編碼。解決辦法就是需要為System.Text.Encoding.CodePages添加一個依賴項,在代碼中添加 Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); 這段代碼
官方文檔鏈接
ExcelDataReader鏈接:https://github.com/ExcelDataReader/ExcelDataReader
到此這篇關(guān)于C#中ExcelDataReader的具體使用的文章就介紹到這了,更多相關(guān)C# ExcelDataReader內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- C#如何將DataTable導(dǎo)出到Excel解決方案
- C#操作EXCEL DataTable轉(zhuǎn)換的實例代碼
- C#將Excel中的數(shù)據(jù)轉(zhuǎn)換成DataSet
- C#實現(xiàn)將DataTable內(nèi)容輸出到Excel表格的方法
- C#使用Datatable導(dǎo)出Excel
- C#中DataGridView導(dǎo)出Excel的兩種方法
- C#把EXCEL數(shù)據(jù)轉(zhuǎn)換成DataTable
- C#把DataTable導(dǎo)出為Excel文件
- C#實現(xiàn)讀取Excel文件并將數(shù)據(jù)寫入數(shù)據(jù)庫和DataTable
相關(guān)文章
C#實現(xiàn)IDisposable接口釋放非托管資源
這篇文章主要為大家介紹了C#實現(xiàn)IDisposable接口釋放非托管資源,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-05-05C#實現(xiàn)判斷文件夾存在與否并創(chuàng)建文件夾的方法
這篇文章主要介紹了C#實現(xiàn)判斷文件夾存在與否并創(chuàng)建文件夾的方法,涉及C#針對文件及目錄的判斷與創(chuàng)建操作相關(guān)技巧,需要的朋友可以參考下2017-02-02c#中DataTable轉(zhuǎn)List的2種方法示例
這篇文章主要給大家介紹了關(guān)于c#中DataTable轉(zhuǎn)List的2種方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04