您的位置:首页 > 编程语言 > C#

C# 读取xls格式的文件

2016-07-07 11:14 351 查看
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";
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: