List<string> list1 = new List<string>();
//创建连接字符串
string constr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Data\EPCPosition.xls;Extended Properties=Excel 8.0";
using (OleDbConnection con = new OleDbConnection(constr))
{
string sql = "select * from [sheet1$]";
using (OleDbCommand cmd = new OleDbCommand(sql_11, con))
{
using (OleDbDataReader reader = cmd.ExecuteReader())
{
con.Open();
if (reader.HasRows)
{
while (reader.Read())
{
list1.Add(reader.GetString(0));
}
}
}
}
}
注意:
//读取access数据库不同的地方在于access数据库是
string constr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Data\EPCPosition.xls;Persist Security Info=false";
//xls读取时连接字符串如下
string constr = @"Provider=Microsoft.JET.OLEDB.4.0;Data Source=C:\Data\EPCPosition.xls;Extended Properties=Excel 8.0";
C# 读取xls格式的文件
最新推荐文章于 2025-08-18 17:07:04 发布
本文介绍如何利用C#编程语言结合OLEDB技术来读取Excel文件中的数据,并将其存储到List<string>中。文章提供了完整的代码示例,包括连接字符串的配置以及通过OleDbCommand执行SQL查询的方法。

1253

被折叠的 条评论
为什么被折叠?



