简体   繁体   English

如何将SQL Server 2005数据传输或导出到Excel

[英]How do you transfer or export SQL Server 2005 data to Excel

I have a simple SQL 'Select' query, and I'd like to dump the results into an Excel file. 我有一个简单的SQL'选择'查询,我想将结果转储到Excel文件中。 I'm only able to save as .csv and converting to .xls creates some super ugly output. 我只能保存为.csv并转换为.xls会创建一些超级丑陋的输出。 In any case, as far as I can tell (using Google) this doesn't seem to be so straight forward. 无论如何,据我所知(使用谷歌),这似乎并不是那么直截了当。 Any help would be greatly appreciated. 任何帮助将不胜感激。

SSIS is a no-brainer for doing stuff like this and is very straight forward (and this is just the kind of thing it is for). SSIS是做这样的事情的明智之举,而且非常直接(这就是它的用途)。

  1. Right-click the database in SQL Management Studio 在SQL Management Studio中右键单击该数据库
  2. Go to Tasks and then Export data, you'll then see an easy to use wizard. 转到“任务”然后“导出数据”,您将看到一个易于使用的向导。
  3. Your database will be the source, you can enter your SQL query 您的数据库将是源,您可以输入您的SQL查询
  4. Choose Excel as the target 选择Excel作为目标
  5. Run it at end of wizard 在向导结束时运行它

If you wanted, you could save the SSIS package as well (there's an option at the end of the wizard) so that you can do it on a schedule or something (and even open and modify to add more functionality if needed). 如果您愿意,也可以保存SSIS包(在向导末尾有一个选项),以便您可以按计划或其他方式执行(甚至可以打开并修改以根据需要添加更多功能)。

Use "External data" from Excel. 使用Excel中的“外部数据”。 It can use ODBC connection to fetch data from external source: Data/Get External Data/New Database Query 它可以使用ODBC连接从外部源获取数据:数据/获取外部数据/新数据库查询

That way, even if the data in the database changes, you can easily refresh. 这样,即使数据库中的数据发生变化,您也可以轻松刷新。

I've found an easy way to export query results from SQL Server Management Studio 2005 to Excel. 我找到了一种将查询结果从SQL Server Management Studio 2005导出到Excel的简便方法。

1) Select menu item Query -> Query Options. 1)选择菜单项Query - > Query Options。

2) Set check box in Results -> Grid -> Include column headers when copying or saving the results . 2) 复制或保存结果时,在结果 - >网格 - > 包括列标题中设置复选框。

After that, when you Select All and Copy the query results, you can paste them to Excel, and the column headers will be present. 之后,当您选择全部并复制查询结果时,您可以将它们粘贴到Excel,并且列标题将存在。

See this 看到这个


This is by far the best post for exporting to excel from SQL: 到目前为止,这是从SQL导出到excel的最佳帖子:

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=49926 http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=49926

To quote from user madhivanan , 引用用户madhivanan

Apart from using DTS and Export wizard, we can also use this query to export data from SQL Server2000 to Excel 除了使用DTS和Export向导之外,我们还可以使用此查询将数据从SQL Server2000导出到Excel

Create an Excel file named testing having the headers same as that of table columns and use these queries 创建一个名为testing的Excel文件,其标题与表列的标题相同,并使用这些查询

1 Export data to existing EXCEL file from SQL Server table 1从SQL Server表将数据导出到现有的EXCEL文件

insert into OPENROWSET('Microsoft.Jet.OLEDB.4.0', 
    'Excel 8.0;Database=D:\testing.xls;', 
    'SELECT * FROM [SheetName$]') select * from SQLServerTable

2 Export data from Excel to new SQL Server table 2将数据从Excel导出到新的SQL Server表

select * 
into SQLServerTable FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0', 
    'Excel 8.0;Database=D:\testing.xls;HDR=YES', 
    'SELECT * FROM [Sheet1$]')

3 Export data from Excel to existing SQL Server table 3将数据从Excel导出到现有SQL Server表

Insert into SQLServerTable Select * FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0', 
    'Excel 8.0;Database=D:\testing.xls;HDR=YES', 
    'SELECT * FROM [SheetName$]')

4 If you dont want to create an EXCEL file in advance and want to export data to it, use 4如果您不想提前创建EXCEL文件并想要将数据导出到该文件,请使用

