Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从Excel手册到字典中阅读两列内容<;ID,字符串>;?_C#_Asp.net_Excel - Fatal编程技术网

C# 从Excel手册到字典中阅读两列内容<;ID,字符串>;?

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”的工作表,前两列

我必须使用C#从Excel中读取两列(ID和值)到字典。您能告诉我如何完成此任务吗?

我建议使用外部免费库EPPLUS: 它非常快速和可靠(这是办公自动化或使用oledb所不能做到的)。
只需读入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();
        }
    }