YAPS on Custom-Excel-for-RS: Drive Subscriptions with a Stick Shift

by Lisa Nicholls Sun, February 07 2010 12:02

 

The title gives away the punchline on this one...

Cliff Leighton asks

Also, does anyone know how to make the output filename end with .XLS in an emailed SSRS Subscription with the "XML file with report data" option? The emailed file from the Subscription comes out as *filename*.xml and my users will be very confused with an XML file versus a XLS file.

and at least one other person (Sanket) appeared to have a similar problem last November:

Hi,I want to change name of report file when exporting to excel.
i have got no idea how to do it. Frown
 Please help Smile

... which certainly makes it a question worth answering. I asked Sanket this:

Sanket: are you exporting using an application?  Or is the user doing the export from ReportViewer or Report Manager? Or what?

... and you'll see why shortly.

Before I answer, the inevitable caveat

Folks, while an XML extension may be confusing, consider the following:

  • If the user's computer has ever had Excel 2003 installed on it, chances are that a double-click will open the file in Excel by default, and the XML file will show an appropriate/specialized icon to show that it is an "Excel XML file", not a standard XML icon.  Not terribly confusing.  How does this work?  It's all down to the processing instruction that we added to the SSML:
    <?mso-application progid="Excel.Sheet"?>

    This screen shot shows you what it looks like when it works as expected (remember, this will be different depending on the client computer; for example, the Standard XML file icon you see here belongs to Stylus Studio, because that's my default owner for this type of file on this computer):

  • If the user has Excel 2007 installed as the default owner for files with an XLS extension, and the file is really SSML rather than Excel binary, the result they will when they doubleclick is a warning like the following:



    Naturally, you can tell your users to ignore this warning and answer "Yes" to the dialog.  But it's not exactly what I would call un-confusing.

So, IMHO... you pays your money and you takes your choice.  With that understood, let's address the stated requirement.

The short answer is "no", but we don't care.

Try to remember that the default subscription interface you see in Report Manager is just a basic UI given to you by default. 

Just like the parameters panel for ReportViewer, which we all love to hate, there's nothing intrinsically required about it.

Just like the Excel renderer can be obviated by creating our own SSML from the XML output, the base Excel rendition isn't the one we have to accept.

A subscription is just a timed task or job. If you look at SQL Agent, it looks something like this: 

The one step in the job looks something like this: 

exec ReportServer.dbo.AddEvent
  @EventType='TimedSubscription',
  @EventData='512f89b1-7dd8-4b2f-806b-29927da7a81e'

... and the code underneath is probably not very complicated either, although I've never looked.  (That would be like trying to fix the default Excel renderer.)

Let's leave it alone, and think about the right way to fix our problem here, which (from Cliff's point of view) is that you are allowed to set the full name of the output file when it goes to a file share, but you're not allowed to set the extension when you send the subscription to an email address.

We won't need any grenades

This is actually an easy problem to solve, several different ways (remember the paying the money and taking the choice part?)

Here's a overview of how to do this thing.

Start with the knowledge that this is just a timed task, and write your own! It will be very simple, and SSIS is probably the most natural approach (but if you don't like it you don't have to use it, as you'll easily see).

  1. Use a fileshare subscription, or...

    1. If you're using SSIS, you can invoke the subscription yourself. Or use a file watcher task (these are available from better SSIS jockeys than me, so check it out) and stick with the standard subscription schedule.
      Method notes: You don't need to use the code you see above, you can just use exec sp_start_job.  (That's one reference. You'll find others, I'm sure. It turns out this is pretty much an FAQ.)
    2. Pick up the file and rename it.
    3. Email it out. 
      1. If your subscription was data driven, you should be able to look at the same data as RS does, to figure out to whom you email. 
      2. Emailing is simple, whether you use a purpose-built SSIS task or your own code.
  2. ... Skip the subscription altogether. Create a timed task or SSIS job that does what you need, without one:

    1. Retrieve the report yourself, with minimal code. 
      1. You can use either SOAP or REST to do this. 
        Method notes: I've shown several examples of how to do this on this blog already, but I'll add another example here, which shows a Q&D REST approach to doing this inside SSIS.  It comes from my xmlRSDocs processing code, but you should be able to see how to apply it. 
        I'm posting this bit because I think I remember having to help somebody with the correct classes to use for a low-level post approach like this, when working inside SSIS, once.
      2. Here's an alternative -- you'll find many -- for invoking RS inside SSIS, and this one uses SOAP.  I think this is a bit overcomplicated and I think you can apply some of my other instructions for SOAP invocations more easily, including one SOAP version done quite recently. But maybe this one is more "orthodox", so I don't want to push my methods on you..
    2. Obviously when you're picking up the file yourself, name it anything you want.
    3. As above, email it out.

