SQL直接操作excel表(查询/导入/插入)

前端之家收集整理的这篇文章主要介绍了SQL直接操作excel表(查询/导入/插入)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

<div class="codetitle"><a style="CURSOR: pointer" data="5395" class="copybut" id="copybut5395" onclick="doCopy('code5395')"> 代码如下:

<div class="codebody" id="code5395">
--配置权限
EXEC sp_configure 'show advanced options',1;
GO
RECONFIGURE;
GO
EXEC sp_configure 'Ad Hoc Distributed Queries',1;
GO
RECONFIGURE;
GO

sql SERVER 和EXCEL的数据导入导出

1、在sql SERVER里查询Excel数据

:
-- ======================================================
<div class="codetitle"><a style="CURSOR: pointer" data="97109" class="copybut" id="copybut97109" onclick="doCopy('code97109')"> 代码如下:
<div class="codebody" id="code97109">
SELECT
FROM OpenDataSource( 'Microsoft.Jet.OLEDB.4.0',
'Data Source="c:\book1.xls";User ID=Admin;Password=;Extended properties=Excel 5.0')...[Sheet1$]
下面是个查询的示例,它通过用于 Jet 的 OLE DB 提供程序查询 Excel 电子表格。
SELECT

FROM OpenDataSource ( 'Microsoft.Jet.OLEDB.4.0',
'Data Source="c:\Finance\account.xls";User ID=Admin;Password=;Extended properties=Excel 5.0')...xactions

-------------------------------------------------------------------------------------------------

2、将Excel的数据导入sql server

:
-- ======================================================
<div class="codetitle"><a style="CURSOR: pointer" data="75082" class="copybut" id="copybut75082" onclick="doCopy('code75082')"> 代码如下:
<div class="codebody" id="code75082">
SELECT into newtable
FROM OpenDataSource( 'Microsoft.Jet.OLEDB.4.0',
'Data Source="c:\book1.xls";User ID=Admin;Password=;Extended properties=Excel 5.0')...[Sheet1$]

实例:
<div class="codetitle"><a style="CURSOR: pointer" data="14603" class="copybut" id="copybut14603" onclick="doCopy('code14603')"> 代码如下:
<div class="codebody" id="code14603">
SELECT
into newtable
FROM OpenDataSource( 'Microsoft.Jet.OLEDB.4.0',
'Data Source="c:\Finance\account.xls";User ID=Admin;Password=;Extended properties=Excel 5.0')...xactions

-------------------------------------------------------------------------------------------------

3、将sql SERVER中查询到的数据导成一个Excel文件


-- ======================================================
T-sql代码
<div class="codetitle"><a style="CURSOR: pointer" data="96041" class="copybut" id="copybut96041" onclick="doCopy('code96041')"> 代码如下:<div class="codebody" id="code96041">
EXEC master..xp_cmdshell 'bcp 库名.dbo.表名out c:\Temp.xls -c -q -S"servername" -U"sa" -P""'

参数:S 是sql服务器名;U是用户;P是密码
说明:还可以导出文本文件等多种格式
实例:
<div class="codetitle"><a style="CURSOR: pointer" data="78287" class="copybut" id="copybut78287" onclick="doCopy('code78287')"> 代码如下:<div class="codebody" id="code78287">
EXEC master..xp_cmdshell 'bcp saletesttmp.dbo.CusAccount out c:\temp1.xls -c -q -S"pmserver" -U"sa" -P"sa"'
EXEC master..xp_cmdshell 'bcp "SELECT au_fname,au_lname FROM pubs..authors ORDER BY au_lname" queryout C:\ authors.xls -c -Sservername -Usa -Ppassword'

在VB6中应用ADO导出EXCEL文件代码
<div class="codetitle"><a style="CURSOR: pointer" data="4990" class="copybut" id="copybut4990" onclick="doCopy('code4990')"> 代码如下:<div class="codebody" id="code4990">
Dim cn As New ADODB.Connection
cn.open "Driver={sql Server};Server=WEBSVR;DataBase=WebMis;UID=sa;WD=123;"
cn.execute "master..xp_cmdshell 'bcp "SELECT col1,col2 FROM 库名.dbo.表名" queryout E:\DT.xls -c -Sservername -Usa -Ppassword'"

------------------------------------------------------------------------------------------------

4、在sql SERVER里往Excel插入数据

:
-- ======================================================
<div class="codetitle"><a style="CURSOR: pointer" data="24880" class="copybut" id="copybut24880" onclick="doCopy('code24880')"> 代码如下:<div class="codebody" id="code24880">
insert into OpenDataSource( 'Microsoft.Jet.OLEDB.4.0',
'Data Source="c:\Temp.xls";User ID=Admin;Password=;Extended properties=Excel 5.0')...table1 (A1,A2,A3) values (1,2,3)

T-sql代码
<div class="codetitle"><a style="CURSOR: pointer" data="91768" class="copybut" id="copybut91768" onclick="doCopy('code91768')"> 代码如下:<div class="codebody" id="code91768">
INSERT INTO
OPENDATASOURCE('Microsoft.JET.OLEDB.4.0',
'Extended Properties=Excel 8.0;Data source=C:\training\inventur.xls')...[Filiale1$]
(bestand,produkt) VALUES (20,'Test')

原文链接:https://www.f2er.com/mssql/63561.html
excel表sqlsql

猜你在找的MsSQL相关文章