在木匠的眼中月亮也是木头做的。

欲达则重之。

 

sql 2012 操作EXCEL 2013

确认是否有Microsoft.ACE.OLEDB.12.0数据接口

 

 --进行配置:

EXEC sp_configure 'show advanced options', 1
GO
RECONFIGURE
GO
EXEC sp_configure 'Ad Hoc Distributed Queries', 1
GO
RECONFIGURE
GO
--允许在进程中使用ACE.OLEDB.12
EXEC master.dbo.sp_MSset_oledb_prop N'Microsoft.ACE.OLEDB.12.0', N'AllowInProcess', 1
--允许动态参数
EXEC master.dbo.sp_MSset_oledb_prop N'Microsoft.ACE.OLEDB.12.0', N'DynamicParameters', 1

 

--读取excel内容

SELECT *  FROM OpenDataSource('Microsoft.ACE.OLEDB.12.0','Data Source=d:\ceshi.xls;Extended properties="Excel 8.0;hdr=no"')...[sheet1$]  WHERE F5 ='订单金额'

注:最后一个参数为选取的excel工作薄名称;  F5为select结果集字段名

 

 

--  EXCEL 复制副本并更新某单元格内容

--先启用 XP_CMDSHELL命令

EXEC sp_configure 'show advanced options', 1
GO
RECONFIGURE
GO
EXEC sp_configure 'xp_cmdshell', 1
GO
RECONFIGURE
GO

---执行复制:

master.dbo.xp_cmdshell  'xcopy D:\excel\1.xlsx D:\mydoc\  /y/s '

--更新

update OpenRowSet('Microsoft.ACE.OLEDB.12.0', 'Excel 12.0;HDR=Yes;IMEX=0;Database=D:\mydoc\1.xlsx', 'select * from [sheet1$]') set F6='111' where F5 ='订单金额'

--插入一行

INSERT into OpenRowSet('Microsoft.ACE.OLEDB.12.0', 'Excel 12.0;HDR=Yes;IMEX=0;Database=D:\project\1.xls', 'select * from [sheet1$]')
select 1,1,1,1,1,1,1,1,1,1,1

注:插入字段数量和select字段数量一致

posted on 2016-10-28 13:58  popland  阅读(158)  评论(0编辑  收藏  举报

导航