C# 如何从Excel中获取列表名?

C# 如何从Excel中获取列表名?,c#,excel,oledb,oledbcommand,C#,Excel,Oledb,Oledbcommand,如何从Excel文件中获取列表名?我从Excel读取的实际脚本如下: DataSet da = new DataSet(); OleDbDataAdapter adapter = new OleDbDataAdapter(); string name = "PP_s_vypocty"; // I actually using manualy name for read, but i want extract name from file. string FileName = fullpath;

如何从Excel文件中获取列表名?我从Excel读取的实际脚本如下:

DataSet da = new DataSet();
OleDbDataAdapter adapter = new OleDbDataAdapter();
string name = "PP_s_vypocty"; // I actually using manualy name for read, but i want extract name from file.
string FileName = fullpath;
string _ConnectionString = string.Empty;
string _Extension = Path.GetExtension(FileName);
// Checking for the extentions, if XLS connect using Jet OleDB
if (_Extension.Equals(".xls", StringComparison.CurrentCultureIgnoreCase))
{
    _ConnectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0; Data Source={0};Extended Properties=Excel 8.0", FileName);
}
// Use ACE OleDb
else if (_Extension.Equals(".xlsx", StringComparison.CurrentCultureIgnoreCase))
{
    _ConnectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=Excel 8.0", FileName);
}

OleDbConnection con = new OleDbConnection(_ConnectionString);
string strCmd = "SELECT J38 FROM " + name;
OleDbCommand cmd = new OleDbCommand(strCmd, con);

try
{
     con.Open();
     da.Clear();
     adapter.SelectCommand = cmd;
     adapter.Fill(da);
     UniqueValue.money.Add(double.Parse(da.ToString()));
}

catch (Exception ex)
{
    MessageBox.Show(ex.ToString());
}


finally
{
    con.Close();
}
我想从excel列表中提取名称,无需手动定义


您可以使用OleDbConnection.GetSchema返回一个datatable,其中包含数据库包含的表列表(本例中为excel工作表)