Connecting Tech Pros Worldwide Help | Site Map

Create Excel spreadsheet from Javascript

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 20th, 2005, 02:10 PM
Jim, N2VX
Guest
 
Posts: n/a
Default Create Excel spreadsheet from Javascript

I'd like to create/display an Excel spreadsheet from javascript. We
have an HTML page with results of a search and it can be reasonably
large.

The first attempt was to format the data into an HTML table and send
it to an ASP page. The ASP page has:
Response.AddHeader ("Content-Disposition", "inline");
Response.ContentType = "application/vnd.ms-excel");
Response.Write(formatted_html_data);

Works fine until there's more than 100K bytes of data. It then hits
an ASP limit and blows chunks.

Second attempt: Do it all on the client side in Javascript. A
variable has the HTML:

<html>
<head>
<meta http-equiv="Content-Type" CONTENT="application/vnd.ms-excel">
<meta http-equiv="Content-Disposition" CONTENT="inline">
</head>
<body>
<table>
....html-formatted data...
</table></body></html>

We send the data to a new window:
var newWin = window.open('','','width=300,height=300');
newWin.document.write(variable_with_html);
newWin.document.close()

The window pops up, html-formatted data displays in it, but Excel
doesn't run.

What's the difference between the first method and the second? Both
send 'stuff' to the browser that it will interpret.

Are the <meta> tags wrong? It's the only thing I can think of, since
one is generated by ASP (and it works) and the other by me (doesn't
work).

Thanks,
Jim

  #2  
Old July 20th, 2005, 02:11 PM
ExGuardianReader
Guest
 
Posts: n/a
Default Re: Create Excel spreadsheet from Javascript

Jim, N2VX wrote:
[color=blue]
> I'd like to create/display an Excel spreadsheet from javascript. We
> have an HTML page with results of a search and it can be reasonably
> large.
>
> The first attempt was to format the data into an HTML table and send
> it to an ASP page. The ASP page has:
> Response.AddHeader ("Content-Disposition", "inline");
> Response.ContentType = "application/vnd.ms-excel");
> Response.Write(formatted_html_data);
>
> Works fine until there's more than 100K bytes of data. It then hits
> an ASP limit and blows chunks.[/color]

Try NOT building up a HUGE string before you write to the Response
object. Have a function which outputs the Excel data to the Response
rather than one which creates a huge string.

HINT: You can create proper, functional Excel Spreadsheets by sending a
correctly formatted XML spreadsheet with that ContentType. See

http://msdn.microsoft.com/library/de.../odc_xmlss.asp

It's finicky about the XML formatting, but once you get it right, you
can get some really nice spreadsheets out. Persevere with this approach.

This is the way to go, you cannot generate a spreadsheet in javascript.
[color=blue]
> Second attempt: Do it all on the client side in Javascript. A
> variable has the HTML:
>
> <html>
> <head>
> <meta http-equiv="Content-Type" CONTENT="application/vnd.ms-excel">
> <meta http-equiv="Content-Disposition" CONTENT="inline">
> </head>
> <body>
> <table>
> ...html-formatted data...
> </table></body></html>
>
> We send the data to a new window:
> var newWin = window.open('','','width=300,height=300');
> newWin.document.write(variable_with_html);
> newWin.document.close()
>
> The window pops up, html-formatted data displays in it, but Excel
> doesn't run.[/color]

No, it won't suddenly fire up Excel. You are writing data into an HTML
document in the browser. That's what document.write() does. Once you are
IN a document, it makes no difference what data you write into it - it's
an HTML DOCUMENT!

The previous method used the HTTP standard (Do a google on it) to inform
the browser what kind of data was coming back from the server. You
correctly informed the browser that Excel data was coming back
(ContentType = "application/vnd.ms-excel"), so the browser offers to run
the application (or save the output).

Hope this helps.

Nige

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.