简体   繁体   English

以编程方式将SQL Server存储过程的结果保存为Excel格式

[英]Saving results of a SQL Server stored procedure to an Excel format programatically

I need to save the results of a SQL Server stored procedure into an Excel file. 我需要将SQL Server存储过程的结果保存到Excel文件中。 How can I do this programatically, as part of the same stored procedure - preferably as the last step of the stored procedure? 作为同一存储过程的一部分,我如何以编程方式进行此操作-最好作为存储过程的最后一步?

Please note that I do not have a separate web application. 请注意,我没有单独的Web应用程序。 I want to perform this save functionality to Excel, as part of the stored procedure itself. 我想将此存储功能执行到Excel,作为存储过程本身的一部分。

This is a bit long for a comment. 这有一段评论的时间。

My first suggestion is to save the data as a delimited file, which can easily be imported into Excel. 我的第一个建议是将数据另存为定界文件,可以轻松将其导入Excel。 This should be fine for archival purposes, for instance, or for sending the results around by email. 例如,对于存档目的或通过电子邮件发送结果,这应该很好。

The next suggestion is to save the results in a table, and to have a data connection to that table. 下一个建议是将结果保存在表中,并与该表建立数据连接。 You can then have a query in the Excel file that automatically refreshes the data. 然后,您可以在Excel文件中查询,以自动刷新数据。 This is handy for having special formatting and charts in the Excel file. 这对于在Excel文件中具有特殊格式和图表非常方便。

Another option is to use openrowset() to directly write to an Excel file. 另一个选择是使用openrowset()直接写入Excel文件。 The process would often be something like copying a template file to a new location and then writing the data to it. 该过程通常类似于将模板文件复制到新位置,然后将数据写入其中。

The best method really depends on what you want to do with the results. 最好的方法实际上取决于您要如何处理结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM