首页 > 其他分享 >使用xlxs.js读取上传xlsx文件

使用xlxs.js读取上传xlsx文件

时间:2022-11-19 10:24:54浏览次数:46  
标签:xlsx sheet XLSX res utils xlxs js worksheet var

xlsx的github地址:https://github.com/SheetJS/sheetjs

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <script src="jquery/jquery-1.9.1.min.js"></script>
    <script src="sheetjs-master/dist/xlsx.full.min.js"></script>
    <script>
      function readFile() {
        const file = $("#uploadFile")[0].files[0];
        const fileTypes = [
            'application/vnd.ms-excel',
            'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
        ]
        if (fileTypes.indexOf(file.type) === -1) {
            alert('文件类型错误');
            return;
        }
        var reader = new FileReader();
        const data = [];
        reader.onload = function (e) {
          var data = e.target.result;
          var workbook = XLSX.read(data, { type: "binary",  cellDates: true});
          var sheetNames = workbook.SheetNames; // 工作表名称集合
          var worksheet = workbook.Sheets[sheetNames[0]]; // 这里我们只读取第一张sheet
          // var res = XLSX.utils.sheet_to_csv(worksheet);
          // var res = XLSX.utils.sheet_to_txt(worksheet);
          // var res = XLSX.utils.sheet_to_html(worksheet);
          var res = XLSX.utils.sheet_to_json(worksheet);
          console.log(res);
        };
        reader.readAsBinaryString(file);
      }
   </script>
  <body>
    <input type="file" name="file" id="uploadFile" size="10" onchange="readFile(this);" />
  </body>
</html>

 

标签:xlsx,sheet,XLSX,res,utils,xlxs,js,worksheet,var
From: https://www.cnblogs.com/caroline2016/p/16905546.html

相关文章