Create an Excel file with Javascript?

M

Martin

I have a situation where I'm displaying some information in a table on
a web page. I've given the user the ability to make several different
"queries" and show different sub-sets of the data.

I would like to add a button to the page that would allow the user to
create an .XLS file that would contain the current contents of the
table. I realize that I could create it on the server and allow him to
download it but I'd rather let him create it right there on his
machine. Is it possible to have a script that can do such a thing?

Obviously, I do not want to rely on there being any specific
Excel-related software installed on the user's machine.
 
M

Martin Honnen

Martin wrote:

I would like to add a button to the page that would allow the user to
create an .XLS file that would contain the current contents of the
table. I realize that I could create it on the server and allow him to
download it but I'd rather let him create it right there on his
machine. Is it possible to have a script that can do such a thing?

Not really, a web page in general even if there are ways exposed to
script such as
new ActiveXObject('Excel.Application')
in IE/Win doesn't have the rights to do such things.
You might be able to used signed scripts with Mozilla/Netscape to
request privileges for file access but there is certainly no support
then to create an Excel file from the HTML DOM.
With IE/Win if someone is willing to consider your web site as trusted
then file access might be possible with Scripting.FileSystemObject (but
that is geared towards text files) and if Excel is around then you could
instantiate it.
But in general if you know how to create Excel on the server and have
the possibilities on the server of your site then do it on the server.

Or I guess if the browser user simply copies the table and pastes into
Excel it might work out.
 
M

McKirahan

Martin said:
I have a situation where I'm displaying some information in a table on
a web page. I've given the user the ability to make several different
"queries" and show different sub-sets of the data.

I would like to add a button to the page that would allow the user to
create an .XLS file that would contain the current contents of the
table. I realize that I could create it on the server and allow him to
download it but I'd rather let him create it right there on his
machine. Is it possible to have a script that can do such a thing?

Obviously, I do not want to rely on there being any specific
Excel-related software installed on the user's machine.

You might offer the user an option to generate a CSV file which is displayed
as a Web page with instructions to save it with a CSV extension then to
double-click on it to open it in MS-Excel (if present) then to save that
with an XLS extension. Of course you could use FSO to do (most of) the
above (with the attendant security warnings).
 
R

Robert

David Given said:
Martin wrote:

Possibly your best bet is to send the information to a CGI script somewhere
that creates the Excel file and makes it available for download; it
wouldn't be particularly difficult, and would move the nasty
creating-the-binary bit out of the Javascript domain.

Expanding on this idea. You could have a form when submitted would
upload the table to the server. The server could generate the excel
file and send the excel file as the response to the client. On windows
IE with excel, the page would open in Excel. Otherwise, it would most
likely be saved on disk.

You have a fast link and not that much data, the user will hardly notice.

(( Oh, I guess you wanted to do it on the client. ))

Robert
 
M

Martin

I have this working now - when the user clicks the provided link, I
generate the .XLS file on the server and send it out. The problem I
have now is, depending on the client's setup, the .XLS file is opening
up and displaying in the user's browser. Most of the time, this is not
what the user will want to do - he's downloading to save it for future
reference.

On one of my test machines, a dialog pops up asking if I want to open
the file or save it. Can someone tell me if there is a way for me to
force this dialog to pop-up ?

Thanks


Martin wrote:
[...]
I would like to add a button to the page that would allow the user to
create an .XLS file that would contain the current contents of the
table. I realize that I could create it on the server and allow him to
download it but I'd rather let him create it right there on his
machine. Is it possible to have a script that can do such a thing?

Javascript in web pages can't access the file system, so I'd say probably
not.

There are a number of evil platform-specific ways of getting around this,
but they're all evil (LiveConnect, aaah!) or platform-specific (ActiveX,
aaaah!).

Possibly your best bet is to send the information to a CGI script somewhere
that creates the Excel file and makes it available for download; it
wouldn't be particularly difficult, and would move the nasty
creating-the-binary bit out of the Javascript domain.
 
G

Grant Wagner

Martin said:
I have this working now - when the user clicks the provided link, I
generate the .XLS file on the server and send it out. The problem I
have now is, depending on the client's setup, the .XLS file is opening
up and displaying in the user's browser. Most of the time, this is not
what the user will want to do - he's downloading to save it for future
reference.

On one of my test machines, a dialog pops up asking if I want to open
the file or save it. Can someone tell me if there is a way for me to
force this dialog to pop-up ?

Yes. Before you send the XLS file from the server, send the following
headers:

Content-Disposition: attachment; filename="yourfile.xls"
Content-type: application/vnd.ms-excel

Most well-behaved browsers (including IE) will prompt the user to save
the file, even if Microsoft Excel is installed on the client system.
 
M

Martin

Grant -

Thanks. That does exactly what I want. And, I would never have figured
it out on my own.

BTW, the client didn't recognize "vnd.ms-excel" but when I changed it
to "xls", it did.

Thanks again.

Martin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top