上篇文章向大家介绍了用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. Keras(四)CNN 卷积神经网络 RNN 循环神经网络 原理及实例

    CNN 卷积神经网络 卷积 池化 https://www.cnblogs.com/peng8098/p/nlp_16.html 中有介绍 以数据集MNIST构建一个卷积神经网路 from keras. ...

  2. 矩阵之间无循环计算L2距离

    实现两个矩阵的无循环计算欧氏距离 Euclidean distance navigation: 1.问题描述 2.解决方法 1.问题来源 kNN算法中会计算两个矩阵的距离 可以使用循环的方法来实现,效 ...

  3. Increasing heap size while building the android source code on Ubuntu 15.10

    http://stackoverflow.com/questions/34940793/increasing-heap-size-while-building-the-android-source-c ...

  4. pandas数据分析输出excel产生文本形式存储的百分比数据,如何处理?

    关键词: python.pandas.to_excel.文本形式存储的数据 需求描述: 我用 python pandas 写了数据统计与分析脚本,并把计算结果用 pandas 的 to_excel() ...

  5. spring组件注册

    基于注解和类的组件注册 @Conditional 作用:按照一定的条件进行判断,如果满足条件的话就给spring容器中注册bean ​ 该注解既可以标注到方法上面,也可以标注到类上面(只有满足条件时, ...

  6. 实现一个简易版的vuex持久化工具

    背景 最近用uni-app开发小程序项目时,部分需要持久化的内容直接使用vue中的持久化插件貌似不太行,所以想着自己实现一下类似vuex-persistedstate插件的功能,想着功能不多,代码量应 ...

  7. Storm VS Flink ——性能对比

    1.背景 Apache Flink 和 Apache Storm 是当前业界广泛使用的两个分布式实时计算框架.其中 Apache Storm(以下简称"Storm")在美团点评实时 ...

  8. java.util.Timer简介

    Timer是用于管理在后台执行的延迟任务或周期性任务,其中的任务使用java.util.TimerTask表示.任务的执行方式有两种: 按固定速率执行:即scheduleAtFixedRate的两个重 ...

  9. maven:Fatal error compiling: 无效的目标发行版: 1.8.0_45 -> [Help 1]

    使用mvn clean install命令的时候出现如下的错误: Failed to execute goal org.apache.maven.plugins:maven-compiler-plug ...

  10. 神奇的 SQL 之层级 → 为什么 GROUP BY 之后不能直接引用原表中的列

    前言 开心一刻 感觉不妙呀,弟弟舔它! 不该舔的,舔到怀疑人生了...... GROUP BY 后 SELECT 列的限制 标准 SQL 规定,在对表进行聚合查询的时候,只能在 SELECT 子句中写 ...