C# 从Excel手册到字典中阅读两列内容<;ID,字符串>;?
我必须使用C#从Excel中读取两列(ID和值)到字典。您能告诉我如何完成此任务吗?我建议使用外部免费库EPPLUS: 它非常快速和可靠(这是办公自动化或使用oledb所不能做到的)。C# 从Excel手册到字典中阅读两列内容<;ID,字符串>;?,c#,asp.net,excel,C#,Asp.net,Excel,我必须使用C#从Excel中读取两列(ID和值)到字典。您能告诉我如何完成此任务吗?我建议使用外部免费库EPPLUS: 它非常快速和可靠(这是办公自动化或使用oledb所不能做到的)。 只需读入via ePlus,使用ToDataTable()方法并将数据表读入词典。我建议使用外部免费库ePlus: 它非常快速和可靠(这是办公自动化或使用oledb所不能做到的)。 只需读入via ePlus,使用ToDataTable()方法并将数据表读入词典。假设您有一张名为“Country”的工作表,前两列
只需读入via ePlus,使用ToDataTable()方法并将数据表读入词典。我建议使用外部免费库ePlus: 它非常快速和可靠(这是办公自动化或使用oledb所不能做到的)。
只需读入via ePlus,使用ToDataTable()方法并将数据表读入词典。假设您有一张名为“Country”的工作表,前两列中有代码和名称 还有excel 2010
using System.Data.OleDb;
static void Main()
{
string excl_connection_string = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\CountryCode.xlsx;Extended Properties=""Excel 12.0 Xml;HDR=YES""";
string sql = "SELECT * FROM [Country$]";
OleDbConnection con = new OleDbConnection(excl_connection_string);
OleDbCommand cmd = new OleDbCommand(sql, con);
try
{
con.Open();
OleDbDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
Console.WriteLine("Country Code = {0}, Name= {1}", reader.GetString(0), reader.GetString(1));
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
con.Dispose();
}
}
假设您有一个名为“Country”的工作表,前两列中有代码和名称 还有excel 2010
using System.Data.OleDb;
static void Main()
{
string excl_connection_string = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\CountryCode.xlsx;Extended Properties=""Excel 12.0 Xml;HDR=YES""";
string sql = "SELECT * FROM [Country$]";
OleDbConnection con = new OleDbConnection(excl_connection_string);
OleDbCommand cmd = new OleDbCommand(sql, con);
try
{
con.Open();
OleDbDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
Console.WriteLine("Country Code = {0}, Name= {1}", reader.GetString(0), reader.GetString(1));
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
con.Dispose();
}
}