How can I send table content or sql query results to clipboard from asp page?

There are many examples of this kind of thing on the internet. But none of them work in Firefox. So I figured I posed a challenge for Stack Overflow users.

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

It can either get data from HTML table (generated by query)

Or send the results directly to the clipboard (in a format that excel reads)

I have an intranet with many asp pages. Some of them generate quite large tables and some of them are then copied to excel. currently the user has to click in the top left cell, then scroll down the page and move the cursor off the end of the table. If they don't do it exactly (in Firefox), the data will not be considered correct.

I want them to just be able to click the button, then they go to excel, click in cell a1 and ctrl-v. Data should be displayed correctly separated into columns and rows (formatting does not matter)

Any ideas?

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

EDIT

I chose the simplest option from the answers (I may have many pages to "do", so the simplest will be the best.

In the shape of...

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

      

In the code later ...

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

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

end if

      

+1


a source to share


2 answers


First, Excel has a lot of support for copy / paste of html table objects. You need to make sure this suits your needs.

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

I haven't tested this, but it should help you with this:



<%
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 need to do is create a new page with this code, and then in your interface you will have a link to this page that will upload the CSV.

+2


a source


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

You will potentially have three pages:

  • a Include a page that contains the code to create the HTML table starting with
  • a view page that uses include to generate the output of the table, written in other markup such as navigation, and "send excel link" (which is the link to the page below).
  • 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 simply clicks on the link and the table contents appear in Excel.

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

0


a source







All Articles