【问题标题】:Adding Value to ComboBox from Excel using Winform C#使用 Winform C# 从 Excel 向 ComboBox 添加值
【发布时间】:2015-12-04 17:03:27
【问题描述】:

下面附上 Excel Used Excel

   //Establish a Connection
            string XlxPath = @"C:\Users\BreakIn\Documents\Visual Studio 2010\Projects\Test\TestData\Test Data.xlsx";
            string XlxPathCon = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + XlxPath + ";Extended Properties=Excel 8.0;HDR=Yes'";
            OleDbCommand MyDataAdp = new OleDbCommand();
            OleDbConnection Xlxconn = new OleDbConnection(XlxPathCon);
            MyDataAdp.Connection = Xlxconn;

          //  Accessing Sheets
            Xlxconn.Open();
            DataTable xdt;
            // Get all Sheets in Excel File
            xdt = Xlxconn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
            Xlxconn.Close();

            DataSet ds = new DataSet();
            String SheetName = xdt.Rows[0]["TABLE_NAME"].ToString();
            MyDataAdp.CommandText = "SELECT Test From[" + SheetName + "]";

            OleDbDataAdapter da = new OleDbDataAdapter(MyDataAdp);
            da.SelectCommand = MyDataAdp;
            da.Fill(ds);

            Testdes.Items.Clear();
            foreach (DataRow value in xdt.Rows)

            {
                Testdes.DataSource = ds.Tables["SheetName"].Columns["test"];
                Testdes.Items.Add(value["test"]);
            }
           Testdes.SelectedIndex = 0;
        }
    }
}

}

其中“Testdes”是组合框。

如何将 Excel 中特定行中“C”列的值放入 ComboBox。

【问题讨论】:

  • 开始删除 Testdes.DataSource = ...
  • 如果您提供其他信息,例如:编译错误或运行时错误,提供答案可能更容易。
  • 没有报错,在没有给Combobox添加任何值的情况下运行代码

标签: c# windows excel winforms combobox


【解决方案1】:

您确定没有错误吗? 您的扩展属性的开头引号似乎丢失了

";Extended Properties=Excel 8.0;HDR=Yes'";

应该是:

";Extended Properties='Excel 8.0;HDR=Yes'";

【讨论】:

  • 谢谢,我现在已经更正了,代码运行到“DataTable xdt;”并打开包含组合框的文件。
  • 并且组合框不包含任何值
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-28
  • 1970-01-01
  • 2012-04-19
  • 2011-11-14
相关资源
最近更新 更多