Want to execute sp_send_dbmail and send results in excel format

Viewed 6360

I want to execute sp_send_dbmail and have results emailed in excel format. I have tried .csv and .txt but the results are not organized in neat columns.

This is my code

EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'DBMail_profile',
@recipients = 'myaddress@me.com',
@subject = 'Test',
@query = N'EXEC DatabaseTest.dbo.test',
@attach_query_result_as_file = 1,
@query_result_width = 150,
@query_result_header= 1,
@query_attachment_filename = 'test.txt',
@importance = 'High',
@query_result_no_padding = 1

Also have tried this

EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'DBMail_profile',
@recipients = 'myaddress@me.com',
@subject = 'Test',
@query = N'EXEC DatabaseTest.dbo.test',
@attach_query_result_as_file = 1,
@query_result_width = 150,
@query_result_header= 1,
@query_attachment_filename = 'test.csv',
@importance = 'High',
@query_result_no_padding = 1

I would like my results to be in a clean organized format. Do I have some of the parameters set incorrectly? Or is there a way to have attachment as .xls or .xlsx file? I may be explaining incorrectly, I can answer any questions

Here is what I get back from .csv

EventDate FirstName LastName Department DeptCode OpCode Start Finish ElapsedTime Units UPH

2017-05-30 My Name Flat LL PP 07:00:00 07:15:00 00:15:00 700 2800
2017-05-31 His Name Maintenance FK PR 08:00:00 08:30:00 00:30:00 100 200
2017-05-30 Her Name Hang SD OC 12:32:00 14:31:00 01:59:00 23 12

When I have @query_attachment_file = 'test.xlsx' the attachment is identical to what the .csv provides.

I want to see attachment as such

2017-05-30  My   Name   Flat        LL  PP  07:00:00    07:15:00    00:15:00    700 2800
2017-05-31  His  Name   Maintenance FK  PR  08:00:00    08:30:00    00:30:00    100 200
2017-05-30  Her  Name   Hang        SD  OC  12:32:00    14:31:00    01:59:00    23  12

Stored Procedure .dbo.test looks like this

DECLARE 
@Now DATETIME,
@EndReportDate DATETIME,
@StartReportDate DATETIME,
@StartTime VARCHAR(16) ,
@EndTime VARCHAR(16)

SET @Now = GETDATE()
SET @EndReportDate = DATEADD(dd, DATEDIFF(dd, 0, @Now), -1)
SET @StartReportDate = DATEADD(dd, DATEDIFF(dd, 0, @Now), -9)
SET @StartTime = '01:00:00AM'
SET @EndTime = '11:59:00PM' 

SELECT 
TimeLog.EventDate AS 'EventDate'
 ,AssociateInfo.FirstName 'FirstName'
 ,AssociateInfo.LastName 'LastName'
 ,AssociateInfo.Department 'Department'
 ,TimeLog.DeptCode 'DeptCode'
 ,TimeLog.OpCode 'OpCode'
 ,TimeLog.StartTime 'Start'
 ,TimeLog.FinishTime 'Finish'
 ,ElapsedTime = convert(time(0),dateadd(second,datediff(second,StartTime,FinishTime),0)) 
,TimeLog.Units
,UPH = cast(isnull(Units / nullif(datediff(minute,StartTime,FinishTime)*1.0,0),0.0) *60 as decimal(10,0))
FROM dbo.TimeLog INNER JOIN dbo.AssociateInfo WITH (NOLOCK)
ON AssociateInfo.ID = TimeLog.ID

WHERE EventDate BETWEEN @StartReportDate + ' ' + @StartTime
                   AND     @EndReportDate + ' ' + @EndTime

ORDER BY  UPH DESC  
1 Answers
Related