C#读取EXL中的数据步骤案例

编程入门 行业动态 更新时间:2024-10-15 10:12:45

C#读取EXL中的数据<a href=https://www.elefans.com/category/jswz/34/1771434.html style=步骤案例"/>

C#读取EXL中的数据步骤案例

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.OleDb;
using System.Data;

namespace Excel操作
{
class Program
{
static void Main(string[] args)
{
//excel文件路径
string fileName = @"d:\文档\visual studio 2017\Projects\MyTest\Excel操作\装备信息.xls";
//连接字符串
//,xls后缀时用以下连接字符串
// "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + fileName + ";" + ";Extended Properties=\"Excel 8.0;HDR=YES;IMEX=1\"";
//.xlsx后缀时用以下连接字符串
// "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + fileName + ";" + ";Extended Properties=\"Excel 12.0;HDR=YES;IMEX=1\"";
//
string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + fileName + ";" + ";Extended Properties=\"Excel 8.0;HDR=YES;IMEX=1\"";
//连接oledb
OleDbConnection connetction = new OleDbConnection(connectionString);
connetction.Open();
//查询语句
string sql = "select * from [Sheet1$]";
//将查询结果放入adapter数据适配器中
OleDbDataAdapter adapter = new OleDbDataAdapter(sql, connetction);
//建立一个空的Dataset数据集
DataSet dataSet = new DataSet();
//将adapter中的数据Fill到dataSet中
adapter.Fill(dataSet);
//关闭连接,此时查询结果数据已经在dataset中了
connetction.Close();
//取得dataSet中的tables集合
DataTableCollection tableCollection = dataSet.Tables;
//取得第一张table
DataTable table = tableCollection[0];
//取得table中的所有row
DataRowCollection rowCollection = table.Rows;
//遍列所有row中的信息(不括标题字段)
foreach (DataRow row in rowCollection)
{
for (int i = 0; i < 8; i++)
{
Console.Write(row[i] + " ");
}
Console.WriteLine();
}
Console.ReadKey();
}
}
}

转载于:.html

更多推荐

C#读取EXL中的数据步骤案例

本文发布于:2024-02-27 15:09:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1707051.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:步骤   案例   数据   EXL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!