导入
如果表已存在,SQL语句为:
insert into aa select * from opendatasource('Microsoft.Jet.OLEDB.4.0',
'Data Source=D:/aa.xls;Extended Properties="Excel 8.0"')...[sheet1$]
如果表不存在,SQL语句为:
select * into aa From opendatasource('Microsoft.Jet.OLEDB.4.0',
'Data Source=D:/aa.xls;Extended Properties="Excel 8.0"')...[sheet1$]
可能会发生的异常:
如果发生“链接服务器"(null)"的 OLEDB访问接口 "Microsoft.Jet.OLEDB.4.0"报错。提供程序未给出有关错误的任何信息。
无法初始化链接服务器"(null)"的 OLEDB访问接口 "Microsoft.Jet.OLEDB.4.0"的数据源对象。”异常可能是excel文件关闭.
如果发生"不能将值 Null插入列 'Grade',表 'student.dbo.StuGrade';列不允许有空值。 Insert失败
语句已终止。” 异常,则可能是excel文件与数据库表中的字段不匹配
以上操作的是office 2003 ,如果要操作office 2007则需采用如下方式
如果表已存在,SQL语句为:
insert into aa select * from opendatasource('Microsoft.Ace.OLEDB.12.0',
'Data Source=D:/aa.xls;Extended Properties="Excel 12.0"')...[sheet1$]
如果表不存在,SQL语句为:
select * into aa From opendatasource('Microsoft.Ace.OLEDB.12.0',
'Data Source=D:/aa.xls;Extended Properties="Excel 12.0"')...[sheet1$]
导出
使用 insert into openrowset('Microsoft.Jet.OLEDB.4.0','Excel 5.0;HDR=YES;IMEX=YES;Database=D:/aa.xls',[sheet1$]) select * from StuGrade
可以将数据导出到 excel2003中,但前提必须是表已经存在,字段名都已有且与表对应。而使用下面的自动创建文件和表头,又会发生异常,插不进去。目前看来只能一条一条插。 解决这个问题可以先创建
一个excel文件并添加表头,可以使用下面的语句:
引用网址:http://blog.csdn.net/qygaojiansheng/archive/2009/04/26/4126364.aspx