将SQL查询数据导出到Excel
我有一个返回非常大的数据集的查询。我无法像平常那样将其复制并粘贴到 Excel 中。我一直在研究如何直接导出到 Excel 工作表。我正在运行 Microsoft Server 2003 的服务器上运行 SQL SERVER 2008。我正在尝试使用 Microsoft.Jet.OLEDB.4.0 数据提供程序和 Excel 2007。我拼凑了一小段代码,看起来像这样已经在例子中看到过。
INSERT INTO OPENDATASOURCE('Microsoft.Jet.OLEDB.4.0',
'Data Source=C:\Working\Book1.xlsx;Extended Properties=EXCEL 12.0;HDR=YES')
SELECT productid, price FROM dbo.product
然而这不起作用,我收到一条错误消息说
“关键字“SELECT”附近的语法不正确”。
有谁对如何做到这一点或可能有更好的方法有任何想法吗?
I have a query that returns a very large data set. I cannot copy and paste it into Excel which I usually do. I have been doing some research on how to export directly to an Excel sheet. I am running SQL SERVER 2008 on a server running Microsoft Server 2003. I am trying to use the Microsoft.Jet.OLEDB.4.0 data provider and Excel 2007. I've pieced together a small piece of code that looks like this from what I've seen in examples.
INSERT INTO OPENDATASOURCE('Microsoft.Jet.OLEDB.4.0',
'Data Source=C:\Working\Book1.xlsx;Extended Properties=EXCEL 12.0;HDR=YES')
SELECT productid, price FROM dbo.product
However this is not working, I am getting an error message saying
"Incorrect syntax near the keyword 'SELECT'".
Does anyone have any ideas about how to do this or possibly a better approach?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我不知道这是否是您要查找的内容,但您可以将结果导出到 Excel,如下所示:
在结果窗格中,单击左上角的单元格以突出显示所有记录,然后右键单击顶部- 左侧单元格并单击“结果另存为”。导出选项之一是 CSV。
您也可以尝试一下:
最后,您可以考虑使用 SSIS(取代 DTS)进行数据导出。以下是教程的链接:
http://www.accelebrate.com/sql_training/ssis_2008_tutorial.htm
== 更新 #1 ==
要将结果保存为带有列标题的 CSV 文件,可以按照以下步骤操作:
I don't know if this is what you're looking for, but you can export the results to Excel like this:
In the results pane, click the top-left cell to highlight all the records, and then right-click the top-left cell and click "Save Results As". One of the export options is CSV.
You might give this a shot too:
Lastly, you can look into using SSIS (replaced DTS) for data exports. Here is a link to a tutorial:
http://www.accelebrate.com/sql_training/ssis_2008_tutorial.htm
== Update #1 ==
To save the result as CSV file with column headers, one can follow the steps shown below:
如果您只需要导出到 Excel,则可以使用导出数据向导。
右键单击数据库,任务->导出数据。
If you're just needing to export to excel, you can use the export data wizard.
Right click the database, Tasks->Export data.
我遇到了类似的问题,但有一点不同 - 当结果集来自一个查询时,上面列出的解决方案有效,但在我的情况下,我有多个单独的选择查询,我需要将结果导出到 Excel。下面只是一个示例来说明,尽管我可以执行
name in
子句...向导允许我将一个查询的结果导出到 Excel,但在本例中并非不同查询的所有结果。
当我研究时,我发现我们可以禁用结果网格并启用结果文本。因此,按 Ctrl + T,然后执行所有语句。这应该在输出窗口中将结果显示为文本文件。您可以将文本处理为制表符分隔格式,以便导入到 Excel 中。
您还可以按 Ctrl + Shift + F 将结果导出到文件 - 它导出为 .rpt 文件,可以使用文本编辑器打开该文件并进行操作以进行 Excel 导入。
希望这对其他有类似问题的人有所帮助。
I had a similar problem but with a twist - the solutions listed above worked when the resultset was from one query but in my situation, I had multiple individual select queries for which I needed results to be exported to Excel. Below is just an example to illustrate although I could do a
name in
clause...The wizard was letting me export the result from one query to excel but not all results from different queries in this case.
When I researched, I found that we could disable the results to grid and enable results to Text. So, press Ctrl + T, then execute all the statements. This should show the results as a text file in the output window. You can manipulate the text into a tab delimited format for you to import into Excel.
You could also press Ctrl + Shift + F to export the results to a file - it exports as a .rpt file that can be opened using a text editor and manipulated for excel import.
Hope this helps any others having a similar issue.
对于来这里寻找如何在 C# 中执行此操作的人,我已经尝试了以下方法,并在
dotnet core 2.0.3
和entity Framework core 2.0 中取得了成功.3
首先创建您的模型类。
然后安装 EPPlus Nuget 包。 (我使用的版本是 4.0.5,可能也适用于其他版本。)
创建
ExcelExportHelper
类,它将包含将数据集转换为 Excel 行的逻辑。该类与您的模型类或数据集没有依赖关系。现在在您想要生成 Excel 文件的位置添加此方法,可能是控制器中的方法。您也可以为存储过程传递参数。 请注意,该方法的返回类型是
FileContentResult
。无论您执行什么查询,重要的是您必须在列表中获得结果
。更多详细信息可以在此处
For anyone coming here looking for how to do this in C#, I have tried the following method and had success in
dotnet core 2.0.3
andentity framework core 2.0.3
First create your model class.
Then install EPPlus Nuget package. (I used version 4.0.5, probably will work for other versions as well.)
The create
ExcelExportHelper
class, which will contain the logic to convert dataset to Excel rows. This class do not have dependencies with your model class or dataset.Now add this method where you want to generate the excel file, probably for a method in the controller. You can pass parameters for your stored procedure as well. Note that the return type of the method is
FileContentResult
. Whatever query you execute, important thing is you must have the results in aList
.More details can be found here
我发现您正在尝试将 SQL 数据导出到 Excel 以避免将非常大的数据集复制粘贴到 Excel 中。
您可能有兴趣了解如何将 SQL 数据导出到 Excel 并自动更新导出(使用任何 SQL 数据库:MySQL、Microsoft SQL Server、PostgreSQL)。
要将数据从 SQL 导出到 Excel,您需要执行以下 2 个步骤:
结果将是您想要将 SQL 数据库中的数据查询到 Excel 的表列表:

步骤 1:将 Excel 连接到外部数据源:您的 SQL 数据库
步骤 2:将 SQL 数据导入到Excel
要自动更新导出,还有 2 个附加步骤:
我创建了一个分步教程 关于整个过程,从连接 Excel 到 SQL,直到整个过程自动更新。您可能会找到详细说明和屏幕截图有用。
I see that you’re trying to export SQL data to Excel to avoid copy-pasting your very large data set into Excel.
You might be interested in learning how to export SQL data to Excel and update the export automatically (with any SQL database: MySQL, Microsoft SQL Server, PostgreSQL).
To export data from SQL to Excel, you need to follow 2 steps:
The result will be the list of tables you want to query data from your SQL database into Excel:

Step1: Connect Excel to an external data source: your SQL database
Step 2: Import your SQL data into Excel
To update the export automatically, there are 2 additional steps:
I’ve created a step-by-step tutorial about this whole process, from connecting Excel to SQL, up to having the whole thing automatically updated. You might find the detailed explanations and screenshots useful.