0

cvs data fileI am try to display the csv file data in a html table. I can display the data but in a incorrect formate.

I tried to split the column with ",". But the problem has a data in of the row some think like this "study period: Semester 1, 2019". The 2019 is showing in a different table.

<script type="text/javascript">
    function Upload() {
        var fileUpload = document.getElementById("fileUpload");
        var regex = /^([a-zA-Z0-9\s_\\.\-:])+(.csv|.txt)$/;
        if (regex.test(fileUpload.value.toLowerCase())) {
            if (typeof (FileReader) != "undefined") {
                var reader = new FileReader();
                  reader.on load = function (e) {
                    var table = document.createElement("table");
                    var rows = e.target.result.split("\n");
                    for (var i = 0; i < rows.length; i++) {
                        var cells = rows[i].split(",");
                        if (cells.length > 1) {
                            var row = table.insertRow(-1);
                            for (var j = 0; j < cells.length; j++) {
                                var cell = row.insertCell(-1);
                                cell.innerHTML = cells[j];
                            }
                        }
                    }
                    var dvCSV = document.getElementById("dvCSV");
                    dvCSV.innerHTML = "";
                    dvCSV.appendChild(table);
                }
                reader.readAsText(fileUpload.files[0]);
            } else {
                alert("This browser does not support HTML5.");
            }
        } else {
            alert("Please upload a valid CSV file.");
        }
    }
</script>
<input type="file" id="fileUpload" />
<input type="button" id="upload" value="Upload" on click="Upload()" />
<hr />
<div id="dvCSV">
</div>

i expected to get the result as entity in respective rows and column, but i am getting half of the data in one row and rest in another

text
  • 93
  • 1
  • 1
  • 5
  • If the CSV file is rendering like that in your spreadsheet program I have to imagine that text is also delimited in quotes. – tshimkus Mar 26 '19 at 00:29
  • 1
    Possible duplicate of [How can I parse a CSV string with Javascript, which contains comma in data?](https://stackoverflow.com/questions/8493195/how-can-i-parse-a-csv-string-with-javascript-which-contains-comma-in-data) – tshimkus Mar 26 '19 at 00:31

0 Answers0