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, ...
随机推荐
- 设计模式(C#)——06桥接模式
推荐阅读: 我的CSDN 我的博客园 QQ群:704621321 在早先,几乎每个手机的充电器接口都是不同的.每个型号的手机都有一个充电器,此时我们把充电器作为一个抽象类,抽象类中提 ...
- Leetcode之深度优先搜索(DFS)专题-547. 朋友圈(Friend Circles)
Leetcode之深度优先搜索(DFS)专题-547. 朋友圈(Friend Circles) 深度优先搜索的解题详细介绍,点击 班上有 N 名学生.其中有些人是朋友,有些则不是.他们的友谊具有是传递 ...
- coco标注信息与labelme标注信息的详解、相互转换及可视化
引言 在做实例分割或语义分割的时候,我们通常要用labelme进行标注,labelme标注的json文件与coco数据集已经标注好的json文件的格式和内容有差异.如果要用coco数据集的信息,就要对 ...
- 【Leetcode】【简单】【66. 加一】【JavaScript】
题目描述 66. 加一 给定一个由整数组成的非空数组所表示的非负整数,在该数的基础上加一. 最高位数字存放在数组的首位, 数组中每个元素只存储单个数字. 你可以假设除了整数 0 之外,这个整数不会以零 ...
- Service:让客户端发现pod并与之通信
5.1.Service介绍 5.1.1.Serice简介 5.1.1.1什么是Service service是k8s中的一个重要概念,主要是提供负载均衡和服务自动发现. Service 是由 kube ...
- Android Activity启动耗时统计方案
作者:林基宗 Activity的启动速度是很多开发者关心的问题,当页面跳转耗时过长时,App就会给人一种非常笨重的感觉.在遇到某个页面启动过慢的时候,开发的第一直觉一般是onCreate执行速度太慢了 ...
- Erlang模块erl翻译
命令: erl 概述: Erlang模拟器 描述: erl程序启动一个Erlang运行时系统.准确的信息是依赖于系统的(举例,erl是否是脚本或程序,其它程序调用). ...
- Python 的整数与 Numpy 的数据溢出
某位 A 同学发了我一张截图,问为何结果中出现了负数? 看了图,我第一感觉就是数据溢出了.数据超出能表示的最大值,就会出现奇奇怪怪的结果. 然后,他继续发了张图,内容是 print(100000*20 ...
- Cookie与Seesion的作用
1.什么是Cookie与Session? cookie:首次访问服务器,服务器返回cookie置浏览器,存到用户电脑.之后去访问同一服务器,浏览器会携带相应cookie判断是否是同一浏览器的访问,告知 ...
- 内存泄露检测工具Valgrind
内存泄露简介 什么是内存泄漏 内存泄漏(Memory Leak)是指程序中已动态分配的堆内存由于某种原因,程序未释放或无法释放,造成系统内存的浪费,导致程序运行速度减慢甚至系统崩溃等严重后果. 内存泄 ...