ExcelDataReader read excel file
上篇文章向大家介绍了用DocumentFormat.OpenXml.dll读取excel的方法,这里再向大家介绍一种轻量级简便的方法,用的是Excel.dll,及ICSharpCode.SharpZipLib.dll, 很简单,只需要在vs2013中通过add reference->Manage NuGet Packages->找到ExcelDataReader->点击Install。
Code:
public class ExcelDataReader
{
private string path;
public string Path
{
get { return path; }
set { path = value; }
}
private bool isFirstRowAsColumnNames;
public bool IsFirstRowAsColumnNames
{
get { return IsFirstRowAsColumnNames; }
set { isFirstRowAsColumnNames = value; }
}
public ExcelDataReader(string path, bool isFirstRowAsColumnNames)
{
this.path = path;
this.isFirstRowAsColumnNames = isFirstRowAsColumnNames;
}
private IExcelDataReader GetExcelDataReader()
{
using (FileStream fileStream = File.Open(path, FileMode.Open, FileAccess.Read))
{
IExcelDataReader dataReader;
if (path.EndsWith(".xls"))
{
dataReader = ExcelReaderFactory.CreateBinaryReader(fileStream);
}
else if (path.EndsWith(".xlsx"))
{
dataReader = ExcelReaderFactory.CreateOpenXmlReader(fileStream);
}
else
{
throw new Exception("The file to be process is not an excel file.");
}
dataReader.IsFirstRowAsColumnNames = isFirstRowAsColumnNames;
return dataReader;
}
}
private DataSet GetExcelDataAsDataSet()
{
return GetExcelDataReader().AsDataSet();
}
private DataTable GetExcelWorkSheet(string workSheetName)
{
DataSet dataSet = GetExcelDataAsDataSet();
var sheets = from DataTable sheet in dataSet.Tables
select sheet.TableName;
DataTable workSheet = dataSet.Tables[workSheetName];
if (workSheet == null)
{
throw new Exception(string.Format("The worksheet {0} does not exist, has an incorrect name, or does not have any data in the worksheet", workSheetName));
}
return workSheet;
}
private IEnumerable<string> GetWorkSheetNames()
{
DataSet dataSet = GetExcelDataAsDataSet();
var sheets = from DataTable sheet in dataSet.Tables
select sheet.TableName;
return sheets;
}
public List<List<DataRow>> GetData()
{
List<List<DataRow>> dataRows = new List<List<DataRow>>();
IEnumerable<string> workSheets = GetWorkSheetNames();
logger.Debug("Worksheets count :{0}.", workSheets.Count());
foreach (string sheet in workSheets)
{
try
{
DataTable workSheet = GetExcelWorkSheet(sheet);
List<DataRow> rows = (from DataRow row in workSheet.Rows
where !string.IsNullOrEmpty(row[].ToString())
select row).ToList();
if (rows.Count > )
{
dataRows.Add(rows); }
}
catch (Exception ex)
{ }
}
return dataRows;
}
}
}
ExcelDataReader read excel file的更多相关文章
- NetSuite SuiteScript 2.0 export data to Excel file(xls)
In NetSuite SuiteScript, We usually do/implement export data to CSV, that's straight forward: Collec ...
- Read Excel file from C#
Common way is: var fileName = string.Format("{0}\\fileNameHere", Directory.GetCurrentDirec ...
- csharp:using OpenXml SDK 2.0 and ClosedXML read excel file
https://openxmlexporttoexcel.codeplex.com/ http://referencesource.microsoft.com/ 引用: using System; u ...
- Creating Excel File in Oracle Forms
Below is the example to create an excel file in Oracle Forms.Pass the Sql query string to the below ...
- Formatting Excel File Using Ole2 In Oracle Forms
Below is the some useful commands of Ole2 to format excel file in Oracle Forms.-- Change font size a ...
- Read / Write Excel file in Java using Apache POI
Read / Write Excel file in Java using Apache POI 2014-04-18 BY DINESH LEAVE A COMMENT About a year o ...
- The 13th tip of DB Query Analyzer, powerful processing EXCEL file
The 13thtip of DB Query Analyzer, powerful processing EXCEL file MA Genfeng (Guangdong UnitollServic ...
- How to create Excel file in C#
http://csharp.net-informations.com/excel/csharp-create-excel.htm Before you create an Excel file in ...
- Apache POI – Reading and Writing Excel file in Java
来源于:https://www.mkyong.com/java/apache-poi-reading-and-writing-excel-file-in-java/ In this article, ...
随机推荐
- Oracle批量更改所有表的字段取值_类型_原字段名
CREATE PROCEDURE 存储过程名称 is cursor c_tab is select * from user_tab_columns t r_tab user_tab_columns%r ...
- Beego orm 模型字段与数据库类型的对应
深度学习,ORM 推荐的对应数据库类型,在此列出,自动建表功能也会以此为标准.默认所有的字段都是 NOT NULL MySQL go mysql int, int32-设置auto或者名称为Id in ...
- list 分组
Map<Long, List<LogDataVo>> corpIdMap = list.stream().collect(Collectors.groupingBy(LogDa ...
- 传入值参数&传入引用参数的区别
传值&传引用 1.传值 是把实参的值赋值给行参 那么对行参的修改,不会影响实参的值 2.传地址 是传值的一种特殊方式,只是他传递的是地址 那么传地址以后,实参和行参都指向同一个对象 3.传引用 ...
- SCRUM MASTER检查单
转自:http://www.scrumcn.com/agile/scrum-knowledge-library/scrum.html#tab-id-18 一位合格的ScrumMaster通常能够同时处 ...
- 2019DX#10
Solved Pro.ID Title Ratio(Accepted / Submitted) 1001 Minimum Spanning Trees 22.22%(2/9) 1002 Lin ...
- codeforces 816 B. Karen and Coffee(思维)
题目链接:http://codeforces.com/contest/816/problem/B 题意:给出n个范围,q个查询问查询区间出现多少点在给出的n个范围中至少占了k次 题解:很显然的一道题目 ...
- codeforces 789 C. Functions again(dp求区间和最大)
题目链接:http://codeforces.com/contest/789/problem/C 题意:就是给出一个公式 然后给出一串数求一个区间使得f(l,r)最大. 这题需要一个小小的处理 可以设 ...
- js中的所有兼容问题总结
js兼容问题总结 在学习js过程中很多人都遇到过兼容问题,这些兼容问题是因为各版本浏览器不同导致的,为了解决这些兼容问题,js给我们提供了解决这些兼容问题的方案,对此,我个人进行了汇集以及总结. ...
- js jQuery显示隐藏div的几种方法
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/qq_36135335/article/d ...