2014-12-08 1 views
0

Excel에서 C#으로 데이터에 액세스하려고합니다. 이상적으로는 데이터를리스트 또는 시리즈 콜렉션에 넣고 싶습니다. 이 튜토리얼 (http://www.aspsnippets.com/Articles/Read-Excel-file-using-OLEDB-Data-Provider-in-C-Net.aspx)을 사용하고있었습니다.C#에서 Excel 데이터에 액세스하는 방법

매우 도움이되었지만 데이터 어댑터 부분을 놓친 것 같습니다. 여기에 그의 예제를 따르는 코드가있다.

 string connectionString = null; 
     connectionString = "Provider = Microsoft.ACE.OLEDB.12.0; Data Source = P:\\Visual Studio 2012\\Projects\\SmartSheetAPI\\SmartSheetAPI\\bin\\Debug\\OUTPUT.xls; Extended Properties = 'excel 12.0 Xml; HDR=YES; IMEX=1;';";    

     //Establish Connection 
     string dataSource = "P:\\Visual Studio 2012\\Projects\\SmartSheetAPI\\SmartSheetAPI\\bin\\Debug\\OUTPUT.xls;"; 
     string excelConnection = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source = " + dataSource + " Extended Properties='Excel 8.0; HDR=Yes'"; 
     OleDbConnection connExcel = new OleDbConnection(connectionString); 
     OleDbCommand cmdExcel = new OleDbCommand(); 
     cmdExcel.Connection = connExcel; 

     //Accessing Sheets 
     connExcel.Open(); 
     DataTable dtExcelSchema; 
     dtExcelSchema = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null); 
     connExcel.Close(); 

     //access excel Sheets (tables in database) 
     DataSet dataset = new DataSet(); 
     string SheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString(); 
     cmdExcel.CommandText = "SELECT * From [" + SheetName + "]"; 
     da.SelectCommand = cmdExcel; 
     da.Fill(dataset); 
     connExcel.Close(); 

하단 세 줄을 보면 da.SelectCommand 및 da. 데이터 집합을 채우는 데 사용되는 것을 알 수 있습니다. 그러나 나는 이것이 데이터 어댑터를 필요로한다고 생각하며, 그의 예에서는 그렇게하지 않는다. 나는 아래와 같이 데이터 어댑터 만들기 시도 :

SqlDataAdapter dataadapter = new SqlDataAdapter(); 

을하지만 난라는 오류가 발생합니다 : 암시 '에는 System.Data.SqlClient.SqlCommand에 유형을'System.Data.OleDb.OleDbCommand '변환 할 수 없습니다.

select 문까지 제대로 작동한다는 것을 알고 있습니다. 누군가 내가 기본적으로 선택 진술에 들어있는 정보에 액세스 할 수 있기를 원합니다.

답변

1

SqlDataAdapter이 아닌 OleDBDataAdapter이 필요합니다. 그래서,이 작업을 수행합니다

OleDBDataAdapter da = new OleDBDataAdapter(cmdExcel); 
da.Fill(dataset); 

엑셀은 OLEDB 데이터 소스이며, 단지 데이터베이스 연결 및 조작에 대한 것과 같은 당신이 일반적으로 OleDb로 시작됩니다 사용해야하므로 클래스는, Sql로 시작된다. OLEDB 연결을 사용하여 엑셀 데이터를 액세스

Documentation

+0

오류 주어진 의미가 있습니다. – Danrex

2

Aspose .Usage은 우선은 프로젝트에 컨트롤의 참조를 추가 한 후 다음 코드를 시도 할 수 있습니다 매우 간단 같은 headache.You가 대신 타사 컨트롤을 시도 할 수 있습니다 항상 .

//Creating a file stream containing the Excel file to be opened 
FileStream fstream = new FileStream("C:\\book1.xls", FileMode.Open); 

//Instantiating a Workbook object 
//Opening the Excel file through the file stream 
Workbook workbook = new Workbook(fstream); 

//Accessing the first worksheet in the Excel file 
Worksheet worksheet = workbook.Worksheets[0]; 

//Exporting the contents of 7 rows and 2 columns starting from 1st cell to DataTable 
DataTable dataTable = worksheet.Cells.ExportDataTable(0, 0, 7, 2, true); 

//Binding the DataTable with DataGrid 
dataGrid1.DataSource = dataTable; 

//Closing the file stream to free all resources 
fstream.Close(); 
+0

감사합니다. – Danrex

관련 문제