-
C#教程之C#教程之C# 读取Excel,一波华丽的操作
本站最新发布 C#从入门到精通
试听地址 https://www.xin3721.com/eschool/CSharpxin3721/
试听地址 https://www.xin3721.com/eschool/CSharpxin3721/
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[0]; // The result of each spreadsheet is in result.Tables } } } } }
ExcelDataReader 项目地址
ExcelDataReader nuget包管理
ExcelDataReader.DataSet nuget包管理
栏目列表
最新更新
如何使用OS模块中的stat方法
Python os 模块
seek() 方法
python打开文件实例1
Python写入文件
什么是流?
文件操作如何进制逐行读取
Python相对路径
with创建临时运行环境
Python文件操作
.Net Standard(.Net Core)实现获取配置信息
Linux PXE + Kickstart 自动装机
Shell 编程 基础
Shell 编程 条件语句
CentOS8-网卡配置及详解
Linux中LVM逻辑卷管理
1.数码相框-相框框架分析(1)
Ubuntu armhf 版本国内源
Linux中raid磁盘阵列
搭建简易网站
access教程之Access简介
mysql 安装了最新版本8.x版本后的报错:
Mysql空间数据&空间索引(spatial)
如何远程连接SQL Server数据库的图文教程
复制SqlServer数据库的方法
搜索sql语句
sql中返回参数的值
sql中生成查询的模糊匹配字符串
数据定义功能
数据操作功能