C# 读取Excel,一波华丽的操作
C# 读取Excel,其实有很多方法。但是今天要来一波华丽的操作。
先看效果:

以上这波操作使用了 ExcelDataReader 和 ExcelDataReader.DataSet 完成的。
ExcelDataReader 是一个快速读取 Excel 的 C# 库。使用简单,读取速度比较快,感觉比 NPOI 快一点。但是遗憾的是只能读 Excel 没有写的操作。
以上这波操作的全部代码:
using ExcelDataReader;
using System;
using System.IO;
using System.Windows.Forms; namespace ExcelFastRead
{
public partial class FrmMain : Form
{
public FrmMain()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog dialog = new OpenFileDialog();
dialog.Filter = "(Excel 97-03)|*.xls|(Excel 2007)|*.xlsx|ALL|*.*";
if (DialogResult.OK != dialog.ShowDialog())
{
return;
} string strFileName = dialog.FileName;
if (string.IsNullOrWhiteSpace(strFileName))
{
return;
} using (var stream = File.Open(strFileName, FileMode.Open, FileAccess.Read))
{ // Auto-detect format, supports:
// - Binary Excel files (2.0-2003 format; *.xls)
// - OpenXml Excel files (2007 format; *.xlsx)
using (var reader = ExcelReaderFactory.CreateReader(stream))
{ // Choose one of either 1 or 2: // 1. Use the reader methods
do
{
while (reader.Read())
{
// reader.GetDouble(0);
}
} while (reader.NextResult()); ExcelDataSetConfiguration configuration = new ExcelDataSetConfiguration()
{ // Gets or sets a value indicating whether to set the DataColumn.DataType
// property in a second pass.
//UseColumnDataType = true, // Gets or sets a callback to obtain configuration options for a DataTable.
ConfigureDataTable = (tableReader) => new ExcelDataTableConfiguration()
{ // Gets or sets a value indicating the prefix of generated column names.
//EmptyColumnNamePrefix = "Column", // Gets or sets a value indicating whether to use a row from the
// data as column names.
UseHeaderRow = true, // Gets or sets a callback to determine which row is the header row.
// Only called when UseHeaderRow = true.
//ReadHeaderRow = (rowReader) => {
// // F.ex skip the first row and use the 2nd row as column headers:
// rowReader.Read();
//}, // Gets or sets a callback to determine whether to include the
// current row in the DataTable.
//FilterRow = (rowReader) => {
// return true;
//}, // Gets or sets a callback to determine whether to include the specific
// column in the DataTable. Called once per column after reading the
// headers.
//FilterColumn = (rowReader, columnIndex) => {
// return true;
//}
}
};
var result = reader.AsDataSet(configuration); // 2. Use the AsDataSet extension method
//var result = reader.AsDataSet(); dgvList.DataSource = result.Tables[]; // The result of each spreadsheet is in result.Tables
}
} }
}
}
ExcelDataReader.DataSet nuget包管理
C# 读取Excel,一波华丽的操作的更多相关文章
- C#操作Excel(2)-- 打开-读取Excel文档
由于要为某软件实现导出Excel功能,故有此文. 本文的开发环境是Visual Studio 2010 ,C#, Excel 2007. 新建C#工程后打开Solution Explorer,可以看到 ...
- c# .Net :Excel NPOI导入导出操作教程之读取Excel文件信息及输出
c# .Net :Excel NPOI导入导出操作教程之读取Excel文件信息及输出 using NPOI.HSSF.UserModel;using NPOI.SS.UserModel;using S ...
- C#操作Excel文件(读取Excel,写入Excel)
看到论坛里面不断有人提问关于读取excel和导入excel的相关问题.闲暇时间将我所知道的对excel的操作加以总结,如今共享大家,希望给大家可以给大家带了一定的帮助.另外我们还要注意一些简单的问题1 ...
- c#dev操作读取excel方法
一:使用spreadsheetControl1 方法 1:打开excel; private void barButtonItem1_ItemClick(object sender, DevExpres ...
- C# 操作Excel基础篇(读取Excel、写入Excel)
注意事项:Excel的数据表中最多只能储存65535行数据,超出后,需要将数据分割开来进行储存.同时对于Excel中的乱码象限,是由于编码的错误方式导致引起的! 一.读取Excel数据表,获得Data ...
- 接口自动化--读取Excel操作(openpyxl)
上次我们已经将requests库封装成我们想要的样子了,我们的接口自动化已经完成了最开始的一步了,接下来我们需要完成我们相应的其他模块的封装,下面简单介绍下我们在接口自动化需要用到的模块吧在接口自动化 ...
- python(读取excel操作-xlrd模块)
一.安装xlrd模块 到python官网下载http://pypi.python.org/pypi/xlrd模块安装,前提是已经安装了python 环境. 或者在cmd窗口 pip install ...
- python(读取 excel 操作 xlrd 模块)
一.安装 xlrd 模块 到 python 官网下载 http://pypi.python.org/pypi/xlrd 模块安装,前提是已经安装了 python 环境. 或者在 cmd 窗口 pip ...
- Java 用jxl读取excel并保存到数据库(此方法存在局限,仅限本地电脑操作,放在服务器上的项目,需要把文件上传到服务器,详细信息,见我的别的博客)
项目中涉及到读取excel中的数据,保存到数据库中,用jxl做起来比较简单. 基本的思路: 把excel放到固定盘里,然后前段页面选择文件,把文件的名字传到后台,再利用jxl进行数据读取,把读取到的数 ...
随机推荐
- raptor
raptor - 必应词典 美['ræptər]英['ræptə(r)] n.猛禽:攫禽 网络迅猛龙:雷电威龙:决战侏罗纪
- 兼容多浏览器的网页复制插件(ZeroClipboard)
前言: 常规利用JS编写的网页复制功能是最简单的方法,但是只对IE有效,无法做到兼容其它浏览器,对其他浏览器也就只能弹窗提示用户手动复制了. <script type="text/ja ...
- PHP下ajax跨域的解决方案之jsonp
首先要说明一下json和jsonp的区别? json是一种基于文本的数据交换方式,或者叫做描述数据的一种格式. var person = { "name": "test& ...
- JSP页面中的EL表达式介绍
1.什么是EL? Expression Language(表达式语言) 2.EL的功能 替代JSP页面中数据访问时的复杂编码. 3.EL的特点 自动转换类型 使用简单 4.EL表达式的语法 ${EL ...
- spring开发Eclipse需要做设置
1. 统一工作空间的编码,选择UTF-8 2. 把创建JSP页面的编码修改UTF-8 3. 重新配置Tomcat服务器 * 先配置Tomcat服务器 * 选择服务器 --> open --> ...
- Silverlight实用窍门系列:57.Silverlight中的Binding使用(二)-数据验证
本文将简单讲述Silverlight中的Binding数据时的数据验证. NotifyOnValidationError:是否在出现异常/错误信息的时候激发BindingValidationError ...
- Golang之单元测试
文件名必须以_test.go结尾 使用go test 执行单元测试 例 package main func add(a, b int) int { return a + b } func sub(a, ...
- geoserver 文件系统
我介绍了GeoServer的一些重要的资源以及它们的访问接口,现在来看看它们的保存形式.GeoServer的数据没有保存到数据库,而是文件系统,这让我们的学习轻松不少.默认情况下,GeoServer的 ...
- ffmpeg 入门教程
1.在terminal,安装ffmpeg开源库 #brew install ffmpeg 2.新建一个目录first_ffmpeg,并在first_ffmpeg目录下新建ffmpeg目录 3.拷贝/u ...
- C++之类和对象的使用(一)
对象的初始化 在声明类时直接对数据成员初始化是错误的!下面的例子时错误的!! class Time{ hour =; minitu=; sec=; } //因为类并不是一个实体,而是一种抽象类型,并不 ...