That's about it.  Here's the REST example I promised; this is the code from an SSIS script task.

 
Public Sub Main()  
   ' a script task to grab an SSRS report
   ' from within SSIS, based on some
   ' specialized package variables that make
   ' sense in my scenario.  Your scenario is
   ' different, but the basic idea will work for you.

 
   Dim outputFile As String, requestURL As String
 
   If Dts.Variables("ExportRSType").Value.ToString().Length = 0 Then
      Dts.Variables( "ExportRSType").Value = "PDF"
   End If

   outputFile = Dts.Variables( "ExportRSFilename" ).Value.ToString()  
 
   If System.IO.Path.GetDirectoryName(outputFile) = "" Then
      outputFile = Dts.Variables("InputDir" ).Value.ToString() & _ 
         outputFile 
   End If

   outputFile = System.IO.Path.GetFullPath(outputFile)
   requestURL = _
      Dts.Variables("ExportRSURL" ).Value.ToString() 
   requestURL += _
      "&rs:Command=Render"
   requestURL += _
      "&rs:Format=" & Dts.Variables( "ExportRSType" ).Value.ToString()  

   ' this part is a little kludge because of 
   ' how I set up my URL template variable 
   in the SSIS Package... not really germane:

    If Len(Dts.Variables( "ExportRSParams").Value.ToString()) > 0 Then
      requestURL += _
         "&" & Dts.Variables( "ExportRSParams").Value.ToString().Replace( "^", "&" )
   End If

   Dim ox As System.Net.WebRequest = _ 
      System.Net.WebRequest.Create(requestURL)
   ox.UseDefaultCredentials =  True 'TBD...
 
   Dim oy As System.Net.WebResponse = ox.GetResponse()  
   Dim raw As System.IO.FileStream = _  
   New System.IO.FileStream(outputFile, IO.FileMode.Create)  
   Dim buffer(1024) As Byte
 
   Dim rs As System.IO.Stream = oy.GetResponseStream()  
   Dim read As Integer = rs.Read(buffer, 0, buffer.Length)  
   
   While (read > 0)
      raw.Write(buffer, 0, read)
      read = rs.Read(buffer, 0, buffer.Length)
   End While

   oy.Close()
   raw.Close()
   raw.Dispose()
   rs.Close()
   rs.Dispose()
   oy = Nothing
   ox = Nothing
   raw = Nothing
   rs = Nothing
   Dts.TaskResult = ScriptResults.Success
 
End Sub

... OK?

None of this stuff is magic.

Tags:

Reporting | SQL Server | XML/XSLT | YAPS

Comments (34) -

2/8/2010 10:43:19 PM #

mikrotik

thanks for sharing Lissa Smile

mikrotik Indonesia

2/8/2010 10:44:38 PM #

vietnamet

great post, interest to try

vietnamet Indonesia

2/8/2010 10:46:21 PM #

vietnamet

good description, thanks for your informations

vietnamet Indonesia

2/12/2010 3:22:02 AM #

Omar

This is very useful.

Omar United States

2/15/2010 7:26:51 AM #

Braun

thanks for sharing Lissa

Braun United States

3/3/2010 1:02:06 AM #

how to grow taller

thanks!  very helpful post!! like the template btw ;)

how to grow taller Australia

6/22/2010 3:06:40 AM #

site de casino sans telechargement


I am sure the same concept can be used in a varity of way to get lot of real time information.The person who create this post he is a great human..thanks for shared this with us.

site de casino sans telechargement India

7/7/2010 7:15:03 AM #

mike white

