views:

148

answers:

3

There are many examples on the internet of doing this type of thing. But none of them work in Firefox. So I thought I'd set the challenge for Stack Overflow users.

Basically I need a very easy to use way for a user of a page to get the results of a query into the clipboard so that they can paste it into excel.

It can either be getting the data from a HTML table (generated by the query)

Or send the results directly to the clipboard (in a format readable by excel)

I have an intranet with many asp pages. Some of them generate quite large tables and some of these are then copied into excel. currently the user has to click inside the top left cell, then scroll down the page and move the cursor off the end of the table. If they don't do this exactly right (in Firefox) the data won't be read properly by excel.

I want them to just be able to click a button then they go to excel, click in cell a1, and ctrl-v. The data should show up properly separated into columns and rows (formatting doesn't matter)

Any ideas?

I am open to ASP or Javascript methods. And it must work in both IE and firefox (3)

EDIT

I've chosen the simplest option from the replies (I may have a lot of pages to 'do' so the simplest will be the best.

In the form...

Send results to excel <input type="checkbox" name="sendtoexcel">

In the code later on...

if request.querystring("sendtoexcel") = "on" then

response.contenttype = "application/vnd.ms-excel"
Response.AddHeader "content-disposition","attachment; filename=fname.xls"

end if
+1  A: 

The answer to this question may be helpful http://stackoverflow.com/questions/127040/put-text-on-the-clipboard-with-firefox-safari-and-chrome

Andomar
+1 although Flash was not on the OPs list of methods he's "open" to
bendewey
+2  A: 

First off, Excel has great support for copy/paste of html table objects. You should see if that fits your needs.

Otherwise, I would recommend sending a CSV file of the data down from ASP to the user directly.

I haven't tested this, but it should get you part of the way there:

<%
Response.AddHeader "Content-Type", "application/octet-stream"
Response.AddHeader "Content-Disposition", "attachment; filename=myTable.csv"

Dim customers, c
customers = GetCustomerFromDb()

For Each c in customers
  Response.Write """" & c("Name") & ""","
  Response.Write """" & c("Email") & ""","
  Response.Write """" & c("Address") & ""","
  Response.Write """" & c("City") & ""","
  Response.Write vbCrLf
Next
%>

What you'll want to do is create a new page with this code, then in your interface you'll have a link to this page which will download the CSV.

bendewey
Sorry it took so long for me to get back. I've basically added this to my page...if request.querystring("sendtoexcel") = "on" thenresponse.contenttype = "application/vnd.ms-excel"Response.AddHeader "content-disposition","attachment; filename=fname.xls"end ifand an input int he form. This is enough for now. Thanks for your help.
MrVimes
A: 

The approach I would use in an intranet setting is to have an ASP page that sends the table as HTML but sets the response content type to "application/vnd.ms-excel".

Potentially you would have three pages:

  1. an Include page that contains code to generate the HTML table starting at
  2. a view page which uses the include to create the table output enscounched in other markup such as navigation and a "send to excel link" (which is a link to the page below)
  3. a page that sets its content type to "application/vnd.ms-excel" and uses the same include to generate the table output.

Now the user just clicks the link and the table content appears in Excel.

Note for best operation you will need to add the Content-Dispostion header "filename=sensible.xls; attachment".

AnthonyWJones
I think your answer will be best for me. I haven't got it working with the query results page yet but I've done enough to know I'll eventually get it working. But running out of time so I'll comment later on my progress.
MrVimes