如何修复未捕获的异常:标题签名

时间:2016-08-05 09:59:38

标签: jquery excel uncaught-exception

我想上传一个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);
}

1 个答案:

答案 0 :(得分:2)

您可以对其进行编码,使其同时接受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);}

我尝试过它并且运行正常。如果我可以帮助您,请告诉我。