Click here to Skip to main content
15,944,737 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can i export the values of the text box in html to a csv or a excel
.I have created an HTML Form which will be used from a local computer and want the form data to be saved in CSV file.

Each time the form is submitted, it should add a line in the CSV file.

This needs to be run locally so cannot use PHP or JSP.
Posted
Updated 22-Jul-12 4:23am
v2
Comments
WoodenLegNamedSmith 19-Jul-12 13:38pm    
You cannot WRITE to a local hard drive with JavaScript, you can only do it invoking ActiveX or through server-side technologies...
If it HAS TO BE ran on the client's machine you could always setup IIS and turn the client machine into a server that would be capable of running the code needed. It would be similar to setting up a development machine that can test and debug its own code before you post to the server.

You could also try to store each value in an array and then document.write the array to the webpage after clearing out the document.innerHtml. The user would have to manually save the file but it would work. Good Luck!

Hope this may help you out -

HTML
<html>
  <head>
  <script language="javascript">
    function WriteToFile(passForm) {
      var fso = new ActiveXObject("Scripting.FileSystemObject");
      var fileLoc = "D:\\sample.csv";
      var file = fso.CreateTextFile(fileLoc, true);
      file.writeline(passForm.FirstName.value + ',' +
                     passForm.LastName.value);
      file.Close();
      alert('File created successfully at location: ' + fileLoc);
     }
  </script>
  </head>
  <body>
  <p>create a csv file with following details -</p>
  <form>
    Type your first name: <input type="text" name="FirstName" size="20"><br>
    Type your last name: <input type="text" name="LastName" size="20"><br>
    <input type="button" value="submit" onclick="WriteToFile(this.form)">
  </form>
  </body>
</html>
</br></br></html>


Also, refer to this link File Handling at Client Side Using Javascript[^] for further reference.

Regards,
Niral Soni
 
Share this answer
 
Comments
enhzflep 22-Jul-12 13:55pm    
Great answer, my +5
Notably, scrrun.dll (implements the Scripting.FileSystemObject object) is installed by default since at least XP SP1 - a safe, cheap way to do the job. :)
Just because you don't like the answer WoodenLegNamedSmith gave doesn't stop it being right.
Bumping the question to try and get a answer more to you liking won't change how computers work!

So to re-iterate the answer: Javascript does not have access to the hard-drive on the client PC, nor does the code behind which runs on the server. Security considerations forbid it.

You cannot write a Excel file or a CSV file (or a text file, or a PDF file, or any other type of file) directly to the PC - all you can do is write it to the server and offer it for download and saving to the client - even then you have no control whatsoever over where the client stores the data, or even that he downloads it.

As WoodenLegNamedSmith says, it could be done with an ActiveX control, but the chances of getting one of those installed on any PC these days is remote.
 
Share this answer
 
Comments
enhzflep 22-Jul-12 11:55am    
Sorry old son, gotta disagree with you on two points here.

1. The post was edited by another user, NOT the op..
2. I think most people wouldn't think twice about installing MS Office these days - there's your ActiveX right there.

If the OP had googled read/write excel with javascript, (s)he'd have got quite a number of results that would facilitate the job at hand.

But otherwise, good points. My 4.
Espen Harlinn 23-Jul-12 4:14am    
It's fairly common to disable scripting of activex controls for security reasons ...
OriginalGriff 23-Jul-12 4:29am    
I think (but I could be wrong) that is is the default setting on most browsers now. Chrome for example requires IE installed for ActiveX to work at all!
Espen Harlinn 23-Jul-12 4:35am    
I think you are right, and I haven't seen a corporate environment in a long time that allows activex scripting in the browser.
You could also create a cookie with an indefinite expiry, and use that as a pseudo-database to generate a block of text in CSV format that could then be used to generate a text file. You could then either reload the cookie every time if you can guarantee persistence, or parse the saved CSV when you open the web page locally. This should help get you started:

Code taken from: Javascript: Create and save file - Stack Overflow[^]; credit to: Awesomeness01

JavaScript
function download(text, name, type) {
  var a = document.getElementById("a");
  var file = new Blob([text], {type: type});
  a.href = URL.createObjectURL(file);
  a.download = name;
}
<a href="" id="a">click here to download your file</a>
<button onclick="download('file text', 'myfilename.txt', 'text/plain')">Create file</button>
 
Share this answer
 
Comments
CHill60 20-Jul-17 18:41pm    
Not a bad answer ... but 5 years late!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900