2012-09-26 3 views
2

스프레드 시트에서 많은 데이터를 가져 오려고하지만 C# 코드에서 연결을 성공적으로 수행 할 수 없습니다. BC#에서 Excel 스프레드 시트에 연결 시도 중

아래는 연결 문자열과 연결에 사용하는 코드입니다. 이 프로그램의 목표는 스프레드 시트에서 데이터를 가져 와서 SQL 데이터베이스에 보관하는 것입니다. 나는 그러나이 오류 메시지 receiveing없이 connection.open() 명령을 번번이 수 : * .XLSX를 들어

"외부 테이블 예상 된 형식이 아닌"

 string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\nearod\Desktop\TestLoad.xlsx;Extended Properties='Excel 8.0;HDR=Yes;IMEX=1'"; 
     string queryString = "SELECT * FROM [SQL AgentUnique ID Test Load$]"; 
     try 
     { 
      OleDbDataReader reader; 
      using (OleDbConnection connection = new OleDbConnection(connectionString)) 
      { 

       OleDbCommand command = new OleDbCommand(queryString, connection); 
       connection.Open(); 
       reader = command.ExecuteReader(); 


       while (reader.Read()) 
       { 
        counter++; 

        //Access db values 
        string compCode = ""; 
        string agId = ""; 
        string fName = ""; 
        string lName = ""; 
        string nameSuffix = ""; 


        compCode = reader.GetValue(0).ToString(); 
        agId = reader.GetString(1); 
        fName = reader.GetString(2); 
        lName = reader.GetString(3); 
        nameSuffix = reader.GetString(4); 

        sqlComm.Parameters.Add(companyCode); 
        sqlComm.Parameters.Add(agentID); 
        sqlComm.Parameters.Add(firstName); 
        sqlComm.Parameters.Add(lastName); 
        sqlComm.Parameters.Add(suffix); 

        //Initialize connection objects 
        cm = Dts.Connections["QUAHILSQ03"]; 
        sqlConn = (SqlConnection)cm.AcquireConnection(Dts.Transaction); 
        sqlComm = new SqlCommand("AgentResourcesU01.dbo.sp_AgentIdAprCheck", sqlConn); 
        sqlComm.CommandType = CommandType.StoredProcedure; 

        //Execute stored procedure 
        sqlComm.ExecuteNonQuery(); 

       } 
       reader.Close(); 
       connection.Close(); 
       OleDbConnection.ReleaseObjectPool(); 
      } 
+0

은 BTW 당신은 ​​적절하게 구조화 된 쿼리 MS Access에서 바로 SQL 서버에 데이터 집합을로드 할 수 있어야합니다. – Fionnuala

+0

데이터를 올바르게 정렬하려면 저장 프로 시저를 호출해야합니다. 액세스에서이 작업을 수행 할 수 있습니까? – NealR

+0

MS Access에서 데이터를 확실히 정렬 할 수 있습니다. ACE 드라이버를 사용하여 Excel 레코드 세트를 정렬 할 수도 있습니다. Access에서 가장 가까운 저장 프로 시저에 해당하는 것은 쿼리입니다. – Fionnuala

답변

4

을, 당신은 에이스 드라이버가 필요합니다 : Microsoft.ACE.OLEDB.12.0

string connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0; 
Data Source=C:\Users\nearod\Desktop\TestLoad.xlsx; 
Extended Properties='Excel 8.0;HDR=Yes;IMEX=1'"; 
0

이 글은 오래 동안 작성되었습니다. 그것은 웹폼에, 그러나 .cs 파일은 당신이 필요합니다 보여줍니다 converting an excel spreadsheet to a dataset, datatable and multi-dimensional array

string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source="+FileToConvert+";Extended Properties=Excel 8.0;"; 
try 
{ 
    OleDbConnection connection = new OleDbConnection(connectionString); 
    connection.Open(); 
    //this next line assumes that the file is in default Excel format with Sheet1 as the first sheet name, adjust accordingly 
    OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", connection); 
    DataSet ds = new DataSet(); 
    DataTable dt = new DataTable(); 
    adapter.Fill(ds);//now you have your dataset ds now filled with the data and ready for manipulation 
    // do stuff 
} 
관련 문제