Create an Excel file with Javascript?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Martin

    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.
  • David Given

    #2
    Re: Create an Excel file with Javascript?

    Martin wrote:
    [...][color=blue]
    > 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?[/color]

    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

    Comment

    • Martin Honnen

      #3
      Re: Create an Excel file with Javascript?



      Martin wrote:

      [color=blue]
      > 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?[/color]

      Not really, a web page in general even if there are ways exposed to
      script such as
      new ActiveXObject(' Excel.Applicati on')
      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.FileS ystemObject (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

      Comment

      • McKirahan

        #4
        Re: Create an Excel file with Javascript?

        "Martin" <martinvalley@c omcast.net> wrote in message
        news:0342v0hevp mmmnbtk6eft11mb 61o66kivc@4ax.c om...[color=blue]
        > 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.[/color]

        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).


        Comment

        • Robert

          #5
          Re: Create an Excel file with Javascript?

          In article <iu8Id.479$Lo5. 216@newsfe1-win.ntli.net>,
          David Given <dg@cowlark.com > wrote:
          [color=blue]
          > Martin wrote:[/color]

          [color=blue]
          > 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.[/color]

          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

          Comment

          • Martin

            #6
            Re: Create an Excel file with Javascript?

            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:
            [color=blue]
            >Martin wrote:
            >[...][color=green]
            >> 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?[/color]
            >
            >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.[/color]

            Comment

            • Grant Wagner

              #7
              Re: Create an Excel file with Javascript?

              "Martin" <martinvalley@c omcast.net> wrote in message
              news:fnoav0lagp 2eo2ec0nq43hrhe rkffo6pq1@4ax.c om...[color=blue]
              >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 ?[/color]

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

              Content-Disposition: attachment; filename="yourf ile.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 <gwagner@agrico reunited.com>
              comp.lang.javas cript FAQ - http://jibbering.com/faq


              Comment

              • Martin

                #8
                Re: Create an Excel file with Javascript?

                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"
                <gwagner@agrico reunited.com> wrote:
                [color=blue]
                >"Martin" <martinvalley@c omcast.net> wrote in message
                >news:fnoav0lag p2eo2ec0nq43hrh erkffo6pq1@4ax. com...[color=green]
                >>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 ?[/color]
                >
                >Yes. Before you send the XLS file from the server, send the following
                >headers:
                >
                >Content-Disposition: attachment; filename="yourf ile.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.[/color]

                Comment

                Working...