查看: 106|回覆: 1

[综合] Select data from an Excel sheet in MSSQL

[複製鏈接]

2

主題

0

回帖

0

積分

热心网友

金币
0
閲讀權限
220
精華
0
威望
0
贡献
0
在線時間
0 小時
註冊時間
2010-12-27
發表於 2023-11-13 00:00:00 | 顯示全部樓層 |閲讀模式

select * 
from openrowset('Microsoft.Jet.OLEDB.4.0'
,'Excel 8.0;HDR=YES;IMEX=1;DATABASE=d:\folder\excel.xls',Sheet1$)

-------NOTE------
Setting IMEX=1 tells the driver to use Import mode. In this state, the registry setting ImportMixedTypes=Text will be noticed. This forces mixed data to be converted to text. For this to work reliably, you may also have to modify the registry setting, TypeGuessRows=8. The ISAM driver by default looks at the first eight rows and from that sampling determines the datatype. If this eight row sampling is all numeric, then setting IMEX=1 will not convert the default datatype to Text; it will remain numeric. 

You must be careful that IMEX=1 not be used indiscriminately. This is IMPORT mode, so the results may be unpredictable if you try to do appends or updates of data in this mode. 

The possible settings of IMEX are: 
0 is Export mode
1 is Import mode
2 is Linked mode (full update capabilities) 

回覆

使用道具 舉報

0

主題

2092

回帖

1萬

積分

琼殿精英

金币
10581
閲讀權限
220
精華
0
威望
0
贡献
0
在線時間
0 小時
註冊時間
2011-10-11
發表於 2026-6-13 07:58:12 | 顯示全部樓層
感谢楼主的分享!这个方法确实很实用,正好我最近也在研究SQL Server导入Excel数据的问题。

不过我有个小问题想请教一下:如果Excel文件比较大或者Sheet名称不确定的话,有什么好的方法可以先获取所有的Sheet名称呢?我记得好像需要查询系统表还是有什么特殊的写法?

另外关于那个注册表设置TypeGuessRows的问题,确实很关键。之前我导入一个Excel文件时,前几行都是数字,结果后面有文本数据就被截断了,后来改成TypeGuessRows=0才解决。

总之很实用的教程,收藏了!希望圆梦公社里多些这样的技术干货帖,大家一起进步~

圆梦公社 加油!
回覆

使用道具 舉報

您需要登錄後才可以回帖 登錄 | 立即注册

本版積分規則

圆梦公社,专注于为全球华人提供纯粹技术交流的地方,请勿发布任何政治及违法的言论。如有相关侵权、举报、投诉及建议等,请发 E-mail:dzh188@hotmail.com

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.

在本版发帖返回顶部