Solved

javascript create and open csv file on the fly

Posted on 2001-07-25
3
8,750 Views
Last Modified: 2007-12-19
Hi, I am trying to use below code to create a csv
on the fly and open it use Excel.
In netscape, it actually pop up a window asking whether
use Excel to open . Then the browser freezed.
In IE, it does not do anything.  Any help. Thanks.

<script>
    function test(){
    var myWindow=window.open("","new","")
    myWindow.document.open("application/excel")
    myWindow.document.write("1,2,3")
    myWindow.close()
   }
</script>

<script>
 test();
</script>
0
Comment
Question by:xiaoxiangz
3 Comments
 
LVL 14

Accepted Solution

by:
avner earned 50 total points
ID: 6319703
As far as I know you cannot really don something like that.

To actually write Excel file on the file you need to use excel methods and not the DOM interface.
0
 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 7910420
This question has been abandoned. I will make a recommendation to the
moderators on its resolution in a week or two. I appreciate any comments
that would help me to make a recommendation.
<note>
   In the absence of responses, I may recommend DELETE unless it is clear
   to me that it has value as a PAQ.  Silence = you don't care
</note>

Cd&
0
 

Expert Comment

by:LaurenceHolbrook
ID: 9255431
For client side files, avner is correct as far as he/she went -

You could start excel, populate and use  excel's file saveas to create a CSV file -

Or you could use the File System Object to write a file - I posted an example of writing a CSV file using client side javascripting and the fso at

http://www.experts-exchange.com/Web/Web_Languages/JavaScript/Q_20459721.html

There is no MIME type used, needed or involved on the client side - the MIME type tells the browser what kind of file it is receiving from a server - when you're creating a file using client side scripting, the browser already received the file from the server -

For server side, both of those procedures would work as well, with some additional considerations -

If Excel contains macros, button customizations and/or other sordid items, then it must be sent as a binary file - Microsoft Knowledge Base Article - 193998 has the details of how to send an "application/x-msexcel" to a browser -  while not a CSV file, it would 'force' the browser to open the file with Excel - it basically involves opening Excel, populating it and saving it as an XLS file - then streaming that data in that file byte by byte to a browser -

If Excel file contains only data,  then you can send the data in an HTML table to a browser as an "application/vnd.ms-excel" file - Microsoft Knowledge Base Article - 199841 has the details - this is not a CSV file either - it is an HTML file, but the browser would be 'forced' to use Excel to open the file with - it involves creating a server side HTML file which is then sent to the browser as an Active Server Page -

I'm not sure, but if Excel isn't available, then some kind of 'application can't be found message will probably be generated leading to the 'Open with' dialog box the 'Open with' dialog box might as well be cancelled as there is no alternative to using Excel to view XLS files - and in the unlikely event a user had reassigned the file association for XLS, a foreign application is probably going to crash trying to open an Excel file -

For a server side CSV, you could use Excel as above and saveas a CSV file - MIME type "text/csv" - and send the CSV file to the browser, which will probably open it with Excel -

NB: If it's a 'windows' computer which doesn't have Excel installed on it -
or if it's a 'non-windows' computer -
or if the user has adjusted the file associations for CSV files,
then the browser 'may' use some other application to open the file with - in as much as a CSV file is pure ASCII, there are a lot of applications that will successfully open a CSV file - although the display may be a bit ugly -

You could also use the fso instead of Excel to create the CSV file with on the server and send it to the browser as a "text/csv" file - however, the same caveats apply - there is no guarantee that the file will displayed to the user with Excel -

There is a free Excel viewer file available from MS which would let a non-Excel computer display the contents of an Excel file -
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now