上篇文章向大家介绍了用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的更多相关文章

  1. 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 ...

  2. Read Excel file from C#

    Common way is: var fileName = string.Format("{0}\\fileNameHere", Directory.GetCurrentDirec ...

  3. csharp:using OpenXml SDK 2.0 and ClosedXML read excel file

    https://openxmlexporttoexcel.codeplex.com/ http://referencesource.microsoft.com/ 引用: using System; u ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. How to create Excel file in C#

    http://csharp.net-informations.com/excel/csharp-create-excel.htm Before you create an Excel file in ...

  9. 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, ...

随机推荐

  1. python多进程通信实例分析

    操作系统会为每一个创建的进程分配一个独立的地址空间,不同进程的地址空间是完全隔离的,因此如果不加其他的措施,他们完全感觉不到彼此的存在.那么进程之间怎么进行通信?他们之间的关联是怎样的?实现原理是什么 ...

  2. HTTP首部字段完全解析

    http协议是前端开发人员最常接触到的网络协议.在开发过程中,尤其是调试过程中避免不了需要去分析http请求的详细信息.在这其中头部字段提供的信息最多,比如通过响应状态码我们可以直观的看到响应的大致状 ...

  3. springboot--事务的使用

    @Transactional原理 事务是一些sql语句对数据库操作的集合,因此如果在一个Java方法里涉及了对数据库的操作,业务需要的话我们就可以考虑把这些操作作为一个事务.通过在方法上加个@Tran ...

  4. 使用SpringSecurity保护程序安全

    首先,引入依赖: <dependency> <groupId>org.springframework.boot</groupId> <artifactId&g ...

  5. NLP(四) 正则表达式

    * + ? * :0个或多个 + :1个或多个 ? :0个或1个 re.search()函数,将str和re匹配,匹配正确返回True import re # 匹配函数,输入:文本,匹配模式(即re) ...

  6. codeforces 879 D. Teams Formation(思维)

    题目链接:http://codeforces.com/contest/879/problem/D 题意:这题题意我反正是看了很久,可能是我的理解能力有点差,就是将一个数组倍增m倍然后将连续的相同的k个 ...

  7. Codeforces 735D Taxes(简单数论)

    题目链接 http://codeforces.com/problemset/problem/735/D 题意:一个人的收入为n他要交的税是n的最大除数,他为了少缴税将n分成k个数n1,n2,n2... ...

  8. Investigating Div-Sum Property UVA - 11361

    An integer is divisible by 3 if the sum of its digits is also divisible by 3. For example, 3702 is d ...

  9. hdu2586 How far away ?(lca模版题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 题意:给出一棵树还有两个点然后求这两个点的最短距离. 题解:val[a]+val[b]-2*va ...

  10. 线段树模板 hdu 1166 敌兵布阵

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...