10 Aralık 2015 Perşembe

Read an Excel File with JavaScript (without ActiveXObject)

  1. < script type = "text/javascript" > function Upload() {  
  2.     var fileUpload = document.getElementById("fileUpload");  
  3.     var regex = /^([a-zA-Z0-9\s_\\.\-:])+(.csv|.txt)$/;  
  4.     if (regex.test(fileUpload.value.toLowerCase())) {  
  5.         if (typeof(FileReader) != "undefined") {  
  6.             var reader = new FileReader();  
  7.             reader.onload = function(e) {  
  8.                 var table = document.createElement("table");  
  9.                 var rows = e.target.result.split("\n");  
  10.                 for (var i = 0; i < rows.length; i++) {  
  11.                     var row = table.insertRow(-1);  
  12.                     var cells = rows[i].split(",");  
  13.                     for (var j = 0; j < cells.length; j++) {  
  14.                         var cell = row.insertCell(-1);  
  15.                         cell.innerHTML = cells[j];  
  16.                     }  
  17.                 }  
  18.                 var dvCSV = document.getElementById("dvCSV");  
  19.                 dvCSV.innerHTML = "";  
  20.                 dvCSV.appendChild(table);  
  21.             }  
  22.             reader.readAsText(fileUpload.files[0]);  
  23.         } else {  
  24.             alert("This browser does not support HTML5.");  
  25.         }  
  26.     } else {  
  27.         alert("Please upload a valid CSV file.");  
  28.     }  
  29. } < /script>  

Hiç yorum yok: