简体   繁体   English

如何使用SQL Server导出Excel文件(.xlsx)格式

[英]How to Export an Excel file (.xlsx) format using SQL Server

Actually i looking to export an excel(.xlsx) file format. 实际上,我希望导出excel(.xlsx)文件格式。 Below is my query which is used to send .csv file format using sp_send_dbmail 以下是我的查询,该查询用于使用sp_send_dbmail发送.csv文件格式

 EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'eMail Notification',
@recipients = 'abc@gmail.com',  
@query = 'use [dbname] select im.FullName as EmployeeName, im.MemberIC as NRIC 
         from tblIndividualMst im           
         where im.RecStatus = ''A''
         and (im.DateOfResign >= getdate() or im.DateOfResign is null)' ,
@subject = 'Test',
@attach_query_result_as_file = 1 ,
@query_attachment_filename = 'test.csv',
@query_result_separator=',',@query_result_width =32767,
@query_result_no_padding=1;

So while i tried to modifiy the file format from "test.csv" to "test.xlsx", all the data's messing up in test.xlsx. 因此,当我尝试将文件格式从“ test.csv”修改为“ test.xlsx”时,所有数据都在test.xlsx中混乱了。 So how can i modify my query to get correct .xlsx file format. 因此,如何修改查询以获取正确的.xlsx文件格式。

Any help appreciated. 任何帮助表示赞赏。 Thanks!!! 谢谢!!!

I have once tried it earlier. 我曾经尝试过。 You can use this, though this might not work with sp_send_dbmail : 您可以使用它,尽管这可能不适用于sp_send_dbmail:

insert into OPENROWSET(
   'Microsoft.Jet.OLEDB.4.0', 
   'Excel 8.0;Database=d:\testfile.xls;;HDR=YES', 
   'SELECT * FROM [Sheet1$]')
select * from table1

Alternatively, I will suggest to use SSIS which is actually built to do ETL related task. 或者,我建议使用实际上是为执行ETL相关任务而构建的SSIS。 You can write from any source to excel destination and use send mail task for sending it. 您可以从任何来源写入excel目标,并使用发送邮件任务进行发送。 It is configurable to be used with SQL Server Agent as well. 它也可以配置为与SQL Server代理一起使用。

SSIS details : SSIS详细信息:

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

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