使用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
安装
可以用nuget管理工具安装,也可以通过命令行安装。
Install-Package ExcelDataReader
使用
FileStream stream = File.Open(filePath, FileMode.Open, FileAccess.Read);
//Choose one of either 1 or 2
//1. Reading from a binary Excel file ('97-2003 format; *.xls)
IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader(stream);
//2. Reading from a OpenXml Excel file (2007 format; *.xlsx)
IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
//Choose one of either 3, 4, or 5
//3. DataSet - The result of each spreadsheet will be created in the result.Tables
DataSet result = excelReader.AsDataSet();
//4. DataSet - Create column names from first row
excelReader.IsFirstRowAsColumnNames = true;
DataSet result = excelReader.AsDataSet();
//5. Data Reader methods
while (excelReader.Read())
{
//excelReader.GetInt32(0);
}
//6. Free resources (IExcelDataReader is IDisposable)
excelReader.Close();
更多推荐
已为社区贡献1条内容
所有评论(0)