執行以下語句可以查詢Excel內容:
SELECT * FROM OPENDATASOURCE('Microsoft.Jet.OLEDB.4.0','Data Source=C:\Book1.xls;Extended Properties=Excel 8.0')...[T_TABLE$]
其中:C:\Book1.xls為所需匯入的Excel檔案路徑,T_TABLE$為Excel的表格名稱。(勿漏了$這個符號),Excel 8.0這個為Excel的版本號碼,Office97-2000的版本號碼為8.0
如,匯入資料可以這樣寫:在Sql server的查詢分析器裡鍵入
insert into [t_table]( [u_name] )
SELECT * FROM OPENDATASOURCE('Microsoft.Jet.OLEDB.4.0','Data Source=C:\Book1.xls;Extended Properties=Excel 8.0')...[T_TABLE$]
MS提供的方法有:
SELECT * INTO XLImport3 FROM OPENDATASOURCE('Microsoft.Jet.OLEDB.4.0',
'Data Source=C:\test\xltest.xls;Extended Properties=Excel 8.0')...[Customers$]
SELECT * INTO XLImport4 FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0',
'Excel 8.0;Database=C:\test\xltest.xls', [Customers$])
SELECT * INTO XLImport5 FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0',
'Excel 8.0;Database=C:\test\xltest.xls', 'SELECT * FROM [Customers$]')
具體可參見:
http://support.microsoft.com/default.aspx?scid=kb;zh-cn;321686