2016-09-18 76 views
0

我正在嘗試使用OleDb讀取excel文件,但總是拋出一個異常。我怎麼能這樣做?異常嘗試通過OleDb讀取excel文件?

試圖

private void btnRead_Click(object sender, EventArgs e) { 
      try { 
       OleDbConnection conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + 
                  FILE_PATH + ";Extended Properties=\\Excel 8.0;HDR=YES;IMEX=1\""); 
       String sql = "select * from [alunos.unid2.xls$]"; 
       OleDbCommand command = new OleDbCommand(sql, conn); 

       conn.Open(); 
       OleDbDataReader rs = command.ExecuteReader(); 
       while (rs.NextResult()) { 
        //Console.WriteLine(rs["ALU_NOME"]); 
        strBuilder.Append(rs["ALU_NOME"]); 
       } 
       conn.Close(); 
      }catch (Exception ex) { 
       Console.WriteLine(ex.Message); 
      } 

     } 

異常

Format of the initialization string does not conform to specification starting at index 130. 

回答

1
OleDbConnection conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + FILE_PATH + ";Extended Properties='Excel 8.0;HDR=YES;IMEX=1;'"); 

你缺少開盤報價的Extended Properties

相關問題