EXEC sp_makewebtask 
    @outputfile = 'd:\testing.xls', 
    @query = 'Select * from Database_name..SQLServerTable', 
    @colheaders =1, 
    @FixedFont=0,@lastupdated=0,@resultstitle='Testing details'

(Now you can find the file with data in tabular format) (现在您可以找到包含表格格式数据的文件)

5 To export data to new EXCEL file with heading(column names), create the following procedure 5要使用标题(列名称)将数据导出到新的EXCEL文件,请创建以下过程

create procedure proc_generate_excel_with_columns
(
    @db_name    varchar(100),
    @table_name varchar(100),   
    @file_name  varchar(100)
)
as

--Generate column names as a recordset
declare @columns varchar(8000), @sql varchar(8000), @data_file varchar(100)
select 
    @columns=coalesce(@columns+',','')+column_name+' as '+column_name 
from 
    information_schema.columns
where 
    table_name=@table_name
select @columns=''''''+replace(replace(@columns,' as ',''''' as '),',',',''''')

--Create a dummy file to have actual data
select @data_file=substring(@file_name,1,len(@file_name)-charindex('\',reverse(@file_name)))+'\data_file.xls'

--Generate column names in the passed EXCEL file
set @sql='exec master..xp_cmdshell ''bcp " select * from (select '+@columns+') as t" queryout "'+@file_name+'" -c'''
exec(@sql)

--Generate data in the dummy file
set @sql='exec master..xp_cmdshell ''bcp "select * from '+@db_name+'..'+@table_name+'" queryout "'+@data_file+'" -c'''
exec(@sql)

--Copy dummy file to passed EXCEL file
set @sql= 'exec master..xp_cmdshell ''type '+@data_file+' >> "'+@file_name+'"'''
exec(@sql)

--Delete dummy file 
set @sql= 'exec master..xp_cmdshell ''del '+@data_file+''''
exec(@sql)

After creating the procedure, execute it by supplying database name, table name and file path: 创建过程后,通过提供数据库名称,表名和文件路径来执行它:

EXEC proc_generate_excel_with_columns 'your dbname', 'your table name','your file path'

Its a whomping 29 pages but that is because others show various other ways as well as people asking questions just like this one on how to do it. 这是一个29页,但这是因为其他人展示了各种各样的方式以及人们提出的问题,就像这个问题一样。

Follow that thread entirely and look at the various questions people have asked and how they are solved. 完全遵循该主题并查看人们提出的各种问题以及如何解决这些问题。 I picked up quite a bit of knowledge just skimming it and have used portions of it to get expected results. 我只是略读了一些知识,并且已经使用了部分知识来获得预期的结果。

To update single cells 更新单个单元格

A member also there Peter Larson posts the following: I think one thing is missing here. Peter Larson的成员也发布了以下内容:我认为这里缺少一件事。 It is great to be able to Export and Import to Excel files, but how about updating single cells? 能够导出和导入到Excel文件非常棒,但更新单个单元格怎么样? Or a range of cells? 还是一系列细胞?

This is the principle of how you do manage that 这是您如何管理它的原则

update OPENROWSET('Microsoft.Jet.OLEDB.4.0', 
'Excel 8.0;Database=c:\test.xls;hdr=no', 
'SELECT * FROM [Sheet1$b7:b7]') set f1 = -99

You can also add formulas to Excel using this: 您还可以使用以下方法将公式添加到Excel:

update OPENROWSET('Microsoft.Jet.OLEDB.4.0', 
'Excel 8.0;Database=c:\test.xls;hdr=no', 
'SELECT * FROM [Sheet1$b7:b7]') set f1 = '=a7+c7'

Exporting with column names using T-SQL 使用T-SQL导出列名

Member Mladen Prajdic also has a blog entry on how to do this here 会员Mladen Prajdic也有一篇关于如何在这里做到这一点的博客文章

References: www.sqlteam.com (btw this is an excellent blog / forum for anyone looking to get more out of SQL Server). 参考文献: www.sqlteam.com (顺便说一句,对于希望从SQL Server中获取更多信息的人来说,这是一个出色的博客/论坛)。

If you are looking for ad-hoc items rather than something that you would put into SSIS. 如果您正在寻找ad-hoc项目而不是您将要放入SSIS的东西。 From within SSMS simply highlight the results grid, copy, then paste into excel, it isn't elegant, but works. 从SSMS内部只需突出显示结果网格,复制,然后粘贴到excel,它不优雅,但有效。 Then you can save as native .xls rather than .csv 然后你可以保存为.xls而不是.csv

Here's a video that will show you, step-by-step, how to export data to Excel. 这是一个视频,将逐步向您展示如何将数据导出到Excel。 It's a great solution for 'one-off' problems where you need to export to Excel: 对于需要导出到Excel的“一次性”问题,这是一个很好的解决方案:
Ad-Hoc Reporting 特设报告

It's a LOT easier just to do it from within Excel.!! 从Excel中进行操作会更容易。!! Open Excel Data>Import/Export Data>Import Data Next to file name click "New Source" Button On Welcome to the Data Connection Wizard, choose Microsoft SQL Server. 打开Excel数据>导入/导出数据>导入数据在文件名旁边单击“新建源”按钮单击“欢迎使用数据连接向导”,选择“Microsoft SQL Server”。 Click Next. 点击下一步。 Enter Server Name and Credentials. 输入服务器名称和凭据。 From the drop down, choose whichever database holds the table you need. 从下拉列表中,选择您需要的表格中的任何一个数据库。 Select your table then Next..... Enter a Description if you'd like and click Finish. 选择您的表然后单击下一步.....如果您愿意,请输入描述,然后单击完成。 When your done and back in Excel, just click "OK" Easy. 当您完成并返回Excel时,只需单击“确定”即可。

Create the excel data source and insert the values, 创建Excel数据源并插入值,

insert into OPENROWSET('Microsoft.Jet.OLEDB.4.0', 
'Excel 8.0;Database=D:\testing.xls;', 
'SELECT * FROM [SheetName$]') select * from SQLServerTable

More informations are available here http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=49926 有关更多信息,请访问http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=49926

A handy tool Convert SQL to Excel converts SQL table or SQL query result to Excel file without programming. 一个方便的工具将SQL转换为Excel,无需编程即可将SQL表或SQL查询结果转换为Excel文件。

Main Features - Convert/export a SQL Table to Excel file - Convert/export multiple tables (multiple query results) to multiple Excel worksheets. 主要功能 - 将SQL表转换/导出为Excel文件 - 将多个表(多个查询结果)转换/导出到多个Excel工作表。 - Allow flexible TSQL query which can have multiple SELECT statements or other complex query statements. - 允许灵活的TSQL查询,该查询可以包含多个SELECT语句或其他复杂的查询语句。

B. Regards, Alex B.问候,Alex

There exists several tools to export/import from SQL Server to Excel. 有几种工具可以从SQL Server导出/导入到Excel。

Google is your friend :-) 谷歌是你的朋友:-)

We use DbTransfer (which is one of those which can export a complete Database to an Excel file also) here: http://www.dbtransfer.de/Products/DbTransfer . 我们在这里使用DbTransfer(也是可以将完整数据库导出到Excel文件的那个之一): http ://www.dbtransfer.de/Products/DbTransfer。

We have used the openrowset feature of sql server before, but i was never happy with it, becuase it's not very easy to use and lacks of features and speed... 我们之前使用过sql server的openrowset功能,但我对它一直不满意,因为它不是很容易使用,缺乏功能和速度......

您始终可以使用ADO将结果写入记录集对象的工作表单元格

Try the 'Import and Export Data (32-bit)' tool. 尝试“导入和导出数据(32位)”工具。 Available after installing MS SQL Management Studio Express 2012. 安装MS SQL Management Studio Express 2012后可用。

With this tool it's very easy to select a database, a table or to insert your own SQL query and choose a destination (A MS Excel file for example). 使用此工具,可以非常轻松地选择数据库,表格或插入自己的SQL查询并选择目标(例如,MS Excel文件)。

you can right click on a grid of results in SQL server, and choose save as CSV. 您可以右键单击SQL Server中的结果网格,然后选择另存为CSV。 you can then you can import this into Excel. 然后,您可以将其导入Excel。

Excel gives you a import wizard, ensure you select comma delimited. Excel为您提供导入向导,确保您选择逗号分隔。 it works fine for me when i needed to import 50k+ records into excel. 当我需要将50k +记录导入excel时,它对我来说很好。

Check this. 检查一下。

Query -> Query Options.

Results -> Grid -> Include column headers when copying or saving the results

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

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