Question

How can I get list name from Excel file? My actual script for read from Excel is this:

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();
}

I want to extract name from excel list without manually definition.

Was it helpful?

Solution

You can use OleDbConnection.GetSchema that return a datatable with the list of tables (excel worksheet in your case) that the database contains

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top