Great info, thanks.

Kind regards,
Mike White

mike white United States

7/23/2010 10:36:47 PM #

Miami Phone Systems

You can just rename the file to a XLS file

Miami Phone Systems United States

7/27/2010 8:07:43 AM #

Roulette

thank you very helpful

Roulette Dominican Republic

8/1/2010 12:52:22 PM #

tadalis

LSN, both surpassing the XML file with a MySQL database?

tadalis Russia

8/2/2010 12:24:19 AM #

http://darkred.org

Thanks a lot for the tutorial, just came across it - my lucky day Smile

http://darkred.org United States

8/3/2010 1:31:02 PM #

Masquerade Masks

Excellent use of Excel!

Masquerade Masks United States

8/4/2010 6:54:37 AM #

common file extensions

I hear about this kind of problems all the time, people are downloading various files and then they discover that those files won't work on their computers since their system does not support them. This is not about XML files or XLS only, there are so many new file extensions these days and they can easily get you confused if you have never used them before.

common file extensions United States

8/4/2010 2:14:34 PM #

Heating pads

Excel is a very useful program but gives me a headache a lot of the time

Heating pads United States

8/5/2010 11:51:23 PM #

Stump removal montgomery county

Thank you very much for all the information about SSLM files and Excel files it helped me a lot. Thanks a ton.

Stump removal montgomery county United States

8/9/2010 9:35:13 PM #

Hamster Cage

Thanks a lot for this excel file help

Hamster Cage United States

8/9/2010 11:43:29 PM #

Shower radio

Excel can be a tricky program, but I use it daily in my work and it is truly a life saver for organizing data

Shower radio United States

8/10/2010 1:20:44 PM #

Meat slicer

I have had some trouble with excel lately, that definitely cleared it up

Meat slicer United States

8/10/2010 2:40:11 PM #

Canister Vacuums

HAHA too funny how you said the stick shift Tong

Canister Vacuums United States

8/12/2010 12:46:11 AM #

cheap rooms

I really enjoyed while reading the post and like to appreciate the author. You are bookmarked. Thanks you for sharing the post with all.

cheap rooms United States

8/12/2010 12:48:39 AM #

Calgary network services

I want to say thank you. All your articles are great. I am just your fan. You are bookmarked.

Calgary network services United States

8/26/2010 5:56:18 AM #

outlook pst repair

Thanks for the post, it was nice. I read your other post as well, all were good. I really appreciate the author. You are bookmarked.

outlook pst repair United States

9/6/2010 4:02:41 AM #

Datel

Thanks, this i must try!

Datel Sweden

9/7/2010 7:51:51 PM #

Harvard Boys Forex

Great share.

Harvard Boys Forex United States

9/9/2010 6:47:22 PM #

ufficio affitto

I truly love your article. Excellent job!

ufficio affitto United States

9/13/2010 2:25:58 AM #

netload search

Great Post

netload search United States

10/14/2010 8:07:42 AM #

electric meat slicer

I have got an xml reader especially to handle XML files.

electric meat slicer United States

10/28/2010 2:11:15 AM #

hair extensions kent

I love this post! I dont have a specialist XML reader.... maybe I should get one??

hair extensions kent United Kingdom

10/31/2010 1:30:23 AM #

healthy eating Rotorua

Excellent post. people can so many things form the post. I bookmark the site because I get something very acknowledging. Nice post!!!

healthy eating Rotorua New Zealand

11/7/2010 10:02:21 AM #

kids parties brisbane

Nice post.I like the way you start and then conclude your thoughts. Thanks for this information .I really appreciate your work, keep it up

kids parties brisbane United States

11/27/2010 4:53:09 AM #

Money Saver

Great Article. XML extension is not that every person knows a lot about. And thanks for the SSIS script code.

Money Saver Sweden

5/9/2011 9:10:49 PM #

casino games

Needing this article for not less than two days now . Just about the explanation I was initially looking forward to however your wider belief on the content was offered.

casino games United States

10/23/2011 11:50:52 AM #

Pierre

Hello

interresting, post here may come back soon
Keep like this

Pierre

Add comment




  Country flag
biuquote
  • Comment
  • Preview
Loading