2012-07-19 160 views
4

我已經搜遍了網絡,發現很多人問這個問題,但沒有人確定我的答案。System.Data.OleDb.OleDbException:無法找到可安裝的ISAM

我有一個連接類和一個在頁面中使用該類的方法。

DataConn.cs

public static OleDbConnection ConnectExcel() 
{ 
    //Store the connection details as a string 
    string connstr = 
     String.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=pricelist.xlsx;Extended Properties=Excel 12.0 Xml;HDR=YES"); 

    //Initialise the connection to the server using the connection string. 
    OleDbConnection oledbConn = new OleDbConnection(connstr); 

    //Open the connection, we do this here so we can instantly be able to use SQL commands in the code. 
    oledbConn.Open(); 

    return oledbConn; 
} 

public static void DisconnectExcel() 
{ 
    _oledbConn.Dispose(); 
    _oledbConn.Close(); 
} 

並調用它

protected void Page_Load(object sender, EventArgs e) 
{ 
    // Connection String 
    const string xlStr = "SELECT * FROM [Sheet2$]"; 

    // Create OleDbCommand object and select data from worksheet Food 
    OleDbCommand cmd = new OleDbCommand(xlStr, DataConn.ConnectExcel()); 

    // Create new OleDbDataAdapter 
    OleDbDataAdapter oleda = new OleDbDataAdapter(); 

    oleda.SelectCommand = cmd; 

    // Create a DataSet which will hold the data extracted from the worksheet. 
    DataSet ds = new DataSet(); 

    // Fill the DataSet from the data extracted from the worksheet. 
    oleda.Fill(ds); 

    // Bind the data to the GridView 
    gridPricelist.DataSource = ds; 
    gridPricelist.DataBind(); 
} 

是的,我仍然獲得代碼:

System.Data.OleDb.OleDbException:找不到可安裝的ISAM。

任何人都可以請幫忙嗎?

+0

在這個問題右邊的相關欄目中,您會發現幾十個與您的問題相同的問題。 – Steve 2012-07-19 13:53:28

回答

14

如果您使用多個擴展屬性,則必須引用值標記,否則驅動程序無法將它們與連接字符串中的其他非擴展屬性區分開來;

...Extended Properties=""Excel 8.0;IMEX=1""" 

修改連接字符串

String.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=pricelist.xlsx;Extended Properties=""Excel 12.0 Xml;HDR=YES"""); 

參考: Could not find installable ISAM

+0

完美!感謝您的幫助。 :D – TheGeekZn 2012-07-19 18:03:13

+0

@NewAmbition歡迎你 – 2012-07-19 18:07:59

+0

仍然幫助afetr 1.5年 – 2014-02-06 06:58:08

0

如果已安裝的LibreOffice尋找cli_basetypes.dll,cli_cppuhelper.dll,cli_oootypes.dll,cli_uno.dll ,cli_ure.dll,cli_uretypes.dll然後添加對您的項目的引用(與LibreOffice API一起工作),我還安裝了「用於Word,Excel和PowerPoint Fil的Microsoft Office兼容包e格式「和」Microsoft Access數據庫引擎2010可再發行組件「(要在沒有完整Office安裝的情況下獲取ACE.OLEDB.12.O連接)。這是VB Sample的一部分,其中我連接到oledb以創建一些查詢。

OpenFileDialog.Filter = "Spreadsheets (*.xls*)|*.xls*" 
    OpenFileDialog.Multiselect = False 
    Try 
     If (OpenFileDialog.ShowDialog() = System.Windows.Forms.DialogResult.OK) Then 
      objOffice = CreateObject("com.sun.star.ServiceManager") 'preparar instancia libreOffice (prepare libreOffice instance) 
      instOffice = objOffice.createInstance("com.sun.star.frame.Desktop") 
      Dim obj(-1) As Object 
      Dim myDoc = instOffice.loadComponentFromURL("file:///" & OpenFileDialog.FileName.Replace("\", "/"), "_default", 0, obj) 
      Dim hojas = myDoc.getSheets().getElementNames() 'Obtener nombres de las hojas de calculo (get Spreadsheet names) 
      System.Threading.Thread.Sleep(1000) 'Esperar a que termine la instancia Office (await libreOffice thread) 
      myDoc.Close(True) 

      Dim MyConnection As System.Data.OleDb.OleDbConnection 'Preparar conexión para realizar consulta tipo sql (preparing connection) 
      Dim DtSet As System.Data.DataSet 
      Dim MyCommand As System.Data.OleDb.OleDbDataAdapter 

      If OpenFileDialog.FileName.ToUpper.Contains(".XLSX") Then 
       MyConnection = New System.Data.OleDb.OleDbConnection("provider=Microsoft.ACE.OLEDB.12.0;Data Source='" & OpenFileDialog.FileName & "';Extended Properties='Excel 12.0 Xml;HDR=YES;IMEX=1;'") 
      Else 
       MyConnection = New System.Data.OleDb.OleDbConnection("provider=Microsoft.ACE.OLEDB.12.0;Data Source='" & OpenFileDialog.FileName & "';Extended Properties='Excel 12.0;HDR=YES;IMEX=1'") 
      End If 
相關問題