最近在處理exel的一些東西,遇到了不少問題,如今就在此將問題和網上找到的解決辦法html
1.外部表不是預期格式錯誤web
Excel 「External table is not in the expected format.」 .sql
Question:數據庫
I'm trying to read an Excel (xlsx) file using the code shown below. I get an "External table is not in the expected format." error unless I have the file already open in Excel. In other words, I have to open the file in Excel first before I can read if from my C# program. The xlsx file is on a share on our network. How can I read the file without having to open it first? Thankswindows
string sql = "SELECT * FROM [Sheet1$]"; string excelConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + pathname + ";Extended Properties=/"Excel 8.0;HDR=YES;IMEX=1;/""; using (OleDbDataAdapter adaptor = new OleDbDataAdapter(sql, excelConnection)) { DataSet ds = new DataSet(); adaptor.Fill(ds); } 服務器
Answers:架構
"External table is not in the expected format." typically occurs when trying to use an Excel 2007 file with a connection string that uses: Microsoft.Jet.OLEDB.4.0 and Extended Properties=Excel 8.0app
Using the following connection string seems to fix most problems.less
public static string path = @"C:/src/RedirectApplication/RedirectApplication/301s.xlsx"; public static string connStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;";
以上出處:http://blog.csdn.net/ldl22847/article/details/6322339ide
不少人換了2010後,問的最多的問題之一是2003裏最經典的ADO中的「provider=Microsoft.Jet.OLEDB.4.0」這句怎麼不能用了。
百度一下能夠了解到,Microsoft.Jet.OLEDB.4.0是Microsoft Jet引擎,這適用於2003版本(2003以前的我沒裝,因此也不知道能向下適應到哪一個版本),而在2007中,微軟對其旗下 Access 與 Excel 的主要文件格式進行修改,而且重命名爲 .accdb(Access 2007 數據庫文件)與 .xlsx(Excel 2007 文件),所以未被 Microsoft Jet 引擎所支持,不過微軟也很快的提出了Microsoft Office 2007 Desktop Drivers: Data Connectivity Components 來支持,目前的解決方法就是把鏈接字符串中的數據提供者改成Microsoft.ACE.OLEDB.12.0。
//2003(Microsoft.Jet.Oledb.4.0)
string strConn = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;'", excelFilePath);
//2010(Microsoft.ACE.OLEDB.12.0)
string strConn = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;'", excelFilePath);
其實,這種錯誤的可能性有幾種,好比:
1.沒有安裝數據訪問組件,須要安裝相應版本的數據訪問組件;
2.沒有安裝相應版本的Office客戶端,須要安裝相應版本的Office客戶端;
3.Microsoft.Jet.OLEDB.4.0 在64位系統上不支持,須要修改架構,從x64改成x86,不管是WinForm仍是ASP.NET;或者修改鏈接字符串爲 Microsoft.ACE.OLEDB.12.0而且安裝AccessDatabaseEngine x64數據訪問組件;
4.沒有在IIS應用程序池配置默認屬性,須要在相應的IIS應用程序池啓用32位應用程序;
下面是我收集整理的一些解決方案。
【錯誤】:未在本地計算機上註冊「microsoft.ACE.oledb.12.0」提供程序。
說明: 執行當前 Web 請求期間,出現未處理的異常。請檢查堆棧跟蹤信息,以瞭解有關該錯誤以及代碼中致使錯誤的出處的詳細信息。
異常詳細信息: System.InvalidOperationException: 未在本地計算機上註冊「microsoft.ACE.oledb.12.0」提供程序。
下載Microsoft 2007 Office System 驅動程序:數據鏈接組件安裝
此下載將安裝一組組件,非 Microsoft Office 應用程序可使用它們從 2007 Office system 文件中讀取數據,例如從 Microsoft Office Access 2007(mdb 和 accdb)文件以及 Microsoft Office Excel 2007(xls、xlsx 和 xlsb)文件中讀取數據。這些組件還支持與 Microsoft Windows SharePoint Services 和文本文件創建鏈接。
此外,還會安裝 ODBC 和 OLEDB 驅動程序,供應用程序開發人員在開發與 Office 文件格式鏈接的應用程序時使用。
支持的操做系統: Windows Server 2003 Service Pack 1, Windows Vista, Windows XP Service Pack 2
在windows 2008 r2 x64版本的操做系統中經過oledb提供程序鏈接訪問Excel2007/2003時,程序會報以下錯誤:
System.InvalidOperationException: The 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine.
此查找資料認爲產生此種錯誤的緣由有兩種:
一、在Windows2008r2 x64 OS,在沒有裝Office2007客戶端時,或沒有安裝「數據鏈接組件」
二、當前Web應用程序的應用程序池是64位的Sharepoint應用程序池下時,由於提供鏈接Excel的provider不支持當前64版本的操做系統
解決方案:
方案1:
安裝MS的數據鏈接組件AccessDatabaseEngine(或安裝office 2007客戶端),並將當前承載經過oledb提供程序去訪問Excel2007的Web application對應的應用程序池「啓用兼容32位應用程序」,
在對應的 IIS 應用程序池中,「設置應用程序池默認屬性」右擊/「高級設置」/"啓用32位應用程序",設置爲 true。
方案2:
安裝AccessDatabaseEngine_64版而且要安裝64位版的office 2010客戶端
方案3:
若目標應用程序不能將應用程序池"啓用兼容32位應用程序",只能在64位應用程序池下運行,且又想有上傳Excel的功能,可按以下方式:
a、建立Asp.net WebService application 程序,並經過WebService來鏈接訪問Excel。
b、將此webservice application的應用程序池按方案1方式設置「啓用兼容32位應用程序」。
c、在目標應用程序中添加此webservice。
本文來自cnblogs:http://www.cnblogs.com/q149072205/archive/2012/03/29/2423323.html
今天我把系統換成了64位版的Windows7,結果配完IIS打開網站測試的時候,頓時菊花一緊:未在本地計算機上註冊「Microsoft.Jet.OLEDB.4.0」提供程序。
查了半天發現是IIS跑在64位環境下引發的。而64位下,是木有Access數據庫的驅動的(包括Excel也不行)。
解決辦法是:在目標網站的應用程序池中選擇高級設置,而後將啓用32位應用程序設置爲true,重啓IIS便可!
因爲項目尚未結束,暫時先記錄這些,等後來遇到問題再來補充