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. 【校招面试 之 C/C++】第18题 C++ 中的隐式转换以及explicit关键字

    1.什么是隐式转换: 众所周知,C++的基本类型中并非完全的对立,部分数据类型之间是可以进行隐式转换的. 所谓隐式转换,是指不需要用户干预,编译器私下进行的类型转换行为.很多时候用户可能都不知道进行了 ...

  2. 【转】Hadoop HDFS分布式环境搭建

    原文地址  http://blog.sina.com.cn/s/blog_7060fb5a0101cson.html Hadoop HDFS分布式环境搭建 最近选择给大家介绍Hadoop HDFS系统 ...

  3. 【z】Storm - the world's best IDE framework for .NET

    http://www.codeproject.com/Articles/42799/Storm-the-world-s-best-IDE-framework-for-NET Storm - the w ...

  4. sql ltrim/rtrim 字段中为中文时出现?的问题

    字段存储为中文,类型为nvarchar,使用ltrim时结果集中出现的问号,我的解决办法是:将问号replace掉

  5. 2018年这些UI设计趋势正在流行,跟上必拿高薪!

    数字设计领域和时尚圈是一样的,总会有各种各样的趋势让人眼花缭乱.无论是用户界面的视觉元素,还是用户体验的细节,总有许多值得说道的新玩法和新方向.就目前来看,UI设计的大趋势是更加大胆新颖的视觉设计,通 ...

  6. IT 技术网站收集

    ## 脚本之家 http://www.jb51.net/ ## 菜鸟教程 http://www.runoob.com/ ## 编程中国 https://www.bccn.net/ ##

  7. Selenium安装中的一些问题及解决办法-软硕1703班3组整理分享

    非常感谢软件工程硕士1703班3组同学的热心,他们将安装Selenium过程中踩过的坑替大家填上了.希望还没有来得及踩坑的,或者掉进坑里还没爬出来的小组,能顺利跨过去这个安装的坑. 如下是原文. Se ...

  8. Angular 通过注入 $location 获取与修改当前页面URL

    //1.获取当前完整的url路径 var absurl = $location.absUrl(); //http://172.16.0.88:8100/#/homePage?id=10&a=1 ...

  9. 20155335俞昆《java程序设计》第七周总结

    学号  2016-2017-2  <Java程序设计>第X周学习总结 ##  教材学习内容总结 Lambda  的语法概览 String[]  names={“Justin”,”cater ...

  10. 使用reactjs做一个CRUD功能

    第一步:引入reactjs所依赖的js文件,本案例使用的是bootstrap前端框架,所以引入了相应的js和css文件 第二步:body里面添加两个div 第三步:开始编写reactjs脚本 < ...