如何将Excel工作表列名称填充到下拉列表中?

时间:2012-01-18 06:17:59

标签: asp.net c#-4.0 drop-down-menu

您好我有一个带标题的Excel工作表,我想将这些标题填充到下拉列表中......

任何人都可以帮助我使用select语句和程序

我正在处理一个代码,我知道它是否正确

      DropDownList list = new DropDownList();
                    string connectionstring = String.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 8.0;HDR=YES;IMEX=1;""", Excelpath);  
                    string query1 =  String.Format("select * from [{0}]", DDlist.SelectedItem.Text);                                             
                    // OleDbDataAdapter dataAdapter = new OleDbDataAdapter(query1, connectionstring);                                                
                    using (OleDbConnection conn1 = new OleDbConnection(connectionstring))
                    {
                        OleDbCommand odc1 = new OleDbCommand(string.Format(query1, conn1));
                        conn1.Open();
                        OleDbDataReader dr;
                        dr = odc1.ExecuteReader();
                        while (dr.Read())
                        {
                            list.Items.Add(dr[column.ColumnName].ToString());
                        }
                        dr.Close();
                        conn1.Close();
                    }

在这个方法中,我在这一行发现错误

    dr = odc1.ExecuteReader();Error:ExecuteReader: Connection property has not been initialized.

任何人都可以帮助你,希望提前

3 个答案:

答案 0 :(得分:0)

我认为你没有将Connection传递给OleDBCommand。所以试试,

OleDbCommand odc1 = new OleDbCommand(query1, conn1);

答案 1 :(得分:0)

OleDbCommand类有两个主要参数要传递才能正确执行。那就是Query和连接对象。在您的代码中,您正在传递连接,但它被放置在String.Format()内,因此将其放在外面。请参阅下面的代码......

OleDbCommand odc1 = new OleDbCommand(string.Format(query1, conn1)); 

OleDbCommand odc1 = new OleDbCommand(query1, conn1); 

答案 2 :(得分:0)

试试这可能有效......

                  foreach (DataTable table in DS.Tables)
          {
               foreach (DataColumn column in table.Columns)
                {
                   DropDownList list = new DropDownList();
                   string connectionstring = String.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 8.0;HDR=YES;IMEX=1;""", Excelpath);
                   string query1 = String.Format("select * from [{0}]", DDlist.SelectedItem.Text);                                                                      
                   using (OleDbConnection conn1 = new OleDbConnection(connectionstring))
                   {
                       OleDbCommand odc1 = new OleDbCommand(query1, conn1);
                       conn1.Open();
                        DataTable dte = null;
                       DataSet ds = new DataSet();
                       dte = conn1.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                       OleDbDataAdapter objAdapter1 = new OleDbDataAdapter();
                       objAdapter1.SelectCommand = odc1;
                       objAdapter1.Fill(ds, "xldata");
                        for (int i = 0; i < ds.Tables[0].Columns.Count; i++)
                       {
                           list.Items.Add(ds.Tables[0].Columns[i].ColumnName.ToString());
                       }
                       conn1.Close();
                    }