?
Solved

Javascript SaveAs command

Posted on 2003-03-17
8
Medium Priority
?
4,146 Views
Last Modified: 2013-11-19
Not sure how easy this is, but here goes....

I have an HTML page with a table of data on it. I want to give the user the option to save the data in a .csv or Excel format.
Is this possible using Javascript?


0
Comment
Question by:mrt1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1
8 Comments
 
LVL 63

Expert Comment

by:Zvonko
ID: 8150920
If your sending this table from a web server then it is easy.

Invoking the SavaAs action should send the table html code alone. And it has to send this header for that response:
Content-Type:application/vnd.ms-excel

Followed by an empty line.

This is a VB example:

     Print "Content-Type:application/vnd.ms-excel"
     Print ""
     Print "<TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0>"
     Print "<TR VALIGN=top>"
... and so on
0
 
LVL 2

Author Comment

by:mrt1
ID: 8150944
I have a Perl script which populates the table.
I want a simple button on this 'results' page which allows the user to save the results in a different format.

What's the exact Javascript syntax I need to use?
0
 
LVL 63

Accepted Solution

by:
Zvonko earned 500 total points
ID: 8150964
In Perl it is easy to send the header type information to web client.
After the content is received it is far more difficult to manipulate content headers or produce CSV files and store them.

So my advice is to do it from the server, not with client side JavaScript.

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 2

Author Comment

by:mrt1
ID: 8151171
I'm afraid I don't have any Perl experience. How easy it is to write to an Excel file using Perl?

If it's not really simple, I'd rather take my chances with Javascript.
0
 

Assisted Solution

by:LaurenceHolbrook
LaurenceHolbrook earned 500 total points
ID: 9254059
This really requires a two part answer -  with client side scripting is how to save the html table as CSV file -

The trick here is how to 'grab' the data - one approach is to use the Document Object Model - the other part is writing the CSV file - this task can be done with the File System Object  -

Note the table id in the <table ...> tag which enables the DOM (x=myTable.rows) to identify the data in the rows and columns -

Writing of the data is not extensively error checked - for example an embeded comma in a data element requires quotes around that data element - embedded quotes will be interpreted as text delimiters - the file will probably be created without error, but the application reading the file won't retrieve the data properly -

<html>
 
<head>
<script type="text/javascript">

function WriteCSVFile()
{
 
   var x = myTable.rows
   var ForWriting = 2
//   var ForAppending = 8
   var TriStateFalse = 0

   var fsObj = new ActiveXObject("Scripting.FileSystemObject")
 
   var newFile = fsObj.OpenTextFile("c:\\ClientSideJScriptFile.csv", ForWriting, true, TriStateFalse)

      for (i = 0; i < x.length; i++)
      {
         var y = x[i].cells
         var strTmp = y[0].innerText
         if (y.length > 0)
       {
            for (j = 1; j < y.length; j++)
            {
               strTmp = strTmp + "," + y[j].innerText
            }
       }

         newFile.WriteLine(strTmp)
      }

   newFile.Close()
 
}

</script>
</head>
 
<body>

<form>
<input type="button" onclick="WriteCSVFile()" value="Create CSV File">
</form>
 
<%
Put any table here, your little ol' heart desireth
If you change the id, you gotta change it in the script
%>
 
<table id="myTable" border="1">
<tr>
<td>Abe</td>
<td>23</td>
</tr>
<tr>
<td>Betty</td>
<td>34</td>
</tr>
<tr>
<td>Harold</td>
<td>28</td>
</tr>
</table>

</body>
</html>
0
 

Expert Comment

by:LaurenceHolbrook
ID: 9254097
This really requires a two part answer -  with client side scripting how to save the html table as XLS file -

[it should be noted that most Windows computers with Excel installed will open CSV files with Excel] -

The first problem here is who the hell knows what the 'Excel' file format is - the easiest answer is Excel - an approach here would be to open Excel, use the DOM to 'grab' the data from the HTML table and populate the Excel workbook and then let Excel save the file -

Something like the following will work -

<html>
 
<head>
<script type="text/javascript">


function CreateExcelSheet()
{
   var x=myTable.rows
 
   var xls = new ActiveXObject("Excel.Application")
 
   xls.Workbooks.Add
 
      for (i = 0; i < x.length; i++)
      {
         var y = x[i].cells
 
         for (j = 0; j < y.length; j++)
         {
            xls.Cells( i+1, j+1).Value = y[j].innerText
         }
      }
 
   //  To make it display in its own window
   xls.visible = true
 
   xls.ActiveWorkbook.Save        ()
   xls.ActiveWorkbook.Close       ()
 
}

</script>
</head>
 
<body>

<form>
<input type="button" onclick="CreateExcelSheet()" value="Create Excel Sheet">
</form>
 
<%
Put any table here, your little ol' heart desireth
If you change the id, you gotta change it in the script
%>
 
<table id="myTable" border="1">
<tr>
<td>Abe</td>
<td>23</td>
</tr>
<tr>
<td>Betty</td>
<td>34</td>
</tr>
<tr>
<td>Harold</td>
<td>28</td>
</tr>
</table>

</body>
</html>


0
 
LVL 16

Expert Comment

by:jaysolomon
ID: 10131380
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

Split: Zvonko {http:#8150964} & LaurenceHolbrook {http:#9254059}

Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jAy
EE Cleanup Volunteer
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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…

719 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