[英]How to export the results of a stored procedure directly to a MS Excel file?
I found a way to extract the contents of a MS Sql table directly (faster) to excel. 我找到了一种将MS Sql表的内容直接(更快)提取到excel的方法。 But I do not know how to do this with a stored procedure that requires parameters. 但是我不知道如何使用需要参数的存储过程来执行此操作。 Is it possible to extract, directly, to an Excel File the results of a stored procedure? 是否可以将存储过程的结果直接提取到Excel文件中? I know how to do it indirectly (using a data table) but it is too slow. 我知道如何间接执行此操作(使用数据表),但是它太慢了。 Thank you very much. 非常感谢你。
PS: This is the method I was using to do some tests. PS:这是我用来做一些测试的方法。 It works with a table, but what I need is to extract the result of a stored procedure: 它与表一起工作,但是我需要提取存储过程的结果:
Private Sub SqlToExcelTest2(ByVal excelFilePath As String, _
ByVal nonExistingSheetName As String, _
ByVal sqlServer As String, _
ByVal sqlDatabase As String, _
ByVal sqlUserName As String, _
ByVal sqlPassword As String, _
ByVal sqlTable As String)
Const excelConnStrTemplate As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=""Excel 8.0;HDR=YES;IMEX=2"";"
Dim connStr As String = String.Format(excelConnStrTemplate, _
excelFilePath)
Const adoQueryTemplate As String = "SELECT * INTO {0} FROM [odbc;Driver={{SQL Server}};" & _
"Server={1};Database={2};UID={3};PWD={4}].[{5}] "
Dim query As String = String.Format(adoQueryTemplate, _
nonExistingSheetName, _
sqlServer, _
sqlDatabase, _
sqlUserName, _
sqlPassword, _
sqlTable)
Using oleConn As New OleDb.OleDbConnection(connStr), oleCmd As New OleDb.OleDbCommand(query, oleConn)
oleConn.Open()
oleCmd.ExecuteNonQuery()
oleConn.Close()
End Using
End Sub
I do not have Excel right now to check, but you might try: 我现在没有Excel可以检查,但是您可以尝试:
and take a look at the generated VBA code. 并查看生成的VBA代码。
If you always run the same query (or few different ones), then creating few of such data sources with the auto-refresh on startup could be all you need. 如果您始终运行相同的查询(或少数几个不同的查询),那么仅需要在启动时使用自动刷新来创建此类数据源中的几个即可。
Is Python an option for you? Python是否适合您?
A contrived stored procedure that takes a parameter and returns a result set: 人为设计的存储过程,需要一个参数并返回结果集:
create procedure usp_Temp @howmany tinyint
as
set nocount on
create table #tmp (ID int identity, Letter char(1), String varchar(50))
declare @i tinyint
set @i = 0
while @i < @howmany
begin
insert into #tmp (Letter, String) values('X', 'The quick brown fox...')
set @i = @i + 1
end
select * from #tmp
set nocount off
In Python, you could do something like this: 在Python中,您可以执行以下操作:
import dbi
import odbc
import csv
conn_str = 'Driver={SQL Server};Server=MyServer;Database=MyDb;Uid=MyLogin;Pwd=MyPwd'
conn = odbc.odbc(conn_str)
curs = conn.cursor()
curs.execute('exec usp_temp @howmany = 15')
results = curs.fetchall()
curs.close()
conn.close()
writer = csv.writer(open('tmp.csv', 'wb'), delimiter = ',',
quoting = csv.QUOTE_MINIMAL)
for result in results: writer.writerow(result)
Excel should be able to open up that CSV file without a problem. Excel应该能够毫无问题地打开该CSV文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.