Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Create an Excel file with Javascript?

Reply
Thread Tools

Create an Excel file with Javascript?

 
 
Martin
Guest
Posts: n/a
 
      01-21-2005
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.
 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      01-21-2005


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/
 
Reply With Quote
 
 
 
 
McKirahan
Guest
Posts: n/a
 
      01-21-2005
"Martin" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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).


 
Reply With Quote
 
Robert
Guest
Posts: n/a
 
      01-21-2005
In article <iu8Id.479$(E-Mail Removed)>,
David Given <(E-Mail Removed)> 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
 
Reply With Quote
 
Martin
Guest
Posts: n/a
 
      01-24-2005
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 <(E-Mail Removed)> 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.


 
Reply With Quote
 
Grant Wagner
Guest
Posts: n/a
 
      01-24-2005
"Martin" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>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 <(E-Mail Removed)>
comp.lang.javascript FAQ - http://jibbering.com/faq


 
Reply With Quote
 
Martin
Guest
Posts: n/a
 
      01-24-2005
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"
<(E-Mail Removed)> wrote:

>"Martin" <(E-Mail Removed)> wrote in message
>news:(E-Mail Removed).. .
>>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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to return an excel file or excel data from ASP.NET Anonieko ASP .Net 2 02-12-2008 07:03 PM
Problem with Excel reports ::::Excel 2003 Migration To Excel 2007 =?Utf-8?B?c2hhc2hhbmsga3Vsa2Fybmk=?= ASP .Net 15 10-24-2007 01:34 PM
excel.h - Automating Excel... How to Create new worksheets Jona C++ 2 07-06-2007 03:35 PM
ExtenXLS loads data into Excel 2002 file but damages the Excel file. kp2900@gmail.com Java 1 11-21-2006 05:48 PM
exporting an excel file from database; making changes to excel file and updating the database by importing it back Luis Esteban Valencia ASP .Net 1 01-12-2005 12:28 AM



Advertisments