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进行数据读取,把读取到的数 ...
随机推荐
- C++强制转换
static_cast,dynamic_cast, const_cast, reinterpret_cast dynamic_cast比static_cast多了安全检测,判断源和目标有无继承被继承关 ...
- day4:vcp考试
Q61. Which two statements are true regarding Virtual SAN Fault Domains? (Choose two.)A. They enable ...
- 查看端口号根据pid号找到相关占用端口应用
查看端口号根据pid号找到相关占用端口应用 8080 端口被占用不知道被哪个应用软件占用,下面我来教你查出那个该死的应用 方法/步骤 1 首先用netstat 找到端口对应的pid号,找到之后 ...
- 获取APP的启动图 -Launch Image
http://adad184.com/2015/10/15/tips-access-current-launch-image/
- OC 线程操作 - GCD使用 -线程通讯, 延迟函数和一次性代码
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ // [self downImag ...
- 【SQL模板】二.创建表视图模板TSQL
---Name: 创建表视图模板.sql ---Purpose: 用于创建 数据库中 新的数据表/视图 ---Author: xx ---Time: 2015-12-18 10:26:06 ---Re ...
- yii2中的rules 自定义验证规则详解
yii2的一个强大之处之一就是他的Form组件,既方便又安全.有些小伙伴感觉用yii一段时间了,好嘛,除了比tp"难懂"好像啥都没有. 领导安排搞一个注册的功能,这家伙刷刷刷的又是 ...
- vue-awesome-swiper插件
http://www.cnblogs.com/songrimin/p/6905136.html 这个地址不错 在上一些我的demo代码 第一步安装 npm install vue-awesome-sw ...
- How to add libraries to “External Libraries” in WebStorm/PhpStorm/Intellij
Stack Overflow Questions Developer Jobs Tags Users Log In Sign Up Join Stack Overflow to learn, sh ...
- 转载 springboot 配置读取
前言:了解过spring-Boot这个技术的,应该知道Spring-Boot的核心配置文件application.properties,当然也可以通过注解自定义配置文件**.properties的信息 ...