_conn = new OleDbConnection(_connectionStrting);
_conn.Open();
DataTable dt = _conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
String[] sheetNames = new String[dt.Rows.Count];
int i = 0;
foreach (DataRow row in dt.Rows)
{
sheetNames[i] = row["TABLE_NAME"].ToString();
comboBox2.Items.Add(sheetNames[i]);
i++;
}
_conn.Close();
这段代码对我有用,但我想知道是否有OleDbDataAdapter的解决方案
答案 0 :(得分:0)
OleDbDataAdaptor
与工作表名称无关;工作表名称属于Excel.Workbook,Excel工作簿属于Excel.Application。
您需要遍历工作簿表名称:
using Excel = Microsoft.Office.Interop.Excel;
Excel.Application xl = new Excel.Application();;
Excel.Workbook wb = xl.Workbooks.Open("WorkBookfullPath", 0, true);
foreach (Excel.Worksheet ws in wb.Worksheets) {
{
string wsName = ws.Name;
}
你真的不需要OleDbDataAdapter,在这种情况下(如果你需要从工作表中读取数据)你可以从Excel读取到2维数组(第一维是行,第二维是列):
object[,] data = ws.UsedRange.Value2; // change UsedRange range to your table range, and you can also use ws.UsedRange.FormulaR1C1