>
Blog
Book
Portfolio
Search

2/28/2008

6584 Views // 0 Comments // Not Rated

Opening An ASP.NET 2.0 Web Form In Excel Client, Not Excel Browser

Exporting to Excel seems like a feature that everyone wants to see. I feel as though if I'm not mimicking or automating Excel in some way, I'm dumping data directly into it. Anyways, you can Google dozens of ways to get an ASP.NET 2.0 web application to export to Excel.

Weather you are generating an XLS on the fly and merely redirecting to it, creating Excel-friendly HTML on your page, or even (and Microsoft says this is a major no-no) coding against the Excel object model on the server, your users will inevitably want to see rows and columns when the browser stops spinning.

And like I said, there are several well-documented ways to do this. These can be divided into two different methods:

  • Redirect to an Excel file on the server.  This (assuming the client has Excel installed) will return a browser window that has an Excel-ish feel to it.  This will probably only give about 5% of what the full-blown Excel has, but certainly enough to do some cursory data slicing-and-dicing and saving the file locally.  The pro of this method is that it's easy.
  • Force a download of an Excel file.  After setting the correct content-type and whatnot, this will popup a save file dialog with the ever-present options to Open or Save the file.  Clicking Save saves the file; clicking Open, however, at least on IE 6, will again open the file in the Excel flavor of the browser, not Excel itself.  With this method, you can get away without cluttering the server with file.

In order to force that Open button to actually bring up Excel, just insert a little bit of love into the page's markup. Here's what I did:

Code Listing 1

  1. /><% this.Response.ContentType = "application/vnd.ms-excel"; %>
  2. /><% this.Response.AppendHeader("content-disposition", "attachment; filename=some file name.xls - doesn't matter"); %>

Normailly, you'll see this code in a souce file instead of the markup ala Classic ASP (which, barf, I'm not a fan of). However, by placing it here, we are focing the page itself to ooze into Excel; the page is poised to always be in "Excel client world." When the same code is behind an event handler, it will cause the Response object to pipe the HTML into an Excel browser since we are still in "web world."

And there you have it! I've never seen happier users...

No Tags

No Files

No Thoughts

Your Thoughts?

You need to login with Twitter to share a Thought on this post.


Loading...