469,343 Members | 5,478 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,343 developers. It's quick & easy.

Create an Excel file with Javascript?

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.
Jul 23 '05 #1
7 18516
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.

--
+- David Given --McQ-+ "Feminism encourages women to leave their
| dg@cowlark.com | husbands, kill their children, practice withcraft,
| (dg@tao-group.com) | destroy capitalism and become lesbians." --- Rev.
+- www.cowlark.com --+ Pat Robertson

Jul 23 '05 #2


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.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #3
"Martin" <ma**********@comcast.net> wrote in message
news:03********************************@4ax.com...
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).
Jul 23 '05 #4
In article <iu***************@newsfe1-win.ntli.net>,
David Given <dg@cowlark.com> wrote:
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
Jul 23 '05 #5
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
On Fri, 21 Jan 2005 14:39:10 GMT, David Given <dg@cowlark.com> wrote:
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.


Jul 23 '05 #6
"Martin" <ma**********@comcast.net> wrote in message
news:fn********************************@4ax.com...
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.

--
Grant Wagner <gw*****@agricoreunited.com>
comp.lang.javascript FAQ - http://jibbering.com/faq
Jul 23 '05 #7
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

On Mon, 24 Jan 2005 21:57:32 GMT, "Grant Wagner"
<gw*****@agricoreunited.com> wrote:
"Martin" <ma**********@comcast.net> wrote in message
news:fn********************************@4ax.com.. .
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.


Jul 23 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by =?Utf-8?B?Tml5YXpp?= | last post: by
4 posts views Thread by Rahul | last post: by
reply views Thread by Paul | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.