如何使用 OleDbDataAdapter 在 c# 中获取所有 excel sheet 名称
how can get all excel sheet name in c# using OleDbDataAdapter
_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 的解决方案
OleDbDataAdaptor
与Sheet名字无关; Sheet 个名称属于 Excel.Workbook,Excel 个工作簿属于 Excel.Application。
您需要遍历工作簿 sheet 名称:
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,在这种情况下(如果你需要从工作中读取数据sheet)你可以只从 Excel 读取到二维数组(第一维)是行,第二个维度是列):
object[,] data = ws.UsedRange.Value2; // change UsedRange range to your table range, and you can also use ws.UsedRange.FormulaR1C1
_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 的解决方案
OleDbDataAdaptor
与Sheet名字无关; Sheet 个名称属于 Excel.Workbook,Excel 个工作簿属于 Excel.Application。
您需要遍历工作簿 sheet 名称:
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,在这种情况下(如果你需要从工作中读取数据sheet)你可以只从 Excel 读取到二维数组(第一维)是行,第二个维度是列):
object[,] data = ws.UsedRange.Value2; // change UsedRange range to your table range, and you can also use ws.UsedRange.FormulaR1C1