Wednesday 30 January 2013

Writing Data to CSV File using JavaScript


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
    <head>
        <title>Writing Data to CSV</title>
        <script type="text/javascript">
            function addToCSVFile() {
                var csvData = new Array();  // To collect the names
                var csvFilePath = "F:\\Data.csv"; // File name

                // Collecting the names
                csvData[0] = document.getElementById('firstName').value;
                csvData[1] = document.getElementById('middleName').value;
                csvData[2] = document.getElementById('lastName').value;

                var fso = new ActiveXObject('Scripting.FileSystemObject');
                var oStream = fso.OpenTextFile(csvFilePath, 8, true, 0);
                oStream.WriteLine(csvData.join(','));
                oStream.Close();
                clearData();
                alert("Data Added Successfully");
            }

            function clearData() {
                document.getElementById('firstName').value = "";
                document.getElementById('middleName').value = "";
                document.getElementById('lastName').value = "";
            }
        </script>
    </head>
    <body>
        <p>
            First Name: <input type="text" id="firstName" />
            <br />
            Middle Name: <input type="text" id="middleName" />
            <br />
            Last Name: <input type="text" id="lastName" />
            <br />
            <input type="button" id="addButton" value="Add to CSV File" onClick="javascript: addToCSVFile()" />
        </p>
    </body>
</html>

Note: This code only works in Internet Explorer.



2 comments:

  1. hi, do you have an example whereby there is a option to update the value of the last name in the csv file?

    ReplyDelete