最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Export a Local Html form data to CSV using Javascript or VBscript - Stack Overflow

programmeradmin5浏览0评论

I have created an HTML Form which will be used from a local puter 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.

Any help or idea is appreciated.

I have created an HTML Form which will be used from a local puter 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.

Any help or idea is appreciated.

Share asked Jul 7, 2012 at 6:05 Akash BajajAkash Bajaj 311 gold badge1 silver badge3 bronze badges 2
  • Unless you circumwent (reasonable) security restrictions, a browser can't access the local file system. Unless your 'application' is strictly 'one user, single process', writing to a .csv is risky. So without more details about what you want to achieve, the answer is: Don't even try. – Ekkehard.Horner Commented Jul 7, 2012 at 7:30
  • I want a sort of survey to be pleted by a number of people and the results to be saved directly to a CSV file. Its a work puter, so cannot use excel forms or something like that. Any pointers to this are greatly appreciated. – Akash Bajaj Commented Jul 8, 2012 at 3:07
Add a ment  | 

3 Answers 3

Reset to default 1

I assume this is an IE-only question (VBScript). If so, you can use ActiveXObject called FileSystemObject.

JavaScript:

csv=[]; // Collect form values to this array.

function saveFile(csv){
    var fso,oStream;
    fso=new ActiveXObject('Scripting.FileSystemObject');
    oStream=fso.OpenTextFile('absolute_file_path',8,true);
    oStream.WriteLine(csv.join(','));
    oStream.Close();
    return;
}

function readFile(path){
    var fso,iStream,n,csv=[];
    fso=new ActiveXObject('Scripting.FileSystemObject');
    iStream=fso.OpenTextFile(path,1,true);
    for(n=0;!iStream.AtEndOfStream;n++){
        csv[n]=iStream.ReadLine().split(',');
    }
    iStream.Close();
    return csv;
}

You can read more about FileSystemObject in MSDN.

@Ekkehard.Horner is right in that a browser can't write directly to the local file system.

However, when submitting the form, it is certainly possible to update another part of the browser window provided that your browser is JavaScript enabled. You can then cut-and-paste the accumulated content in your browser to a file if that is your objective.

Here's a simple example I put together that illustrates this concept with just a little JavaScript and a few simple form elements:

<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3/1999/xhtml">
<head>
<title>Capture Form Fields to CSV</title>
<script type="text/javascript">
<!--
function saveValues() {
    var frm = document.form1;

    var record = ""
            +       frm.text1.value
            + "," + frm.text2.value
            + "," + frm.text3.value
            + "\n";

    frm.textarea1.value += record;
}

function clearText() {
    document.form1.textarea1.value = "";
}
//-->
</script>
</head>
<body>
<h1>Capture Form Fields to CSV</h1>

<form name="form1" action="javascript:null">
  <p>
    F1: <input name="text1" type="text" value="field1" /><br />
    F2: <input name="text2" type="text" value="field2"/><br />
    F3: <input name="text3" type="text" value="field3"/>
  </p>
  <p>
    <input name="save" type="button" value="Save"
           onclick="saveValues(); return false"/>
    &#0160;
    <input name="clear" type="button" value="Clear"
           onclick="clearText(); return false"/>
  </p>
  <p>
    <i>Click 'Save' to add content</i><br />
    <textarea name="textarea1" rows="5" cols="40"></textarea>
  </p>
</form>

</body>
</html>

You can certainly get much fancier than this if you are willing to dive into DHTML with a JavaScript library such as jQuery. Just consider the fantastic editing mode provided by this very site!

Old question, but I wanted to update with a good solid response:

Another way would be to use an emulator on the machine that will be running the script. This way you could build the form handling script in the same way that you would if it was hosted on a server. Check out WAMP (or MAMP for Mac), which will create a self contained running instance of Apache with PHP/MySQL on your system. Then you simply place the site's files into the directory either emulator specifies (usually 'www' or 'htdocs') and then you can run it via your browser locally as if it was directly on the server with PHP/MySQL.

发布评论

评论列表(0)

  1. 暂无评论