Create an Excel file with Javascript?

Discussion in 'Javascript' started by Martin, Jan 21, 2005.

  1. Martin

    Martin Guest

    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.
    Martin, Jan 21, 2005
    #1
    1. Advertising

  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/
    Martin Honnen, Jan 21, 2005
    #2
    1. Advertising

  3. Martin

    McKirahan Guest

    "Martin" <> wrote in message
    news:...
    > 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).
    McKirahan, Jan 21, 2005
    #3
  4. Martin

    Robert Guest

    In article <iu8Id.479$>,
    David Given <> 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
    Robert, Jan 21, 2005
    #4
  5. Martin

    Martin Guest

    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 <> 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.
    Martin, Jan 24, 2005
    #5
  6. Martin

    Grant Wagner Guest

    "Martin" <> wrote in message
    news:...
    >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 <>
    comp.lang.javascript FAQ - http://jibbering.com/faq
    Grant Wagner, Jan 24, 2005
    #6
  7. Martin

    Martin Guest

    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"
    <> wrote:

    >"Martin" <> wrote in message
    >news:...
    >>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.
    Martin, Jan 24, 2005
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Luis Esteban Valencia
    Replies:
    1
    Views:
    1,371
    Carl Prothman [MVP]
    Jan 12, 2005
  2. Replies:
    1
    Views:
    1,780
    extentechjohn
    Nov 21, 2006
  3. Jona
    Replies:
    2
    Views:
    443
    Victor Bazarov
    Jul 6, 2007
  4. =?Utf-8?B?c2hhc2hhbmsga3Vsa2Fybmk=?=

    Problem with Excel reports ::::Excel 2003 Migration To Excel 2007

    =?Utf-8?B?c2hhc2hhbmsga3Vsa2Fybmk=?=, Oct 5, 2007, in forum: ASP .Net
    Replies:
    15
    Views:
    1,486
    =?Utf-8?B?c2hhc2hhbmsga3Vsa2Fybmk=?=
    Oct 24, 2007
  5. Jim, N2VX

    Create Excel spreadsheet from Javascript

    Jim, N2VX, Jan 30, 2004, in forum: Javascript
    Replies:
    1
    Views:
    1,071
    ExGuardianReader
    Jan 31, 2004
Loading...

Share This Page