2011-10-09 35 views
0

我試圖讀取Html類型與C#代碼的Excel文件。 我收到'未指定的錯誤'。C#代碼讀取Excel(HTML導入)

這是我的連接字符串:

Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties='HTML Import; // c:\1.xls 

這是我的代碼:

private string GetTableName(OleDbConnection conn) 
{ 
    string tableName = null; 
    try 
    { 
     conn.Open(); 

     var dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null); 
     if (dt == null) 
     { 
      log.Error("Table schema is not available."); 
      return tableName; 
     } 

     tableName = dt.Rows[0]["TABLE_NAME"].ToString(); 
    } 
    catch (Exception e) 
    { 
     log.Warn(e); 
     return null; 
    } 
    finally 
    { 
     conn.Close(); 
    } 
    return tableName; 
} 

我找遍了整個互聯網和谷歌,沒有人有完全一樣的問題。

我想了解我的代碼有什麼問題,或者這是什麼意思'未指定的錯誤?

謝謝!

+0

如果我沒記錯的話,Windows XP Service Pack中的一個改變了圍繞噴墨驅動程序的安全設置並閱讀了html表格,但是我無法在我想到的msdn上找到該文檔。它基本上限制了讀取html表格的能力。如果我找到它,我會回來併發布鏈接。祝你好運 – Robert

+0

檢查http://support.microsoft.com/kb/240770 theres一節談論'DisabledExtensions' – Robert

回答

1

嘗試使用

Provider=Microsoft.ACE.OLEDB.12.0; 

編輯:

使用此連接字符串

"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0;HDR=YES;IMEX=1\";" 

HDR =是指頭部被視爲一個數據行,而不是列名(更改它取決於您的需要)

IMEX = 1指定該表包含混合數據

0

爲Excel 2007更改連接字符串作爲

Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 12.0 Xml;HDR=YES'; 

ACE OLEDB 12.0與Office 2007的發佈有可能使用Microsoft.ACE.OLEDB.12.0連接到舊的.xls(EXCEL 97-2003)工作簿也是如此。

+0

嗨,現在我得到「外部表格不是在預期的格式。」 – Idoshhh

+0

http://stackoverflow.com/questions/1139390/excel-external-table-is-not-in-the-expected-format – Damith