2016-08-05 62 views
0

我想上传一个excel文档,阅读该文档,并以表格格式在jquery中编写excel文档内容,我通过here的示例参考实现了此目的。首先感谢那参考,但在这里我上传我的Excel文件从系统本地存储它显示错误称为“未捕获的异常:标头签名:预计d0cf11e0a1b11ae1锯504b030414000600”。如果我正在上传我从here下载的文档正在生成成功的结果。我没有得到什么是我最近2天有这个错误发生的错误 任何人可以告诉我如何解决这个问题如何解决未捕获的异常:标题签名

这是我的HTML代码

<html> 
    <head> 
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> 
     <script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.8.0/jszip.js"></script> 
     <script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.8.0/xlsx.js"></script> 
     <script src=" https://cdnjs.cloudflare.com/ajax/libs/xls/0.7.4-a/xls.js"></script> 
     <title>Test Excel</title> 
    </head> 
    <body onload="load_excel();"> 
     <input type="file" id="my_file_input"/> 
     <div id='my_file_output'></div> 
     <script type="text/javascript" src="cordova.js"></script> 
     <script type="text/javascript" src="js/index.js"></script> 
    </body> 
</html> 

这是我的JS代码:

function load_excel(){ 

    var oFileIn; 
    oFileIn = document.getElementById('my_file_input'); 
    if(oFileIn.addEventListener) { 
     oFileIn.addEventListener('change', filePicked, false); 
    } 

} 
function filePicked(oEvent){ 

    alert('Function trigggered'); 

    // Get The File From The Input 
    var oFile = oEvent.target.files[0]; 
    var sFilename = oFile.name; 
    // Create A File Reader HTML5 
    var reader = new FileReader(); 

    // Ready The Event For When A File Gets Selected 
    reader.onload = function(e) { 
     var data = e.target.result; 
     var cfb = XLS.CFB.read(data, {type: 'binary'}); 
     var wb = XLS.parse_xlscfb(cfb); 
     // Loop Over Each Sheet 
     wb.SheetNames.forEach(function(sheetName) { 
      // Obtain The Current Row As CSV 
      var sCSV = XLS.utils.make_csv(wb.Sheets[sheetName]); 
      var data = XLS.utils.sheet_to_json(wb.Sheets[sheetName], {header:1}); 
      $.each(data, function(indexR, valueR) { 
       var sRow = "<tr>"; 
       $.each(data[indexR], function(indexC, valueC) { 
        sRow = sRow + "<td>" + valueC + "</td>"; 
       }); 
       sRow = sRow + "</tr>"; 
       $("#my_file_output").append(sRow); 
      }); 
     }); 
    }; 

    // Tell JS To Start Reading The File.. You could delay this if desired 
    reader.readAsBinaryString(oFile); 
} 
+0

你是否发现两个excel文件有什么区别? –

+0

谢谢你的建议。它只是从'XLS'格式保存的excel文件中检索数据,而不是'XLSX',通过将文件格式更改为'XLS'来完美检索数据。谢谢你@HassanALi – lakshmansundeep

+0

I为你感到高兴。 –

回答

1

,使其同时接受XLSX和XLS您可以可以编写它。根据你的问题和SheetJS/js-xlsx的文档,我建议是这样的:

function filePicked(oEvent) { 
// Get The File From The Input 
var oFile = oEvent.target.files[0]; 
var sFilename = oFile.name; 
// Create A File Reader HTML5 
var reader = new FileReader(); 

// Ready The Event For When A File Gets Selected 
reader.onload = function(e) { 
    var data = e.target.result; 
    var cfb = XLSX.read(data, {type: 'binary'}); 
    console.log(cfb) 
    cfb.SheetNames.forEach(function(sheetName) { 
     // Obtain The Current Row As CSV 
     var sCSV = XLS.utils.make_csv(cfb.Sheets[sheetName]); 
     var oJS = XLS.utils.sheet_to_json(cfb.Sheets[sheetName]); 

     $("#my_file_output").html(sCSV); 
     console.log(oJS) 
     $scope.oJS = oJS 
    }); 
}; 

// Tell JS To Start Reading The File.. You could delay this if desired 
reader.readAsBinaryString(oFile);} 

我尝试过了,它工作正常。让我知道我是否可以帮助你。