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 项目地址

ExcelDataReader nuget包管理

ExcelDataReader.DataSet nuget包管理

C# 读取Excel,一波华丽的操作的更多相关文章

  1. C#操作Excel(2)-- 打开-读取Excel文档

    由于要为某软件实现导出Excel功能,故有此文. 本文的开发环境是Visual Studio 2010 ,C#, Excel 2007. 新建C#工程后打开Solution Explorer,可以看到 ...

  2. c# .Net :Excel NPOI导入导出操作教程之读取Excel文件信息及输出

    c# .Net :Excel NPOI导入导出操作教程之读取Excel文件信息及输出 using NPOI.HSSF.UserModel;using NPOI.SS.UserModel;using S ...

  3. C#操作Excel文件(读取Excel,写入Excel)

    看到论坛里面不断有人提问关于读取excel和导入excel的相关问题.闲暇时间将我所知道的对excel的操作加以总结,如今共享大家,希望给大家可以给大家带了一定的帮助.另外我们还要注意一些简单的问题1 ...

  4. c#dev操作读取excel方法

    一:使用spreadsheetControl1 方法 1:打开excel; private void barButtonItem1_ItemClick(object sender, DevExpres ...

  5. C# 操作Excel基础篇(读取Excel、写入Excel)

    注意事项:Excel的数据表中最多只能储存65535行数据,超出后,需要将数据分割开来进行储存.同时对于Excel中的乱码象限,是由于编码的错误方式导致引起的! 一.读取Excel数据表,获得Data ...

  6. 接口自动化--读取Excel操作(openpyxl)

    上次我们已经将requests库封装成我们想要的样子了,我们的接口自动化已经完成了最开始的一步了,接下来我们需要完成我们相应的其他模块的封装,下面简单介绍下我们在接口自动化需要用到的模块吧在接口自动化 ...

  7. python(读取excel操作-xlrd模块)

    一.安装xlrd模块 到python官网下载http://pypi.python.org/pypi/xlrd模块安装,前提是已经安装了python 环境. 或者在cmd窗口  pip install  ...

  8. python(读取 excel 操作 xlrd 模块)

    一.安装 xlrd 模块 到 python 官网下载 http://pypi.python.org/pypi/xlrd 模块安装,前提是已经安装了 python 环境. 或者在 cmd 窗口  pip ...

  9. Java 用jxl读取excel并保存到数据库(此方法存在局限,仅限本地电脑操作,放在服务器上的项目,需要把文件上传到服务器,详细信息,见我的别的博客)

    项目中涉及到读取excel中的数据,保存到数据库中,用jxl做起来比较简单. 基本的思路: 把excel放到固定盘里,然后前段页面选择文件,把文件的名字传到后台,再利用jxl进行数据读取,把读取到的数 ...

随机推荐

  1. 对象池 object pool

    对象池适用于: 对象的创建很耗时 对象频繁地释放再创建 对象池的实现:将释放的对象放进对象池中,在新建对象时,从对象池取对象 public class ObjectPool<T> wher ...

  2. phpStudy3——往数据库中添加数据

    前言: 前边介绍了查询数据库的方法,这里介绍下往数据库中添加数据的方法. 项目需求: 用户在前端页面输入的用户名和手机号码,点击提交后后端判断手机号码是否已经存在.如果不存在,那么插入数据库到数据库, ...

  3. 'WebElement' object is not iterable

    checkbox.html源码: <html> <head> <meta http-equiv="content-type" content=&quo ...

  4. 安全运维 -- 更改ssh端口

    环境:Ubuntu 16 前言 黑客遍地都是,ssh/pop3/ftp等爆破工具的流行让站长的日常运维工作量大大加重.Metasplot,Bruter等工具更是针对以上协议有专门 的破解方法,有字典破 ...

  5. Gitlab不小心关闭了sign-in,无法登录web的坑。。。

    手贱一不小心用root在gitlab后台把登录功能给关了,当时我就懵逼了. 解决方法如下: #进入数据库修改配置[root@gitlab-server ~]# gitlab-psql gitlabhq ...

  6. MySql中4种批量更新的方法

    最近在完成MySql项目集成的情况下,需要增加批量更新的功能,根据网上的资料整理了一下,很好用,都测试过,可以直接使用. mysql 批量更新共有以下四种办法 1..replace into 批量更新 ...

  7. qt基本类

    多firstpage secondpage thirdpage fouthpage  实现 多页面 xml解析 实现 按钮 和 slot实现 mysql数据库访问实现

  8. 【附案例】UI交互设计不会做?设计大神带你开启动效灵感之路

    随着网络技术的创新发展,如今UI交互设计应用越来越广泛,显然已经成为设计的主流及流行的必然趋势.UI界面交互设计中的动效包括移动,滑块,悬停效果,GIF动画等.UI界面交互设计为何越来越受到青睐?它有 ...

  9. C变参数函数demo

    #include <stdio.h> #include <stdarg.h> int sum(int a,...) {     int temp = 0,sum=0,count ...

  10. 使用JDBC连接MySql时出现:The server time zone value '�й���׼ʱ��' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration

    在连接字符串后面加上?serverTimezone=UTC 其中UTC是统一标准世界时间. 完整的连接字符串示例:jdbc:mysql://localhost:3306/test?serverTime ...