2014-02-11 4 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

당신은 데이터베이스가

+0

http://msdn.microsoft.com/it- 포함 (귀하의 경우 엑셀 워크 시트) 테이블의 목록이있는 데이터 테이블을 반환 OleDbConnection.GetSchema을 사용할 수 있습니다 it/library/ms254934 (v = vs.110) .aspx – bdn02

관련 문제