2017-08-12 79 views
0

我在這裏有一個問題 我做了一個頁面,用於上傳一個csv文件,然後將它們顯示到一個html表格中。我使用簡單的excel庫來做到這一點。 我從csv文件得到正確的價值,但我不知道爲什麼。該值不會顯示爲html表格。我已經做出了這個方法,但我認爲這是行不通的。顯示一個csv數據到html表中時顯示結果爲false

有人請幫助我。錯誤顯示結果我的意思是這樣的 我給我顯示 enter image description here

這是我的HTML代碼和我的js的屏幕截圖稱他們

<script type="text/javascript"> 
 

 
      // check browser support 
 
      // console.log(SimpleExcel.isSupportedBrowser); 
 

 
      var fileInputCSV = document.getElementById('file'); 
 

 

 
      // when local file loaded 
 
      fileInputCSV.addEventListener('change', function (e) { 
 

 
       // parse as CSV 
 
       var file = e.target.files[0]; 
 
       var csvParser = new SimpleExcel.Parser.CSV(); 
 
       csvParser.setDelimiter(','); 
 
       csvParser.loadFile(file, function() { 
 

 
        // draw HTML table based on sheet data 
 
        var sheet = csvParser.getSheet(); 
 
        var table = document.getElementById('result'); 
 
        table.innerHTML = ""; 
 
        sheet.forEach(function (el, i) {      
 
         var row = document.createElement('tr'); 
 
         el.forEach(function (el, i) { 
 
          var cell = document.createElement('td'); 
 
          cell.innerHTML = el.value; 
 
          row.appendChild(cell); 
 
         }); 
 
         table.appendChild(row); 
 
        });      
 

 
       }); 
 
      }); 
 

 

 

 
     </script>
<div id ="bodi1" style="border:thick; width:100%; height:15%"> 
 
<form action="../php/tagihan/tagihan_input.php" id="select1" class="select1" name="select1" method="post" enctype="multipart/form-data"> 
 

 
<table width="50%" style="margin-left:40px"> 
 
<p align="center" style="margin-left: 10px"> <strong>Input Data Tagihan Vendor</strong> <br /><br /> 
 
    
 
    <tr> 
 
    <td width="120" style="border:none"><label style="float:left">Input File</label></td> 
 
    <td width="5" style="border:none"><label>:</label></td> 
 
    <td width="215" style="border:none"><input type="file" id="file" name="file" size="25" 
 
    title="Harap check kelengkapan file .csv"/> 
 
    </td> 
 
    <td width="150" style="border:none"> 
 
    <img src="../image/ok.png" class="button" name="ok" id="ok" style="height 
 
    :35px; width:40px" /> 
 
    <img src="../image/show.png" class="button" name="dir" id="dir" style="height:35px; width:40px" 
 
    onclick="up();" /> 
 
    <img src="../image/circle_close_delete_-128.png" class="button" name="close" id="close" 
 
    style="height:35px; width:40px"/> 
 
    </td> 
 
    </tr> 
 
    
 
</table> 
 
<table id="result"> 
 
<tr> 
 
</tr> 
 
<td> 
 
</td> 
 
</table> 
 
</form> 
 
</div>

這個CSV文件enter image description here

+0

你能分享你的csv文件嗎 –

+0

我怎麼能分享它? –

+0

你可以附上一張圖片或者簡單的寫出csv的值。2行應該這樣做。 –

回答

0

enter image description hereenter image description here 我在這裏使用了Simple版Excel的js版本。

<script src="http://faisalman.github.io/simple-excel-js/src/simple-excel.js"></script> 

<div id="bodi1" style="border: thick; width: 100%; height: 15%"> 
    <input type="file" id="fileInputCSV"> 
    <table id="result"> 
    </table> 
</div> 

<script> 
     // check browser support 
     // console.log(SimpleExcel.isSupportedBrowser); 
     var fileInputCSV = document.getElementById('fileInputCSV'); 

     // when local file loaded 
     fileInputCSV.addEventListener('change', function (e) { 

      // parse as CSV 
      var file = e.target.files[0]; 
      var csvParser = new SimpleExcel.Parser.CSV(); 
      csvParser.setDelimiter(','); 
      csvParser.loadFile(file, function() { 

       // draw HTML table based on sheet data 
       var sheet = csvParser.getSheet(); 
       var table = document.getElementById('result'); 
       table.innerHTML = ""; 
       sheet.forEach(function (el, i) { 
        var row = document.createElement('tr'); 
        el.forEach(function (el, i) { 
         var cell = document.createElement('td'); 
         cell.innerHTML = el.value; 
         row.appendChild(cell); 
        }); 
        table.appendChild(row); 
       }); 
      }); 
     }); 
</script> 

請將這些代碼塊相應地安排在您的文件中。第二個腳本標籤應該作爲body標籤內的最後一個標籤。

+0

我試過你的代碼,但仍然一樣T_T –

+0

你如何設置csv文件? 你給csv一個邊框或不是? –

+0

或我的excel文件格式不是2003文件? 我使用excel 2007 –