附上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”是组合框。
如何将列“C”的特定行中的excel中存在的值存入ComboBox。
答案 0 :(得分:0)
您确定没有错误吗? 看起来您的扩展属性的起始引号缺失
";Extended Properties=Excel 8.0;HDR=Yes'";
应该是:
";Extended Properties='Excel 8.0;HDR=Yes'";