2011-04-09 5 views
0

나는 C#의 코드를 사용하여 Access 데이터베이스로 Excel 시트를 가져와야하는 C# .net의 Windows 응용 프로그램에서 작업하고 있습니다. 나는 그물에 다음 코드를 발견하고 사용하려고 :Excel에서 데이터 가져 오기 C# .net에 의해 데이터베이스에 액세스하려면

string path = @"D:\project_excel"; 
     OleDbConnection con; 
     System.Data.DataTable dt = null; 
     //Connection string for oledb 
     string conn = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + path + "; Extended Properties=Excel 8.0;"; 
     con = new OleDbConnection(conn); 
     try 
     { 
      con.Open(); 

      dt = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null); 
      String[] excelsheets = new String[dt.Rows.Count]; 
      int i = 0; 

      foreach (DataRow dr in dt.Rows) 
      { 
       excelsheets[i] = dr["TABLE_NAME"].ToString(); 
       i++; 
      } 
      // here i manaually give the sheet number in the string array 
      DataSet ds = new DataSet(); 
      foreach (string temp in excelsheets) 
      { 
       // Query to get the data for the excel sheet 
       //temp is the sheet name 
       string query = "select * from [" + temp + "]"; 
       OleDbDataAdapter adp = new OleDbDataAdapter(query, con); 
       adp.Fill(ds, temp);//fill the excel sheet data into a dataset ds 
      } 

     } 
     catch (Exception ex) 
     { 

     } 
     finally 
     { 
      con.Close(); 
     } 

를 그 아래에 언급 된 예외를 제공하지만 :

'D 파일을 열 수 없습니다 Microsoft Jet 데이터베이스 엔진 : \ project_excel를 '. 이미 다른 사용자가 독점적으로 열었거나 해당 데이터를 볼 수있는 권한이 필요합니다.

확장 속성의 의미를 알지 못합니다. Microsoft Office 2007 패키지를 사용하고 있습니다. 확장 속성 = 7.0을 설정하면 다음 오류가 발생합니다.

설치 가능한 ISAM을 찾을 수 없습니다.

일부 코드 샘플을 제공해주십시오.

미리 감사드립니다.

+0

코드를 실행하는 동안 파일이 Excel에서 열려 있습니까? – Oded

답변

0

Access 데이터베이스 버전에 맞게 연결 문자열이 올바른지 확인하십시오.

자세한 내용은 오류

모하메드 Inshafudeen J.

0

에 의해 ConnectionString.com

방문의 경우 : 설치 가능한 ISAM을 찾을 수 없습니다.

1 : 엑셀은 "엑셀 97-2003 wrokbook" 2로 파일을 저장 : 또한 참조 "Microsoft.Office.Interop.Excel"

을 포함 행운을 빕니다!

관련 문제