Using sp_send_dbmail to send the query results in the excel sheet


  • I am using below code to send the query results in the excel sheet using sp_send_dbmail but the excel format is not good.How to fix the excel format.

    EXEC msdb.dbo.sp_send_dbmail
       @recipients     = '', 
       @subject        = 'Reports',
       @query = ' SELECT ID,Indicator,EffecDate,ProvNumber,ProvName,ErrorDescription FROM IntegrationDb.dbo.[Report]',
       @attach_query_result_as_file = 1,
       @query_attachment_filename= 'Report.xlsx'

    Friday, June 28, 2013 6:10 PM


  • Hi Deepu982,

    As far as I know, we need to generate the Excel manually and then send it as a mail attachment, for example, we can use SSIS to generate the Excel file. Additionally, we can send the result as “.csv” file with the codes on the following link. 

    How To: Use msdb.dbo.sp_send_dbmail to Email CSV Files

    Allen Li
    TechNet Community Support

    Sunday, June 30, 2013 8:34 AM

All replies