Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import CSV file into HTML form, and export HTML form to CSV file

I'm having trouble when trying to import a CSV file into an HTML form. I want the user to be able to pick a CSV file from their computer, and when they do, it will automatically fill the form for them. At the same time, if the user didn't import a file, I'd like it to be so that they can export their newly typed form into a CSV file and save it to their computer. I would really like to use javascript for this, and not PHP or a database. It doesn't seem too difficult, just don't know where to start. Thanks for answers.

like image 853
Austin P. Avatar asked Oct 09 '15 15:10

Austin P.


People also ask

Can I export HTML data to a CSV file?

As demonstrated above, we exported HTML data to a CSV file in less than 10 lines of code. But we are not limited to only that, we can create and export Blob files with different content types like JSON, excel, word, plain text…etc. I hope you enjoyed learning from this article and if you have any questions, please leave a comment below.

What is a CSV file and how to use it?

CSV files are very useful for the import and export of data to other software applications. Sometimes while developing web applications, you may come into a scenario where you need to download a CSV file converted from an HTML table.

How do I convert a table to a CSV file?

Step 1: Create an HTML table: Create a simple HTML page with a table and a button. This button will be used as a trigger to convert the table into comma-separated values and download it in the form of a CSV file. Apply your own needed CSS stylings.

How do I upload a CSV file to a form?

Create a file and name it `index.php` . This is a simple form for uploading CSV file. This file will also show the results in a simple table on the same page. When the user submits the form, all records will be saved in the database.


1 Answers

I think this will start you off. I use d3.js to parse the file.

HTML

<input id="upload" type="file">
<form id="csvForm">
    <input id="a"></input>
    <input id="b"></input>
    <input id="c"></input>
    <input id="d"></input>
</form>
<button id="download">Download</button>

JS

document.getElementById("upload").addEventListener("change", upload, false);
document.getElementById("download").addEventListener("click", download, false);

function upload(e) {
    var data = null;
    var file = e.target.files[0];

    var reader = new FileReader();
    reader.readAsText(file);
    reader.onload = function (event) {
        var csvData = event.target.result;

        var parsedCSV = d3.csv.parseRows(csvData);

        parsedCSV.forEach(function (d, i) {
            if (i == 0) return true; // skip the header
            document.getElementById(d[0]).value = d[1];
        });
    }
}

function download(e) {
    data = [["id","value"]];

    var f = d3.selectAll("#csvForm > input")[0];
    f.forEach(function(d,i){
        data.push([d.id, d.value]);
    });

    var csvContent = "data:text/csv;charset=utf-8,";
    data.forEach(function (d, i) {
        dataString = d.join(",");
        csvContent += i < data.length ? dataString + "\n" : dataString;
    });

    var encodedUri = encodeURI(csvContent);
    var link = document.createElement("a");
    link.setAttribute("href", encodedUri);
    link.setAttribute("download", "FormData.csv");
    link.click(); 
}

where your CSV is structured like so, with each row having a corresponding input id:

id,value
a,red
b,blue
c,green
d,yellow

http://jsfiddle.net/rg8td0Ln/5/

like image 58
Charles Clayton Avatar answered Oct 08 '22 10:10

Charles Clayton