Link to home
Start Free TrialLog in
Avatar of xiaoxiangz
xiaoxiangz

asked on

javascript create and open csv file on the fly

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>
ASKER CERTIFIED SOLUTION
Avatar of avner
avner

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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&
Avatar of LaurenceHolbrook
LaurenceHolbrook

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

https://www.experts-exchange.com/questions/20459721/Urgent-Problem-with-document-open-method.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 -