C# 把Excel中的数据批量导入DataSet中


    private void button1_Click(object sender, EventArgs e)
        {
            DataSet ds = new DataSet();

            string conn = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source='文件的绝对路径';Extended Properties='Excel 8.0;HDR=Yes;IMEX=1'";//连接字符串

            OleDbConnection MyConnection = new OleDbConnection(conn);//得到连接对象

            OleDbDataAdapter MyCommand = new OleDbDataAdapter("select * from [Sheet1$]", MyConnection);
           
            MyConnection.Open();

            MyCommand.Fill(ds, "exdtSource");
            this.dataGridView1.DataSource = ds.Tables["exdtSource"];//在dataGridView1中显示

            MyConnection.Close();

            foreach (DataRow p in ds.Tables["exdtSource"].Rows) {

                foreach (var i in p.ItemArray)
                {
                    Console.WriteLine(i);
                }
            }

你可能感兴趣的:(C#,Excel,dataset)