2014-02-11 54 views
1

如何從Excel文件中獲取列表名稱?我從讀取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列表的名稱,而無需手動定義。

回答

0

您可以使用OleDbConnection.GetSchema返回一個DataTable,與表的表(Excel工作表中的情況下),該數據庫包含

+0

http://msdn.microsoft.com/it-它/庫/ ms254934(v = vs.110)的.aspx – bdn02