.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
Imran Ghani

Home >> Code Snippets >> Javascript >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Save and load csv file using javascript

Posted By:Kemal AL GAZZAH       Posted Date: June 02, 2016    Points: 40    Category: Javascript    URL: http://www.dotnetspark.com  

Here a first version of javascript code allowing to - save data inputted by user from 5 fields into a csv file with ; separator - load a csv data file . Hopefully, I will post new versions with more features
 

<html>
<head>
<title>Save and load csv file using Javascript</title>
<script>

/*
save and load csv file with ; separator
using javascript
by Kamel Gazzah 
kamelgazzah@gmail.com
vERSION v1.0 02/06/2016
*/

/* CHANGE THE PATH of youf file here*/
const myfolder="d:\\dev\\data.txt";


function load()
{
var fso = new ActiveXObject("Scripting.FileSystemObject");  
var txtFile = fso.OpenTextFile(myfolder, 1, false, 0);         

var fText = txtFile.ReadAll();  
txtFile.Close();  
fso = null  
var array = fText.split("\r\n");


for (var i=0; i< array.length; i++) /**/
{
var array2=array[i].split(";");
for (var j=0;j<array2.length; j++)
{
document.getElementById("id"+(1*j+1)+1*(i+1)).value=array2[j];
}

}
}   








function WriteToFile(ofile,sText){ 
var fso = new ActiveXObject("Scripting.FileSystemObject"); 
var s = fso.CreateTextFile(ofile, true);
s.WriteLine(sText); 
s.Close(); 

function save()
{

var sText="";
var i=1;
var h=0
h=document.getElementById("hc").value;
if (h>0)
{
var fso = new ActiveXObject("Scripting.FileSystemObject"); 
var s = fso.CreateTextFile(myfolder, true); 


for(i=1;i<h+1;i++)
{


sText=document.getElementById("id1"+i).value+";"+document.getElementById("id2"+i).value+";"+document.getElementById("id3"+i).value+";"+document.getElementById("id4"+i).value+";"+document.getElementById("id5"+i).value;
s.WriteLine(sText); 
}
s.Close(); 
}
}



function addrow()
{
    var h=1*document.getElementById("hc").value;

    h=h+1;
    var table = document.getElementById("myTable");    
    var row = table.insertRow(h-1);    
    document.getElementById("hc").value=h;

    
    var element = document.createElement("input");   
    element.setAttribute("type", "text"); 
    element.setAttribute("value", h);
    element.setAttribute("id", "id0"+h);
    element.setAttribute("size", "20");
    var cell1 = row.insertCell(0);  
    cell1.appendChild(element);
    cell1.setAttribute("width", "10%");



    element = document.createElement("input");  
    element.setAttribute("type", "text");
    element.setAttribute("id", "id1"+h);
    element.setAttribute("size", "20");
    element.setAttribute("value", "");
    var cell2 = row.insertCell(1);
    cell2.appendChild(element);
    cell2.setAttribute("width", "11.25%");
 

    element = document.createElement("input");
    element.setAttribute("type", "text");
    element.setAttribute("id", "id2"+h);
    element.setAttribute("size", "20");
    element.setAttribute("value", "");
    var cell3 = row.insertCell(2);
    cell3.appendChild(element);
    cell3.setAttribute("width", "11.25%");

    element = document.createElement("input");
    element.setAttribute("type", "text");
    element.setAttribute("id", "id3"+h);
    element.setAttribute("size", "20");
    element.setAttribute("value", "");
    var cell4 = row.insertCell(3);
    cell4.appendChild(element);
    cell4.setAttribute("width", "11.25%");

    element = document.createElement("input");
    element.setAttribute("type", "text");
    element.setAttribute("id", "id4"+h);
    element.setAttribute("size", "20");
    element.setAttribute("value", "");  
    var cell5 = row.insertCell(4);
    cell5.appendChild(element);
    cell5.setAttribute("width", "11.25%");

    element = document.createElement("input");
    element.setAttribute("type", "text");
    element.setAttribute("id", "id5"+h);
    element.setAttribute("size", "20");
    element.setAttribute("value", "");
    var cell6 = row.insertCell(5);
    cell6.appendChild(element);
    cell6.setAttribute("width", "11.25%");

    

   
   

    element = document.createElement("input");
    element.setAttribute("type", "button");
    element.setAttribute("value", "Add");
    element.setAttribute("onclick", "addrow()");
    element.setAttribute("id", "b"+h);
    element.setAttribute("size", "20");
    var cell8 = row.insertCell(6);
    cell8.appendChild(element);
    cell8.setAttribute("width", "11.25%");


    if(h==1)
{
element = document.createElement("input");
    element.setAttribute("type", "button");
    element.setAttribute("value", "SAVE");
    element.setAttribute("onclick", "save()");
    element.setAttribute("id", "b"+h);
    element.setAttribute("size", "20");
    var cell9 = row.insertCell(7);
    cell9.appendChild(element);
    cell9.setAttribute("width", "11.25%");



element = document.createElement("input");
    element.setAttribute("type", "button");
    element.setAttribute("value", "Load");
    element.setAttribute("onclick", "load()");
    element.setAttribute("id", "b"+h);
    element.setAttribute("size", "20");
    var cell10 = row.insertCell(8);
    cell10.appendChild(element);
    cell10.setAttribute("width", "11.25%");
}


    
 

}


</script>

</head>
<body onload="addrow()">

<!--<input type="button" id="b1" onclick="WriteToFile('d:\\dev\\output.txt',document.getElementById('t1').value)" value="Save" />
<textarea id="t1"></textarea>
-->


<table id="myTable" width="100%">
</table>
<input type="hidden" id="hc" value="0">
</body>
</html>



     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend