2010-12-16 7 views
0

excel 파일을 datagridview로 가져 오는 방법을 보여주는 YouTube 비디오를 발견했습니다. 오류가 발생합니다. 설치 가능한 ISAM을 찾을 수 없습니다.import excel file error

여기 내 코드는 무엇입니까?

private void button1_Click(object sender, EventArgs e) 
    { 
     OleDbConnection conn = new OleDbConnection(); 
     conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Nick\Desktop\Pricing2.xlsx" + @";Exended Properties=""Excel 8.0;HDR=No;IMEX=1;ImportMixedTypes=Text;TypeGuessRows=0"""; 

     OleDbCommand command = new OleDbCommand 
     (
      "SELECT PartNumber,ShortDescription,RetailPrice,JobberPrice,BasePrice,YourDiscount,YourPrice,LongDescription FROM [Pricing$]",conn 
     ); 
     DataSet ds = new DataSet(); 
     OleDbDataAdapter adapter = new OleDbDataAdapter(command); 
     adapter.Fill(ds); 
     dataGridView1.DataSource = ds.Tables[0]; 
    } 

답변

0

당신은 당신의 연결 문자열에 오타가 있습니다

Exended Properties= 

가되어야한다

Extended Properties= 

는 또한 당신에게 당신의 코드에 약간의 개선을 추천 할 것을 일회용 자원을 적절히 처분하는 것입니다 :

using (var conn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Nick\Desktop\Pricing2.xlsx;Extended Properties=""Excel 12.0 Xml;HDR=YES"";")) 
using (var cmd = conn.CreateCommand()) 
{ 
    conn.Open(); 
    cmd.CommandText = "SELECT PartNumber,ShortDescription,RetailPrice,JobberPrice,BasePrice,YourDiscount,YourPrice,LongDescription FROM [Pricing$]"; 
    using (var adapter = new OleDbDataAdapter(cmd)) 
    { 
     adapter.Fill(ds); 
    } 
} 
+0

당신은 신입니다, 감사합니다. –

0

내 경험으로. 오직 97-2003 작품을 능가합니다.

VB에서 코드!

Private Sub DodajExcel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DodajExcel.Click 
OpenFileDialog2.Filter = "Excel Files (*.xls)|*.xls|All Files (*.*)|*.*" 
If OpenFileDialog2.ShowDialog = Windows.Forms.DialogResult.OK Then 
    Dim XLSPathx As String = OpenFileDialog2.FileName 
End If 

Dim connectionStringTemplate As String = _ 
"Provider=Microsoft.ACE.OLEDB.12.0;" + _ 
"Data Source={0};" + _ 
"Extended Properties=""Excel 12.0;HDR=Yes;IMEX=1""" 
Dim XLSPath As String = OpenFileDialog2.FileName 
Dim connectionString As String = String.Format(connectionStringTemplate, XLSPath) 
Dim sqlSelect As String = "SELECT * FROM [Arkusz1$];" 
' Load the Excel worksheet into a DataTable 
Dim workbook As DataSet = New DataSet() 
Dim excelAdapter As System.Data.Common.DataAdapter = New System.Data.OleDb.OleDbDataAdapter(sqlSelect, connectionString) 
Try 
    excelAdapter.Fill(workbook) 
    Dim worksheet As DataTable = workbook.Tables(0) 
    DataGridView1.DataSource = worksheet 


Catch 
    End Try 

최종 하위