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进行数据读取,把读取到的数 ...
随机推荐
- Windows phone自定义控件(无外观控件)——FlipPanel
编码前 无外观自定义控件的定义在上一篇中已经有了,至于这一篇的自定义控件,比之前多加入了状态的变化,就像默认的Button具有Pressed.Normal等状态.在状态转变的同时可以加上一些动画,可以 ...
- 对象池 object pool
对象池适用于: 对象的创建很耗时 对象频繁地释放再创建 对象池的实现:将释放的对象放进对象池中,在新建对象时,从对象池取对象 public class ObjectPool<T> wher ...
- 早上突然看明白 shader和材质球的关系
计算机的世界不外乎 指令+数据 shader即Gpu指令,材质即数据
- 旋转链表(所有元素往右移) rotate list
[抄题]: 给定一个链表,旋转链表,使得每个节点向右移动k个位置,其中k是一个非负数 样例 给出链表1->2->3->4->5->null和k=2 返回4->5-& ...
- ios - 设置一个VC的navigationController的显示图案或文字,其他navigationController依旧不变
1. override func viewDidLoad() { super.viewDidLoad() self.navigationController?.delegate = self } 2. ...
- sublime 配置python环境
1. 在工具栏点击Preferences,打开Browse Packages.在打开的文件夹中找到Python,并打开这个文件夹.找到文件Python.sublime-build,并打开. 2. 修改 ...
- php的无刷新实现方法
方法一: 我们通过http的204状态码,页面不跳转. 1.html代码如下: <!DOCTYPE HTML> <html lang="zh-CN"> &l ...
- css3阴影效果
http://blog.csdn.net/freshlover/article/details/7610269
- linux下一些常用系统命令
查看系统打开的文件数 lsof|wc -l 查看当前目录下的文件数 find -type f | wc -l 查看某个目录下的文件数,注意这里/home包括其所有子目录 find /home -typ ...
- 7.11 cookie 失效后 ,重新登陆 页面 可能跳出 框架 ,只剩主题 部分 ,
判断地址 不在 框架里 (项目 地址栏一般 都是 首页地址 ) function url(){ var page=getpage(); if(window==top&&(page ...