使用Winform C#从Excel向组合框添加值
本文关键字:组合 添加 Excel Winform 使用 | 更新日期: 2023-09-27 18:25:37
下面附有Excel使用的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"是组合框。
如何在组合框中获取列"C"的特定行中excel中的值。
您确定没有错误吗?看起来扩展属性的起始引号缺少
";Extended Properties=Excel 8.0;HDR=Yes'";
应该是:
";Extended Properties='Excel 8.0;HDR=Yes'";